【问题标题】:How to replay a currently displayed drawable animation in an Imageview如何在 Imageview 中重放当前显示的可绘制动画
【发布时间】:2014-09-19 14:29:41
【问题描述】:

我创建了一个显示可绘制动画的 Imageview,这取决于用户是否单击按钮 1(第一个动画将显示在 Imageview 中),然后是按钮 2(第二个动画将显示在 ImageView 中)。我的主要问题是重复按钮,因为我希望它以一种方式工作,如果用户单击此重复按钮,则 ImageView 中当前显示/选择的动画将再次显示/动画(无论用户单击按钮 1 还是按钮 2 )。请看一下我的重复按钮代码,当我调试它时,调用这个类时我的应用程序会崩溃。非常感谢您的帮助!

这是我没有重复按钮的工作代码:

公共类 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();
            }

【问题讨论】:

  • logcat 中的错误是什么?
  • 第一行说:FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{}: java.lang.NullPointerException

标签: android animation imageview


【解决方案1】:

最后一点:

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)) {

【讨论】:

  • 好眼光,我已经重命名了它,但它仍然崩溃?
  • logcat 有新的错误吗?我使用您的代码制作了一个小应用程序,运行时没有问题。
  • 我在 logcat 中仍然遇到同样的错误:Caused by: java.lang.NullPointerException at java:94) 我可以看看你在哪里附加了重复按钮吗?
  • 嗨惠特尼,我尝试重新制作 .class 并且它不再崩溃,但是当我单击重复按钮时,它只满足我的 else 条件并且它只调用动画2,它无法重播当我单击按钮 1 时的动画 1。我不确定它可能会忽略我的第一个条件
  • 这一行:repImage.equals(R.drawable.animation_1);它将图像视图与整数进行比较。我进行了研究,看起来您无法从图像视图中获取资源 ID。但是,您可以设置一个标签,然后稍后获取该标签。请参阅上面我的答案中的更新。它适用于我的测试代码。
【解决方案2】:

我终于找到了解决它的方法,我使用 setEnabled 将重播按钮 id 调用到我的 button1 和 button2,但在此之前我取消选中了按钮的启用属性。这些是我添加的行。

 Button replay = (Button)findViewById(R.id.button_reply);
 replay.setEnabled(true);

或者,您也可以使用可见性属性。 希望这有助于万一有人遇到同样的情况。 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多