补间动画在自定义视图中不起作用

发布于 2024-12-18 09:11:16 字数 2616 浏览 0 评论 0原文

我在自定义视图中制作补间动画。所以我创建“MyView Extend view”并且 “Sprite 扩展 ImageView”。我在 Myview 中创建“Sprite”实例,并调用 sprite.onDraw() Myview.onDraw() 方法中的方法。但是......补间动画不起作用...... 只看到图像。帮我。我不明白。

这是源代码。 活动。

public class Myview01Activity extends Activity {
/** Called when the activity is first created. */
Animation MainRotate;
Myview myview;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    MainRotate = AnimationUtils.loadAnimation(Myview01Activity.this, R.anim.rotate);
    myview = new Myview(Myview01Activity.this);
    setContentView(myview);
    Log.i("TAG", "Come in");
}        

我的观点

class Myview extends View {
Sprite sprite;
public Myview(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    sprite = new Sprite(context);
    Log.i("TAG", "Come in");
}
@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    //super.onDraw(canvas);
    sprite.onDraw(canvas);
}

雪碧

class Sprite extends ImageView {
Bitmap bitmap;
Paint paint;
RotateAnimation rotate;
Animation myanimation;
AlphaAnimation blend;
ScaleAnimation scale;
AnimationSet spriteAnimation;
float centerX;
float centerY;
float offsetX;
float offsetY;

public Sprite(Context context) {
    super(context);
    bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
    offsetX = bitmap.getWidth() / 2;
    offsetY = bitmap.getHeight() / 2;
    paint = new Paint();
    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
}
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (spriteAnimation == null) {
        centerX = canvas.getWidth() / 2;
        centerY = canvas.getHeight() / 2;
        createAnimation(canvas);
        Log.i("TAG", "Come in");
    }
    canvas.drawBitmap(bitmap, centerX - offsetX, centerY - offsetY, paint);
}

public void createAnimation(final Canvas canvas) {
    rotate = new RotateAnimation(0, 360, centerX, centerY);
    rotate.setRepeatMode(Animation.REVERSE);
    rotate.setRepeatCount(Animation.INFINITE);
    scale = new ScaleAnimation(0, 2, 0, 2, centerX, centerY);
    scale.setRepeatMode(Animation.REVERSE);
    scale.setRepeatCount(Animation.INFINITE);
    scale.setInterpolator(new AccelerateDecelerateInterpolator());

    spriteAnimation = new AnimationSet(false);
    spriteAnimation.addAnimation(rotate);
    spriteAnimation.addAnimation(scale);
    spriteAnimation.setDuration(3000);
    startAnimation(spriteAnimation);

}

I make tween animation in Custom View. so I create "MyView Extend view" and
"Sprite Extend ImageView". And I make 'Sprite ' instance in Myview, and call sprite.onDraw()
Method in Myview.onDraw() Method. But... tween animation is not working....
Just image is seen. Help me. I don`t understand.

Here is the Source Code.
Activity.

public class Myview01Activity extends Activity {
/** Called when the activity is first created. */
Animation MainRotate;
Myview myview;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    MainRotate = AnimationUtils.loadAnimation(Myview01Activity.this, R.anim.rotate);
    myview = new Myview(Myview01Activity.this);
    setContentView(myview);
    Log.i("TAG", "Come in");
}        

}

Myview.

class Myview extends View {
Sprite sprite;
public Myview(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    sprite = new Sprite(context);
    Log.i("TAG", "Come in");
}
@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    //super.onDraw(canvas);
    sprite.onDraw(canvas);
}

Sprite

class Sprite extends ImageView {
Bitmap bitmap;
Paint paint;
RotateAnimation rotate;
Animation myanimation;
AlphaAnimation blend;
ScaleAnimation scale;
AnimationSet spriteAnimation;
float centerX;
float centerY;
float offsetX;
float offsetY;

public Sprite(Context context) {
    super(context);
    bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
    offsetX = bitmap.getWidth() / 2;
    offsetY = bitmap.getHeight() / 2;
    paint = new Paint();
    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
}
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (spriteAnimation == null) {
        centerX = canvas.getWidth() / 2;
        centerY = canvas.getHeight() / 2;
        createAnimation(canvas);
        Log.i("TAG", "Come in");
    }
    canvas.drawBitmap(bitmap, centerX - offsetX, centerY - offsetY, paint);
}

public void createAnimation(final Canvas canvas) {
    rotate = new RotateAnimation(0, 360, centerX, centerY);
    rotate.setRepeatMode(Animation.REVERSE);
    rotate.setRepeatCount(Animation.INFINITE);
    scale = new ScaleAnimation(0, 2, 0, 2, centerX, centerY);
    scale.setRepeatMode(Animation.REVERSE);
    scale.setRepeatCount(Animation.INFINITE);
    scale.setInterpolator(new AccelerateDecelerateInterpolator());

    spriteAnimation = new AnimationSet(false);
    spriteAnimation.addAnimation(rotate);
    spriteAnimation.addAnimation(scale);
    spriteAnimation.setDuration(3000);
    startAnimation(spriteAnimation);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠栀 2024-12-25 09:11:16

获取一个像这样的实例变量

Sprite view;

,并在创建时执行如下操作:

 MainRotate = AnimationUtils.loadAnimation(Myview01Activity.this, R.anim.rotate);
    view = new Sprite(this);
    setContentView(view);
    view.startAnimation(MainRotate);
    Log.i("TAG", "Come in");

尝试一下。它可能会起作用。

take an instance variable like this

Sprite view;

and inside on create do something like this:

 MainRotate = AnimationUtils.loadAnimation(Myview01Activity.this, R.anim.rotate);
    view = new Sprite(this);
    setContentView(view);
    view.startAnimation(MainRotate);
    Log.i("TAG", "Come in");

Try it.It may work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文