我创建了一个显示可绘制动画的Imageview,具体取决于用户单击按钮1(第一个动画将显示在Imageview中),然后按钮2(第二个动画将显示在ImageView中)。我的主要问题是重复按钮,因为我希望它以某种方式工作,如果用户单击此重复按钮,ImageView中当前显示/选定的动画将再次显示/动画(用户是否单击了按钮1或按钮2 )。请仔细看看我的重复按钮代码,当我调试它时,我的应用程序将在调用此类时崩溃。非常感谢您的帮助!
这是我没有重复按钮的工作代码:
public class AnimatedImage扩展了Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animatedimage);
//Button1
Button btn1 = (Button) findViewById(R.id.btn1animation);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final ImageView displayimage = (ImageView) findViewById(R.id.imageViewsign1);
displayimage.setBackgroundResource(R.drawable.animation_1);
displayimage.post(new Runnable() {
@Override
public void run() {
AnimationDrawable frameAnimation = (AnimationDrawable) displayimage.getBackground
();
frameAnimation.stop();
frameAnimation.start();
}
});
}
});
//Button 2
Button btn2 = (Button) findViewById(R.id.btn2animation);
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final ImageView displayimage = (ImageView) findViewById(R.id.imageViewsign1);
displayimage.setBackgroundResource(R.drawable.animation_2);
displayimage.post(new Runnable() {
@Override
public void run() {
AnimationDrawable frameAnimation2 = (AnimationDrawable) displayimage.getBackground
();
frameAnimation2.stop();
frameAnimation2.start();
}
});
}
});
}}
这是导致我的应用崩溃的部分:
//SignRepeat
Button repeatBtn = (Button) findViewById(R.id.repeatbtn);
repeatBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final ImageView repImage = (ImageView) findViewById(R.id.imageViewsign1);
if (repImage.equals(R.drawable.animation_1)) {
repImage.setBackgroundResource((R.drawable.animation_1));
repImage.post(new Runnable() {
@Override
public void run() {
AnimationDrawable frameAnimation3 = (AnimationDrawable) repImage.getBackground();
frameAnimation3.stop();
frameAnimation3.start(); }});
}else {
repImage.setBackgroundResource((R.drawable.animation_2));
repImage.post(new Runnable() {
@Override
public void run() {
AnimationDrawable frameAnimation = (AnimationDrawable) repImage.getBackground();
frameAnimation4.stop();
frameAnimation4.start(); }});
}
}
});
更新:
@Override
public void onClick(View v) {
final ImageView repImage = (ImageView) findViewById(R.id.imageViewsign1);
if (repImage.getTag().equals(R.drawable.animation_a)) {
repImage.setBackgroundResource((R.drawable.animation_a));
repImage.post(new Runnable() {
@Override
public void run() {
AnimationDrawable frameAnimation3 = (AnimationDrawable) repImage.getBackground();
frameAnimation3.stop();
frameAnimation3.start(); }});
}else if (repImage.getTag().equals(R.drawable.animation_b)){
repImage.setBackgroundResource((R.drawable.animation_b));
repImage.post(new Runnable() {
@Override
public void run() {
AnimationDrawable frameAnimation4 = (AnimationDrawable) repImage.getBackground();
frameAnimation4.stop();
frameAnimation4.start(); }});
}
else {
repImage.setImageResource(R.drawable.image);
Toast toast = Toast.makeText(getApplicationContext(), "Choose between button1 and button2",
Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.show();
}
答案 0 :(得分:0)
最后一点:
AnimationDrawable frameAnimation = (AnimationDrawable) repImage.getBackground();
frameAnimation4.stop();
frameAnimation4.start();
您可以在此处定义frameAnimation,但启动frameAnimation4。尝试重命名匹配的东西。
更新
在您定义图像视图的位置,请在btn1点击后设置标记后添加2行:
final ImageView displayimage = (ImageView) findViewById(R.id.imageViewsign1);
int tag= R.drawable.drawable_animation1;
displayimage.setTag(tag);
再次为btn2点击:
final ImageView displayimage = (ImageView) findViewById(R.id.imageViewsign1);
int tag= R.drawable.drawable_animation2;
displayimage.setTag(tag);
最后是你的比较线:
if (repImage.getTag().equals(R.drawable.drawable_animation1)) {
答案 1 :(得分:0)
我终于找到了解决问题的方法,我使用setEnabled将重播按钮ID调用到我的button1和button2,但在此之前我取消选中按钮的启用属性。这些是我添加的行。
Button replay = (Button)findViewById(R.id.button_reply);
replay.setEnabled(true);
或者您也可以使用visibility属性。 希望这有助于万一有人进入相同的场景。 :)