Tween Animation在自定义视图中不起作用

时间:2011-11-27 13:23:43

标签: android android-layout android-animation

我在自定义视图中制作补间动画。所以我创建了“MyView Extend view”和 “Sprite扩展ImageView”。我在Myview中创建'Sprite'实例,并调用sprite.onDraw() Myview.onDraw()方法中的方法。但是...补间动画不起作用...... 只是图像被看到了。帮我。我不明白。

这是源代码。 活性。

public class Myview01Activity extends Activity {
/** Called when the activity is first created. */
Animation MainRotate;
Myview myview;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    MainRotate = AnimationUtils.loadAnimation(Myview01Activity.this, R.anim.rotate);
    myview = new Myview(Myview01Activity.this);
    setContentView(myview);
    Log.i("TAG", "Come in");
}        

}

MyView的。

class Myview extends View {
Sprite sprite;
public Myview(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    sprite = new Sprite(context);
    Log.i("TAG", "Come in");
}
@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    //super.onDraw(canvas);
    sprite.onDraw(canvas);
}

的Sprite

class Sprite extends ImageView {
Bitmap bitmap;
Paint paint;
RotateAnimation rotate;
Animation myanimation;
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.ic_launcher);
    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);
        Log.i("TAG", "Come in");
    }
    canvas.drawBitmap(bitmap, centerX - offsetX, centerY - offsetY, paint);
}

public 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(false);
    spriteAnimation.addAnimation(rotate);
    spriteAnimation.addAnimation(scale);
    spriteAnimation.setDuration(3000);
    startAnimation(spriteAnimation);

}

1 个答案:

答案 0 :(得分:0)

采取像这样的实例变量

Sprite view;

并在创建内部执行以下操作:

 MainRotate = AnimationUtils.loadAnimation(Myview01Activity.this, R.anim.rotate);
    view = new Sprite(this);
    setContentView(view);
    view.startAnimation(MainRotate);
    Log.i("TAG", "Come in");

试试吧。它可能有用。