Search
Saturday 25 November 2017
  • :
  • :

How To Implement TransitionDrawable in Android? – Part 2

In last part we show how to implement Transition Drawable? Now let us discuss Continuous TransitionDrawable here as a second Part.

Continuous Transition Drawable

Let me recall a bit about it, Transition Drawable s name itself explains that it will transit between different images. Transition Drawable is actually an extension of LayerDrawable, so rather to say it will  transit between different images, we can say it will transit between layers.

Transition Drawable supports transition between only two layers.

So using the same sample code we will create an animation or rather say continuous Transition Drawable until you want.

For creating sample code you can check previous post about Transition Drawable.

For making it continuous we will use Timer, and schedule a TimerTask which runs continuously with some specified delay.

Android App Development

Create a call named ContinuousTransitionDrawableTimerTask extends TimerTask inside MainActivity and a Timer instance to schedule this TimerTask.

import java.util.Timer;
import java.util.TimerTask;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.drawable.TransitionDrawable;
import android.os.Build;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class TransitionActivity extends Activity {

                LinearLayout rootLayout;

                ImageView mImgView;

                Timer mContinuousTransitionDrawableTimer;

                TransitionDrawable transition;

                boolean isForward = true;

                @SuppressWarnings("deprecation")
                @SuppressLint("NewApi")
                @Override
                protected void onCreate(Bundle savedInstanceState) {
                                super.onCreate(savedInstanceState);
                                setContentView(R.layout.transition_activity);

                                mContinuousTransitionDrawableTimer = new Timer();

                                mImgView = (ImageView)findViewById(R.id.transition_img);

                                Resources res = getApplicationContext().getResources();

                                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
                                                transition = (TransitionDrawable) res.getDrawable(R.drawable.translate, null);
                                }else{
                                                transition = (TransitionDrawable) res.getDrawable(R.drawable.translate);
                                }
                                mImgView.setImageDrawable(transition);
                                //TransitionDrawable transition = (TransitionDrawable) rootLayout.getBackground();
                                transition.setCrossFadeEnabled(true);

                                ContinuousTransitionDrawableTimerTask mContinuousTransitionDrawableTimerTask = new ContinuousTransitionDrawableTimerTask();

                                mContinuousTransitionDrawableTimer.schedule(mContinuousTransitionDrawableTimerTask, 0, 5000);

                }

                public class ContinuousTransitionDrawableTimerTask extends TimerTask{

                                @Override
                                public void run() {
                                                runOnUiThread(new Runnable() {

                                                                @Override
                                                                public void run() {
                                                                                if (isForward){
                                                                                                isForward = false;
                                                                                                transition.startTransition(5000);
                                                                                }else{
                                                                                                isForward = true;
                                                                                                transition.reverseTransition(5000);
                                                                                }
                                                                }
                                                });
                                }

                }

}

We created one flag variable to identify forward or reverse transition. Inside ContinuousTransitionDrawableTimerTask run method we implemented if condition to identify forward or reverse case and called respective startTransition() and reverseTrasition() methods.

Inside onCreate() we initialized our mContinuousTransitionDrawableTimer timer and scheduled ContinuousTransitionDrawableTimerTask with 0 millesec delay to run it immediately and repeat as 5000 which you can increase if you want to have some delay between the cycle.

Run the code and see the stars fading.

To stop call cancel() and purge() on timer object.

This Android tutorial is all about to implement Continuous TransitionDrawable. All coding examples in these both tutorials (Part-1 & Part-2) are developed and tested by expert android application developers at Nex Mobility.

Happy Coding…



Vijay is a compulsive blogger who likes to educate like-minded people on various new technologies and trends. He works with Aegis SoftTech as a software developer and has been developing software for years. Stay Connected to him on Facebook and Google+.