Why does view.startAnimation (animation) not work when called from an event?

I created a custom view that uses the TranslateAnimation dummy to customize some layout properties. I use Interpolator to calculate the height and apply it to the view inside the applyTransformation () method of TranslateAnimation.

This works very well if I run 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 with 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 extension method is as follows:

 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 ()" In my touch event, Logcat shows "onTouch ()" Inside the expand () method, Logcat shows "Expand!" - either called from an action or from an event.

Inside the applyTransformation () method, Logcat shows "applyTransformation ()" - BUT! only if the expand () function is called from onCreate (). Any attempt to try to start the animation from the event failed.

It looks like some kind of threading issue. Could it be? Is there something I can't see? As far as I see from other posts, starting animations from events should work without problems ...

Thanks in advance!

+7
source share
1 answer

try the following:

 public void expand() { Log.i("test", "Expand!"); runOnUiThread(new Runnable() { @Override public void run() { 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); } }); } 
+1
source

All Articles