在Android上启动AnimationDrawable

时间:2013-09-18 15:03:18

标签: android animation

我需要有关开始动画的帮助。我得到了第一堂课(图),我想在我的GameView课上开始我的Graph对象的动画。

class Graph {
private AnimationDrawable drawable;   

...

public Graph(View view, AnimationDrawable drawable){
      this.view = view;
      this.drawable = drawable;
      ancho = drawable.getIntrinsicWidth();  
      alto = drawable.getIntrinsicHeight();
      radioColision = (alto+ancho)/4;
}

public void drawGraph(Canvas canvas){
      canvas.save();
      int x=(int) (posX+ancho/2);
      int y=(int) (posY+alto/2);
      canvas.rotate((float) angulo,(float) x,(float) y);
      drawable.setBounds((int)posX, (int)posY,(int)posX+ancho, (int)posY+alto);
      drawable.draw(canvas);
      canvas.restore();
      int rInval = (int) Math.hypot(ancho,alto)/2 + MAX_VELOCIDAD;
      view.invalidate(x-rInval, y-rInval, x+rInval, y+rInval);
}

    public void animate(){
    drawable.start();
}
....

GameView类:

public class GameView extends View {

....
private static AnimationDrawable drawableFish;

private Graph fish;

public GameView(Context context, AttributeSet attrs) {

      super(context, attrs);
      drawableFish = (AnimationDrawable) context.getResources().getDrawable(R.drawable.animation);        
      fish = new Graph(this, drawableFish);        
}


@Override protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    fish.drawGraph(canvas);
    fish.animate();
}
....

以下是定义动画的xml:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:drawable="@drawable/fishmaligno"
    android:duration="200"/>
<item
    android:drawable="@drawable/fish"
    android:duration="200"/>

</animation-list>

我尝试使用animate()方法启动动画。

一切运作良好,但我无法启动动画。

我将不胜感激任何帮助。谢谢!

0 个答案:

没有答案
相关问题