为什么从事件调用时 view.startAnimation(animation) 不起作用?
我创建了一个自定义视图,它使用虚拟 TranslateAnimation 来设置一些布局属性。我使用 Interpolator 来计算高度,并将其应用到 TranslateAnimation 的 applyTransformation() 方法内的视图。
如果我从我的活动中触发动画,这工作得很好。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i("test", "onCreate()");
view.expand(); // This method starts the animation
}
当我尝试使用触摸事件执行相同操作时,没有任何反应。
@Override
// This method is touch handler of the View itself
public boolean onTouch(View v, MotionEvent event) {
Log.i("test", "onTouch()");
this.expand(); // onTouch is part of the view itself and calls expand() directly
return true;
}
我的展开方法如下所示:
public void expand() {
Log.i("test", "Expand!");
TranslateAnimation anim = new TranslateAnimation(0, 0, 0, 0) {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
Log.i("test", "applyTransformation()");
super.applyTransformation(interpolatedTime, t);
// do something
}
};
anim.setDuration(500);
anim.setInterpolator(new AccelerateDecelerateInterpolator());
this.someInternalView.startAnimation(anim);
}
创建活动后 Logcat 显示“onCreate()” 在我的触摸事件 Logcat 中显示“onTouch()” 在 Expand() 方法内部,Logcat 显示“Expand!” - 从活动或事件中调用。
在方法 applyTransformation() Logcat 中显示“applyTransformation()” - 但是!仅当从 onCreate() 调用 Expand() 时。任何尝试从事件启动动画的尝试都会失败。
在我看来,这像是某种线程问题。这可能是吗?我有什么遗漏的吗?据我从其他帖子中看到,从事件启动动画应该没有任何问题......
提前致谢!
I have created a custom view which uses an dummy TranslateAnimation to setup some layout properties. I use the Interpolator to calculate height, and apply it to a view inside the applyTransformation() method of the TranslateAnimation.
This is working quite well, if i trigger the animation from my Activity.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i("test", "onCreate()");
view.expand(); // This method starts the animation
}
When I try to do the same using a touch event, nothing happens.
@Override
// This method is touch handler of the View itself
public boolean onTouch(View v, MotionEvent event) {
Log.i("test", "onTouch()");
this.expand(); // onTouch is part of the view itself and calls expand() directly
return true;
}
My expand method looks like this:
public void expand() {
Log.i("test", "Expand!");
TranslateAnimation anim = new TranslateAnimation(0, 0, 0, 0) {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
Log.i("test", "applyTransformation()");
super.applyTransformation(interpolatedTime, t);
// do something
}
};
anim.setDuration(500);
anim.setInterpolator(new AccelerateDecelerateInterpolator());
this.someInternalView.startAnimation(anim);
}
Once my activity is created Logcat shows "onCreate()"
Inside my touch event Logcat shows "onTouch()"
Inside the expand() method Logcat shows "Expand!" - either called from the activity or from an event.
Inside the method applyTransformation() Logcat shows "applyTransformation()" - BUT! only if expand() is called from the onCreate(). Any attempt in trying to start the animation from an event failed.
This looks to me like some sort of threading problem. Could this be? Is there anything I am missing? As far as I see from other posts, starting animations from events should work without any problems...
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
try this: