我创建了一个自定义视图,它使用一个虚拟的 TranslateAnimation 来设置一些布局属性。我使用插值器计算高度,并将其应用于 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() 时。尝试从事件启动动画的任何尝试都失败了。
这在我看来像是某种线程问题。这可能吗?有什么我想念的吗?据我从其他帖子中看到,从事件开始动画应该没有任何问题......
提前致谢!