2011-02-09 5 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은'내 '활동'이고 나는 '컨텍스트'를 얻을 수있다 사용할 –

+0

같은 대답은 여전히 ​​적용됩니다. 'smokeView'는 비트 맵이어야합니다 ... – techiServices

+0

Ok. 그렇다면 어떻게 움직일 수 있습니까? –

관련 문제