1

我需要有关启动动画的帮助。我有一个第一类(Graph),我想在我的 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();
}
....

游戏视图类:

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() 开始动画。

一切正常,但我无法启动动画。

我将不胜感激。谢谢!

4

0 回答 0