自定义视图中画布上的补间动画

发布于 2024-10-16 14:07:29 字数 576 浏览 2 评论 0原文

我有一个扩展 View 的类,我在 onDraw() 方法中在其画布内绘制了我需要的所有内容,如下所示:

protected void onDraw(Canvas canvas) {
        synchronized (this) {
                float h = mHeight;
                float w = mWidth;

                canvas.drawColor(Color.WHITE);

                float roadLine= (85.0f/100.0f)*h;

                canvas.drawBitmap(mTop, 0, roadLine-mTop.getHeight(), null);

                //this is what I'd like to animate
                canvas.drawBitmap(mSmoke);

        }
    }

How do I make an Animation (tween动画)画在这里?

I have a class that extends View, and I draw everything I need inside its canvas in the onDraw() method, something like this:

protected void onDraw(Canvas canvas) {
        synchronized (this) {
                float h = mHeight;
                float w = mWidth;

                canvas.drawColor(Color.WHITE);

                float roadLine= (85.0f/100.0f)*h;

                canvas.drawBitmap(mTop, 0, roadLine-mTop.getHeight(), null);

                //this is what I'd like to animate
                canvas.drawBitmap(mSmoke);

        }
    }

How do I make an Animation (tween animation) draw in here?

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

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

发布评论

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

评论(1

冷清清 2024-10-23 14:07:29

您无法在另一个类的 onDraw() 方法内绘制 ImageView

这可能更符合您的需求。

public class SimpleAnimation extends Activity {

Sprite sprite;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    sprite = new Sprite(this);
    setContentView(sprite);
}

class Sprite extends ImageView {

    Bitmap bitmap;
    Paint paint;
    RotateAnimation rotate;
    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.icon);
        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);
        }
        canvas.drawBitmap(bitmap, centerX - offsetX, centerY - offsetY, paint);
    }

    private 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(true);
        spriteAnimation.addAnimation(rotate);
        spriteAnimation.addAnimation(scale);
        spriteAnimation.setDuration(10000L);

        startAnimation(spriteAnimation);

    }
  }
}

You can't draw an ImageView inside the onDraw() method of another class.

This is probably more what you're after.

public class SimpleAnimation extends Activity {

Sprite sprite;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    sprite = new Sprite(this);
    setContentView(sprite);
}

class Sprite extends ImageView {

    Bitmap bitmap;
    Paint paint;
    RotateAnimation rotate;
    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.icon);
        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);
        }
        canvas.drawBitmap(bitmap, centerX - offsetX, centerY - offsetY, paint);
    }

    private 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(true);
        spriteAnimation.addAnimation(rotate);
        spriteAnimation.addAnimation(scale);
        spriteAnimation.setDuration(10000L);

        startAnimation(spriteAnimation);

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