将一个动画多个视图的同时多个、视图、动画

2023-09-05 05:50:11 作者:许卿白头上上签

所以,我还想给所有的同时旋转少数意见,都使用相同的旋转功能。的问题是,由于某种原因,旋转作用不同的第二元件。显然,这必须与动画对象实际上改变这两条线中的code之间的状态。很显然,我可以只创建一个单独的动画对象,并应用它,但我觉得还有一个更简单的方法(我有大约15次)

So Id like to rotate a handful of views all at the same time, all using the same rotation specs. The issue is that for some reason the rotation acts differently for the second element. Apparently this has to do with the animation object actually changing state in between those two lines of code. Obviously I could just create a seperate Animation object and apply it, but I feel like there is an easier way (I have about 15 views)

正确旋转只有第一种观点:

Rotates only the first view correctly:

Animation rotateAnim = AnimationUtils.loadAnimation(this, R.anim.rotationtoportrait);
target.startAnimation(rotateAnim);
lightBtn.startAnimation(rotateAnim);

旋转两个正确

Rotates both correctly

Animation rotateAnim = AnimationUtils.loadAnimation(this, R.anim.rotationtoportrait);
Animation rotateAnim2 = AnimationUtils.loadAnimation(this, R.anim.rotationtoportrait);
target.startAnimation(rotateAnim);
lightBtn.startAnimation(rotateAnim2);

XML:

<?xml version="1.0" encoding="utf-8"?>
<rotate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="-90"
    android:toDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration="500" android:fillAfter="true">

任何人有什么想法?

Anyone have any ideas?

推荐答案

所以我想这恰恰是不可能的,所以我创建了一个辅助方法,只适用相同的动画视图列表:

So I guess this just isn't possible, so I created a helper method to just apply the same animation to a list of views:

public void doRotations(ArrayList<View> views, int start, int end, int xprop, float xscale, int yprop, float yscale, int duration, Boolean fillAfter){

    for(int i = 0; i < views.size(); i++){
        RotateAnimation temp = new RotateAnimation(start, end, xprop, xscale, yprop, yscale);
        temp.setDuration(duration);
        temp.setFillAfter(fillAfter);
        views.get(i).startAnimation(temp);
    }
}

绝对是一个黑客,但我想这就是所有我能够做的事情

Definitely a hack, but I guess thats all I'm able to do right now