开发者

Fade in Fade out Animation on different ImageViews with in a table layout sequentially?

I have a table layout with 3 rows and 3 columns. In total I have 9 ImageViews with set backgrounds. All these imageviews are initially hidden but making it invisible. I have method for performing a fade-in fade-out animation. It is working fine when I call my animation function on one imageview. However if I call the animation function from a for loop, all the imageviews are being animated simultaneously. I need this to happen sequentially.

E.g. for(int i=0;i<9;i++){ startanimation(imageViews[i]); }

void startanimation(){ Do fade in fade out on the given imageview working fine on one image }

NOTE: I have declared an array of imageViews so I need not use java reflection to access imageviews dynamically.

int[] imageViews = {R.id.ImageView01, R.id.ImageView02, R.id.ImageView03, R.id.ImageView04, R.id.ImageView05, R.id.ImageView06, R.id.ImageView07, R.id.ImageView08, R.id.ImageView09}

I have tried Thread.sleep(xxx) and also handlers but still not able to achieve my objective. Basically if I send the imageview position and an image to the startanimation(), it should fadein fade out that image at that imageview loc开发者_如何转开发ation and return to the main activity ONLY after completing the whole animation. Right now it seems like it is returning only after completing fadein fadeout animation on all the iamgeviews. Please help.


I have done this in my game. First you need to store all buttons into a map:

private Map<Integer, Button> buttonViews = new HashMap<Integer, Button>();

Then when you create buttons add them all into the map:

buttonViews.put(0, myButton);

Then you need to call a recursive method showButtons(0) like

showButtons(int button){

//set button visible
buttonViews.get(button).setVisibility(View.VISIBLE);
//set button animation
 Animation myAnim = AnimationUtils.loadAnimation(MyActivity.this, R.anim.fadeIn);
 buttonViews.get(button).startAnimation(myAnim);

//then use onAnimationEnd
myAnim.setAnimationListener(new AnimationListener() {
            public void onAnimationEnd(Animation animation...

In onAnimationEnd call showButtons(button++) When all buttons are exhausted break this recursive loop. If you need to make a further delay between each animation then you can use postdelay method: Various ways to handle timing in Android inside of onAnimationEnd


Have you consider using myAnim.setStartOffset(buttonIndex*animDuration) ?


If you want to fade each image individually, then you will have to have a timer for each of the images. Right now, you have only one timer.

Or else you will have to keep a table of the images and fade each one when the previous image is completely faded. That is, make an outer loop to keep track of which image is being faded now and increment the image pointer -- that is, go to the next image -- when the current image is all done fading.

HTH. It sounds complicated, but really it's not. In the loop, just don't touch any other image than the one you are currently fading.

-- pete

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜