Reputation: 33
I wanted to make it with TransitionDrawable class, but it needs a separate file transition.xml. There I define which image I change to.
I need to define them in Java code because I don't know which images I will change too. I have many images and I accidentally get only two images which will change between each other. What can I do? Perhaps I need another class.
Code with transition.xml:
public class TransitionActivity extends Activity
implements OnClickListener {
private ImageView image;
private TransitionDrawable mTransition;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image = (ImageView)findViewById(R.id.image);
image.setOnClickListener(this);
Resources res = this.getResources();
mTransition = (TransitionDrawable)res.getDrawable(R.drawable.transition);
}
@Override
public void onClick(View v) {
image.setImageDrawable(mTransition);
mTransition.startTransition(1000);
}
}
Upvotes: 1
Views: 1830
Reputation: 3827
You can programmatically create a TransitionDrawable
using the class' constructor. You don't need to acquire it from XML. This gives you the flexibility of dynamically assigning the Drawables it transitions between.
// drawable1 and drawable2 can be dynamically assigned in your Java code
Drawables[] drawables = new Drawables[] {
drawable1, drawable2
};
mTransition = new TransitionDrawable(drawables);
Upvotes: 1