在表布局中按顺序淡入淡出不同 ImageView 上的动画?

发布于 2024-10-26 12:21:26 字数 691 浏览 2 评论 0原文

我有一个 3 行 3 列的表格布局。我总共有 9 个具有设置背景的 ImageView。所有这些图像视图最初都是隐藏的,但使其不可见。我有执行淡入淡出动画的方法。当我在一个图像视图上调用动画函数时,它工作正常。但是,如果我从 for 循环调用动画函数,所有图像视图都会同时进行动画处理。我需要这按顺序发生。

例如 for(int i=0;i<9;i++){ 开始动画(imageViews[i]); }

无效开始动画(){ 在给定的图像视图上淡入淡出 在一张图像上工作正常 注意

:我已经声明了一个 imageView 数组,因此我不需要使用 java 反射来动态访问 imageview。

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}

我已经尝试过 Thread.sleep(xxx) 和处理程序,但仍然无法实现我的目标。基本上,如果我将图像视图位置和图像发送到 startanimation(),它应该在该图像视图位置淡出该图像,并仅在完成整个动画后返回到主要活动。现在似乎好像只有在所有图像视图上完成淡入淡出动画后才会返回。请帮忙。

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 location 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

滥情稳全场 2024-11-02 12:21:26

我在我的游戏中做到了这一点。
首先,您需要将所有按钮存储到地图中:

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

然后,当您创建按钮时,将它们全部添加到地图中:

buttonViews.put(0, myButton);

然后您需要调用递归方法 showButtons(0),如

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...

在 onAnimationEnd 中调用 showButtons(button++)
当所有按钮都用完时,打破这个递归循环。
如果您需要在每个动画之间进一步延迟,则可以使用 postdelay 方法: 在 Android 中处理 onAnimationEnd 内计时的各种方法

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

踏雪无痕 2024-11-02 12:21:26

您是否考虑过使用 myAnim.setStartOffset(buttonIndex*animDuration) ?

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

追风人 2024-11-02 12:21:26

如果您想单独淡化每个图像,则必须为每个图像设置一个计时器。现在,您只有一个计时器。

否则,您将必须保留一张图像表,并在前一个图像完全褪色时淡化每个图像。也就是说,创建一个外循环来跟踪现在正在褪色的图像,并在当前图像全部褪色完成时增加图像指针(即转到下一个图像)。

HTH。听起来很复杂,但实际上并非如此。在循环中,除了当前正在淡出的图像之外,不要触摸任何其他图像。

——皮特

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文