吐温动画在自定义视图画布 [英] Tween animation on a Canvas in a custom View

查看:124
本文介绍了吐温动画在自定义视图画布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个扩展查看一类,我画我需要的一切的画布里面的的OnDraw()方法,是这样的:

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?

推荐答案

您无法绘制的ImageView 的OnDraw()另一个类的方法。

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);

    }
  }
}

这篇关于吐温动画在自定义视图画布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆