2011-02-09 65 views
3

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

     } 
    } 

如何使一个动画(补间动画)在这里画?

回答

7

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

    } 
    } 
} 
+0

'TerrorizedBus'是我的'活动',我用它来获取'上下文' – 2011-02-10 00:11:30