gpt4 book ai didi

java - 为Android中的一组对象设置动画

转载 作者:太空狗 更新时间:2023-10-29 16:35:47 26 4
gpt4 key购买 nike

我有几个 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资源?

最佳答案

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

关于java - 为Android中的一组对象设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29233807/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com