[英]how set ClickListener on drawable in Drawable class in android?
[英]How to animate a Drawable in Android?
我的应用程序中有一个Drawable
对象,该对象正在屏幕上移动:
public class MyDrawable extends Drawable {
@Override
public void draw(Canvas canvas) {
// ...
}
// ...
}
到目前为止一切正常,但是现在我必须给这个Drawable
做动画了。 我检查了文档,看来对我来说最好的选择是使用AnimationDrawable
。
我做了这样的事情:
public void addAnimation() {
animation = new AnimationDrawable();
animation.addFrame(resources.getDrawable(R.drawable.anim_0), 100);
animation.addFrame(resources.getDrawable(R.drawable.anim_1), 100);
animation.addFrame(resources.getDrawable(R.drawable.anim_2), 100);
animation.addFrame(resources.getDrawable(R.drawable.anim_3), 100);
animation.setOneShot(false);
refreshAnimationBounds();
animation.start();
}
@Override
public void draw(Canvas canvas) {
refreshAnimationBounds();
animation.draw(canvas);
}
发生的是我的原始Drawable
正在按预期移动,但我只看到第一帧。 可能是什么问题呢?
refreshAnimationBounds();
只是刷新我的Drawable
对象的边界(重新Drawable
它)。
编辑:
共同点:
public void onWindowFocusChanged(boolean hasFocus) {
if (hasFocus) {
animation.start();
} else {
animation.stop();
}
}
解决方案不起作用。
总的来说,Animation包有很多错误。 我回想起动画没有启动时遇到的同样问题,解决的方法是不像以前那样在onCreate()
启动动画,而是在设置所有视图内容之后在onWindowFocusChanged()
启动动画。 我本人并没有扩展AnimationDrawable,但在您的情况下它可能是同一起源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.