补间动画在自定义视图中不起作用
我在自定义视图中制作补间动画。所以我创建“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");
}
我的观点
。
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);
}
雪碧
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);
}
I make tween animation in Custom View. so I create "MyView Extend view" and
"Sprite Extend ImageView". And I make 'Sprite ' instance in Myview, and call sprite.onDraw()
Method in Myview.onDraw() Method. But... tween animation is not working....
Just image is seen. Help me. I don`t understand.
Here is the Source Code.
Activity.
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
获取一个像这样的实例变量
,并在创建时执行如下操作:
尝试一下。它可能会起作用。
take an instance variable like this
and inside on create do something like this:
Try it.It may work.