Я делаю анимацию движения в пользовательском представлении. поэтому я создаю «MyView Extend view» и
"Sprite Расширить ImageView". И я делаю экземпляр Sprite в Myview и вызываю 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);
}