【问题标题】:Tween animation on a Canvas in a custom View自定义视图中画布上的补间动画
【发布时间】:2011-02-09 22:44:10
【问题描述】:

我有一个扩展 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);

        }
    }

如何在此处绘制动画(补间动画)?

【问题讨论】:

    标签: android animation canvas


    【解决方案1】:

    您不能在另一个类的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);
    
        }
      }
    }
    

    【讨论】:

    • 'TerrorizedBus' 是我的'Activity',我用它来获取'Context'
    • 好的。但那我该如何制作动画呢?
    • @iuliux。我为你扩展了我的答案。希望能帮助到你。如果您需要更多帮助,请告诉我。
    • 是的,第二个选项就是我想要的。实际上我已经尝试过了,但我无法将那个 SmokeView 放在我的画布上以启动它的动画。
    • 您不能将View 放在Canvas 上。您需要将其添加到您的布局中,即您的RelativeLayout,其中包含您的其他扩展View。就我个人而言,我会学习执行选项 1 并使用Bitmaps,尤其是在这是一款游戏的情况下。你可以做的是让 smokeView 扩展 ImageView 并覆盖它 onDraw 方法,但你仍然需要移动画布。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-09
    相关资源
    最近更新 更多