25

そのため、すべて同じ回転仕様を使用して、少数のビューをすべて同時に回転させたいと思います。問題は、何らかの理由で、2番目の要素の回転の動作が異なることです。どうやらこれは、アニメーションオブジェクトがこれらの2行のコード間で実際に状態を変更することに関係しているようです。もちろん、別のアニメーションオブジェクトを作成して適用することもできますが、もっと簡単な方法があるように感じます(約15ビューあります)

最初のビューのみを正しく回転します。

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

両方を正しく回転させます

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

誰かアイデアはありますか?

4

3 に答える 3

11

このようにしてください:

ObjectAnimator anim = ObjectAnimator.ofFloat(view, "y", 100f);
arrayListObjectAnimators.add(anim);

ObjectAnimator anim1 = ObjectAnimator.ofFloat(view, "x", 0f);
arrayListObjectAnimators.add(anim1);

ObjectAnimator[] objectAnimators = arrayListObjectAnimators.toArray(new ObjectAnimator[arrayListObjectAnimators.size()]);
AnimatorSet animSetXY = new AnimatorSet();
animSetXY.playTogether(objectAnimators);
animSetXY.duration(1000);
animSetXY.start();
于 2013-02-12T21:55:08.017 に答える
5

だから、これは不可能だと思うので、同じアニメーションをビューのリストに適用するヘルパーメソッドを作成しました。

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);
    }
}

間違いなくハックですが、今できることはそれだけだと思います

于 2013-02-12T21:48:02.967 に答える
0

プログラムで1つのAnimatorSetを作成することで、Kotlinでこれを行うことができました。
1.アニメーション化するビューのArrayListを作成します

 var viewList = arrayListOf(view1,view2,view3)

2. ArrayListをループして、成長するAnimatorSetを作成します

var ix = 0
var anim = AnimatorSet()
var viewList = arrayListOf(view1,view2,view3)
        viewList.forEach{
        // Initiate the animator set with one ObjectAnimator
            if(ix == 0){
                anim = AnimatorSet().apply {
                    play(ObjectAnimator.ofFloat(it, "rotation", 0F, 360F))
                }
            }
        // Add one ObjectAnimator at a time to the growing AnimatorSet
            else{
                var currentAnim = ObjectAnimator.ofFloat(it,"rotation",0F,360F)
                anim = AnimatorSet().apply {
                    play(anim).with(currentAnim)
                }
            }
            ix++
        }

3.アニメーションを開始します

button.setOnClickListener {
            AnimatorSet().apply {
                play(anim)
                start()
            }
于 2020-05-29T03:55:18.050 に答える