TransitionDrawable: automatically transition back after completion

I want to add new ListView items with a nice effect. I thought it was simple and convenient, but I messed up the problem:

I want to play the TransitionDrawable animation and rewind it after it is complete. A new item will be drawn for a moment, and then it will mix with the rest.

TransitionDrawable has methods for playing back and forth animations, but none of them can be used for synchronization. I was expecting the opportunity to specify a callback to complete the animation, for example:

TransitionDrawable transition = (TransitionDrawable) view.getBackground(); transition.startTransition(500, new TransitionCompleteListener(){ public void completed() { transition.reverseTransition(500); } }); 

But nothing of the kind is supported by the TransitionDrawable class.

The problem is this: how to play TransitionDrawable animation, and when it ends - immediately play it back? I had the idea of ​​using the Timer class to delay the execution of the back of the animation, but this solution seems too heavy for such a simple thing.

Or maybe I should use something else that is TransitionDrawable? I would like to avoid using Property Animations since I want to support older devices (and PAs are avaialble with Honeycomb).

+8
android android-ui
source share
5 answers

I have not tried it myself yet, but I would say that what you are striving for is also possible with the help of the old animation View, tween , to be precise. Instead of directly manipulating the background that you can use (provided that you are doing it right now), you can create the same effect by changing the alpha property of the view, which is exactly the same size as the line layout. You can then specify both the β€œnormal” animation and its feedback in one AnimationSet and start with a delay based on the first. Alternatively, Animation provides an AnimationListener with which you can connect and be notified when the first animation finishes, and then start the opposite.

Having looked at the source code of TransitionDrawable, I also see some possibilities for expanding the current implementation using the listener user interface. It should not be too difficult to actually implement the template that you will illustrate in the code snippet in your question.

Now I have a little short time, so give me a comment if you need more specific pointers (like code snippets). Then I will try to find some time this weekend to help you.

+4
source share

Perhaps you could try the following:

 Drawable[] transBack = new Drawable[] { res.getDrawable( R.drawable.ic_acept_blue ), res.getDrawable( R.drawable.ic_acept )}; ImageView btn = (ImageView) findViewById(R.id.resource); TransitionDrawable transitionAcept = new TransitionDrawable(transBack); btn.setImageDrawable(transitionAcept); btn.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub switch (event.getAction()) { case MotionEvent.ACTION_DOWN:{ TransitionDrawable transition = (TransitionDrawable) btn.getDrawable(); transition.startTransition(75); }break; case MotionEvent.ACTION_UP:{ TransitionDrawable transition = (TransitionDrawable) btnAcept.getDrawable(); transition.reverseTransition(75); v.performClick(); }break; default: break; } return true; } }); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //... DO Something here } }); 

The code above implements the TouchListener function to handle the reverse and forward transitions and the clickListener to handle your actions. This is why in MotionEvent.ACTION_UP you call v.performClick(); Hope this help is for you.

0
source share

I expanded the TransitionDrawable implementation (I actually copied and modified it) to accept the repeatCount parameter, which indicates how many times you need to go back and forth. See RepeatableTransitionDrawable in the repository below.

I also plan to add more than two supported features.

https://github.com/mehmet6parmak/CustomDrawables

0
source share

Try with a handler;)

 final TransitionDrawable transition = (TransitionDrawable) sizesSelectionLayout.getBackground(); int duration = 500; transition.startTransition(duration); new Handler().postDelayed(new Runnable() { @Override public void run() { transition.reverseTransition(duration); } }, duration); 

;)

0
source share

Rate using ViewAnimator instead of http://developer.android.com/reference/android/widget/ViewAnimator.html

In the animation object in and out in the ViewAnimator application, there is an AnimationListener that fires the event you want to capture

-2
source share

All Articles