Android 多个动画打开按钮,仅显示最后一个
我有一个动画,我想在3个不同的按钮上实现,但由于某种原因,当我按下它们时,只有最后按下的按钮启动动画,其他两个按钮上的动画停止工作。我在stackoverflow上看到了一个答案,但我不明白。我还注意到,如果我复制动画代码两次,并将其命名为animation2和animation3,然后将其添加到按钮中,那么它就会工作,但我假设这是非常内存不足的。这是我的密码:Android 多个动画打开按钮,仅显示最后一个,android,Android,我有一个动画,我想在3个不同的按钮上实现,但由于某种原因,当我按下它们时,只有最后按下的按钮启动动画,其他两个按钮上的动画停止工作。我在stackoverflow上看到了一个答案,但我不明白。我还注意到,如果我复制动画代码两次,并将其命名为animation2和animation3,然后将其添加到按钮中,那么它就会工作,但我假设这是非常内存不足的。这是我的密码: public class MainActivity extends ActionBarActivity { @Overrid
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button a1 = (Button) findViewById(R.id.a1);
final Button a2 = (Button) findViewById(R.id.a2);
final Button a3 = (Button) findViewById(R.id.a3);
final AnimationDrawable animation = new AnimationDrawable();
animation.addFrame(getResources().getDrawable(R.drawable.a2), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a3), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a4), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a5), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a6), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a7), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a8), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a9), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a10), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a11), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a12), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a13), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a14), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a15), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a16), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a17), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a18), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a19), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a20), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a21), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a22), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a23), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a24), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a25), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a26), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a27), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a28), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a29), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a30), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a31), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a32), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a33), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a34), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a35), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a36), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a37), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a38), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a39), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a40), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a41), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a42), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a43), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a44), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a45), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a46), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a47), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a48), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a49), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a50), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a51), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a52), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a53), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a54), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a55), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a56), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a57), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a58), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a59), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a60), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a61), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a62), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a63), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a64), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a65), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a66), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a67), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a68), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a69), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a70), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a71), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a72), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a73), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a74), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a75), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a76), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a77), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a78), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a79), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a80), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a81), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a82), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a83), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a84), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a85), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a86), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a87), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a88), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a89), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a90), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a91), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a92), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a93), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a94), 20);
animation.addFrame(getResources().getDrawable(R.drawable.a95), 20);
animation.setOneShot(false);
a1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
a1.setBackgroundDrawable(animation);
animation.start();
break;
case MotionEvent.ACTION_UP:
animation.stop();
//a1.setBackgroundResource(R.drawable.index1);
// End
break;
}
return false;
}
});
a2.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
a2.setBackgroundDrawable(animation);
animation.start();
break;
case MotionEvent.ACTION_UP:
animation.stop();
//a1.setBackgroundResource(R.drawable.index1);
// End
break;
}
return false;
}
});
a3.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
a3.setBackgroundDrawable(animation);
animation.start();
break;
case MotionEvent.ACTION_UP:
animation.stop();
//a1.setBackgroundResource(R.drawable.index1);
// End
break;
}
return false;
}
});
}
我终于明白了!以下是需要放入可绘制文件夹代码中的xml代码:
<?xml version="1.0" encoding="utf-8"?>
<animation-list android:id="@+id/custom_button" android:oneshot="false"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/a1" android:duration="20" />
<item android:drawable="@drawable/a2" android:duration="20" />
<item android:drawable="@drawable/a3" android:duration="20" />
<item android:drawable="@drawable/a4" android:duration="20" />
<item android:drawable="@drawable/a5" android:duration="20" />
<item android:drawable="@drawable/a6" android:duration="20" />
<item android:drawable="@drawable/a7" android:duration="20" />
<item android:drawable="@drawable/a8" android:duration="20" />
<item android:drawable="@drawable/a9" android:duration="20" />
<item android:drawable="@drawable/a10" android:duration="20" />
</animation-list>
}为每个按钮定义三个AnimationDrawable对象。是的,三个AnimationDrawable将是一种方式。是的,我发现我可以这样做,但这不是很低效吗?如果我没有3个而是30个按钮,它会不会大大降低手机的速度?难道没有更好的方法吗?为什么不在res/drawable/目录中将动画定义为XML。这样,你就可以在单独的AnimationDrawables中重复使用这个定义。我真的不知道该怎么做
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button a1 = (Button) findViewById(R.id.a1);
final Button a2 = (Button) findViewById(R.id.a2);
final Button a3 = (Button) findViewById(R.id.a3);
a1.setBackgroundResource(R.drawable.custom_button);
a2.setBackgroundResource(R.drawable.custom_button);
a3.setBackgroundResource(R.drawable.custom_button);
final AnimationDrawable anim = (AnimationDrawable) a1.getBackground();
final AnimationDrawable anim2 = (AnimationDrawable) a2.getBackground();
final AnimationDrawable anim3 = (AnimationDrawable) a3.getBackground();
a1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
anim.start();
break;
case MotionEvent.ACTION_UP:
anim.stop();
// End
break;
}
return false;
}
});
a2.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
anim2.start();
break;
case MotionEvent.ACTION_UP:
anim2.stop();
// End
break;
}
return false;
}
});
a3.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
anim3.start();
break;
case MotionEvent.ACTION_UP:
anim3.stop();
// End
break;
}
return false;
}
});
}