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

Java 在Android中为一组对象设置动画,java,android,animation,translate-animation,Java,Android,Animation,Translate Animation,我有几个TextView对象,我需要用相同的动画来设置动画 我这样做的方式看起来是错误的,因为我每次只能为每个对象设置动画 @Override public void onClick(View v) { Animation hideLeftBar, showLeftBar; hideLeftBar = new TranslateAnimation(195, 0, 0, 0); hideLeftBar.setDuration(1000); hideLef

我有几个
TextView
对象,我需要用相同的
动画来设置动画

我这样做的方式看起来是错误的,因为我每次只能为每个对象设置
动画

@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
资源来实现它?

它应该通过编程实现。如果所有元素彼此显示相同,则可以使用ListView执行此操作。使用ListView,您可以轻松地对元素进行更多操作。

但如果您想快速更改此代码,可以使用文本视图列表并轻松使用:

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);
    }
}
列表文本视图=null;
创建公共void(视图v){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
add(findviewbyd(R.id.txtRS));
add(findviewbyd(R.id.txtST));
//在此处初始化其他文本视图。
}
公共void onClick(视图v){
对于(int i=0;i
为了并行运行多个动画,您可以使用视图的ViewPropertyAnimator,如下所示:

for(int i=0;i


对于其他可以使用的动画。translationY()

应该使用一个。它有一个
playthouse()
,可以同时播放所有动画。请参阅一个很好的示例。

使用
列表视图
不是一个合适的解决方案,因为某些
文本视图
上显示的某些值会一直更改。如果在列表视图中,请使用保持器并调用notifyDataSetChanged();您可以动态更改视图。