I have different images, say 100 images or so. Now, I want to apply animation on them. I want my ImageView to get each image after specified interval but when the change of image occurs, each image should FadeIn or FadeOut. I place my images in @drawable/[list_of_images].xml file as:
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/a1" android:duration="1000"/>
<item android:drawabl开发者_如何学编程e="@drawable/a2" android:duration="2000"/>
<item android:drawable="@drawable/a3" android:duration="3000"/>
<item android:drawable="@drawable/a4" android:duration="500"/>
and then I can successfully change these images depending on their time interval in ImageView by using:
public class AnimTest extends Activity
{
AnimationDrawable myAnim;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.anim);
ImageView myIV = (ImageView) findViewById(R.id.image_view);
myIV.setBackgroundResource(R.drawable.list_of_images.xml);
myAnim = (AnimationDrawable) myIV.getBackground();
}
public boolean onTouchEvent(MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
myAnim.start();
return true;
}
return super.onTouchEvent(event);
}
}
The problem is that I can't figure out on how to apply fade effect on every single image, while they are getting changed by the above animation. I can apply fade animation on whole list of images but cannot do this on every image. Am i going to the right direction to achieve this functionality? If not, please guide me to the right path.
Regards, Khawar
You could try setting a repeat count on your Animation to imagecount-1, then adding an AnimationListener to your animation that changes the background image of the ImageView on every repeat and on start.
Here's a simple example that uses RoboGuice (which makes the code cleaner but doesn't make any difference for the purposes of your question): https://github.com/bostonandroid/batgirl/blob/master/src/org/roboguice/batgirl/Batgirl.java
public class Batgirl extends RoboActivity {
// Views
@InjectView(R.id.content) LinearLayout linearLayout;
// Resources
@InjectResource(R.anim.spin) Animation spin;
@InjectResource(R.integer.max_punches) int MAX_PUNCHES;
// Other Injections
@Inject ChangeTextAnimationListener changeTextAnimationListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Set up the animation
linearLayout.setAnimation(spin);
spin.setAnimationListener(changeTextAnimationListener);
spin.setRepeatCount(MAX_PUNCHES - 1);
spin.start();
}
}
/**
* A simple animation listener that swaps out the text between repeats.
*/
class ChangeTextAnimationListener implements AnimationListener {
@InjectView(R.id.hello) TextView helloView;
@Inject Fist fist;
@Inject PackageInfo info;
public void onAnimationRepeat(Animation animation) {
onAnimationStart(animation);
}
public void onAnimationStart(Animation animation) {
helloView.setText( getNextTextString() ); // getNextTextString() not shown in this example
}
public void onAnimationEnd(Animation animation) {
}
}
精彩评论