为 Android 中的一组对象设置动画
Set Animation for a group of objects in Android
我有几个 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
资源?
它应该以编程方式实现。如果所有元素彼此显示相同,则可以使用 ListView 执行此操作。使用 ListView,您可以轻松地对元素执行更多操作。
List View | Android Developers
但是如果您想快速更改此代码,您可以使用 List of 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);
}
}
为了 运行 多个并行动画,您可以像这样使用 View 的 ViewPropertyAnimator:
for (int i = 0; i < parentView.getChildCount(); i++) {
View childView = parentView.getChildAt(i);
childView.animate().translationX(DELTA_X).setDuration(DURATION_ANIM);
}
对于其他动画,您可以使用 .translationY()
你应该使用 AnimatorSet. It has a playTogether()
function that will play all your animations at the same time. See a good example here.
我有几个 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
资源?
它应该以编程方式实现。如果所有元素彼此显示相同,则可以使用 ListView 执行此操作。使用 ListView,您可以轻松地对元素执行更多操作。 List View | Android Developers
但是如果您想快速更改此代码,您可以使用 List of 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);
}
}
为了 运行 多个并行动画,您可以像这样使用 View 的 ViewPropertyAnimator:
for (int i = 0; i < parentView.getChildCount(); i++) {
View childView = parentView.getChildAt(i);
childView.animate().translationX(DELTA_X).setDuration(DURATION_ANIM);
}
对于其他动画,您可以使用 .translationY()
你应该使用 AnimatorSet. It has a playTogether()
function that will play all your animations at the same time. See a good example here.