在Android中为一组对象设置动画

时间:2015-03-24 13:22:53

标签: java android animation translate-animation

我需要使用相同 TextView设置几个Animation个对象。

我这样做的方式看起来不对,因为我只能在每次对象时设置Animation

@Override
    public void onClick(View v) {

    Animation hideLeftBar, showLeftBar;

    hideLeftBar = new TranslateAnimation(195, 0, 0, 0);
    hideLeftBar.setDuration(1000);
    hideLeftBar.setFillAfter(true);

    showLeftBar = new TranslateAnimation(0, 195, 0, 0);
    showLeftBar.setDuration(1000);
    showLeftBar.setFillAfter(true);

    switch(v.getId())
    {
    case R.id.btGMG:

        if(img_GMG.getAlpha() == (float) 0.3)
        {
            img_GMG.setAlpha((float) 1);
            imgLeftBar.startAnimation(showLeftBar);
            txtRS.startAnimation(showLeftBar);
            txtST.startAnimation(showLeftBar);
            txtTR.startAnimation(showLeftBar);
            txtI1.startAnimation(showLeftBar);
            txtI2.startAnimation(showLeftBar);
            txtI3.startAnimation(showLeftBar);
            txtP1.startAnimation(showLeftBar);
            txtP2.startAnimation(showLeftBar);
            txtP3.startAnimation(showLeftBar);
            txtS1.startAnimation(showLeftBar);
            txtS2.startAnimation(showLeftBar);
            txtS3.startAnimation(showLeftBar);
            txtFP1.startAnimation(showLeftBar);
            txtFP2.startAnimation(showLeftBar);
            txtFP3.startAnimation(showLeftBar);
            textIndutive.startAnimation(showLeftBar);
            textCapacitive.startAnimation(showLeftBar);


        }
        else
        {
            img_GMG.setAlpha((float) 0.3);
            imgLeftBar.startAnimation(hideLeftBar);
            txtRS.startAnimation(hideLeftBar);
            txtST.startAnimation(hideLeftBar);
            txtTR.startAnimation(hideLeftBar);
            txtI1.startAnimation(hideLeftBar);
            txtI2.startAnimation(hideLeftBar);
            txtI3.startAnimation(hideLeftBar);
            txtP1.startAnimation(hideLeftBar);
            txtP2.startAnimation(hideLeftBar);
            txtP3.startAnimation(hideLeftBar);
            txtS1.startAnimation(hideLeftBar);
            txtS2.startAnimation(hideLeftBar);
            txtS3.startAnimation(hideLeftBar);
            txtFP1.startAnimation(hideLeftBar);
            txtFP2.startAnimation(hideLeftBar);
            txtFP3.startAnimation(hideLeftBar);
            textIndutive.startAnimation(hideLeftBar);
            textCapacitive.startAnimation(hideLeftBar);
        }


        break;
    }

}

我该如何减少这个?我应该以编程方式或使用XML资源吗?

3 个答案:

答案 0 :(得分:3)

您应该使用AnimatorSet。它有playTogether() function,可以同时播放所有动画。看一个很好的例子here

答案 1 :(得分:1)

它应该以编程方式实现。如果所有元素彼此相同,则可以使用ListView执行此操作。使用ListView,您可以轻松地对元素执行更多操作。 List View | Android Developers

但是如果你想快速更改这段代码,可以使用TextViews列表并轻松使用它:

List<TextView> textViews = null;

public void onCreate(View v) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    textViews.add(findViewById(R.id.txtRS));
    textViews.add(findViewById(R.id.txtST));
    //Initialize other textViews here.
}

public void onClick(View v) {

    for (int i = 0; i < textViews.size(); i++) {
        textViews.get(i).startAnimation(showLeftBar);
    }
}

答案 2 :(得分:1)

为了并行运行多个动画,您可以像这样使用View的ViewPropertyAnimator:

for (int i = 0; i < parentView.getChildCount(); i++) { View childView = parentView.getChildAt(i); childView.animate().translationX(DELTA_X).setDuration(DURATION_ANIM); }

对于其他动画,您可以使用.translationY()