我想在3个不同的按钮上实现一个动画,但出于某种原因,当我按下它们时,只有按下的最后一个按钮才会启动动画,其他两个按钮上的动画将停止运行。我在stackoverflow上看到了一个答案,但我不明白。我还注意到,如果我复制动画代码两次并将其命名为animation2和animation 3,并将其添加到按钮然后它可以正常工作,但我认为它的内存效率非常低。继承我的代码:
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;
}
});
}
答案 0 :(得分:0)
最后我想出来了!这是您需要在可绘制文件夹代码中输入的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>
这是剩下的
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;
}
});
}
}