ImageButton中gif的使用 [英] Usage of gif in ImageButton

查看:26
本文介绍了ImageButton中gif的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当有人点击 ImageButton 的 src 时,如何在动画 GIF 图像和静态图像之间切换?

How to toggle between an animated GIF image and a static one in ImageButton's src when someone clicks it?

onCreate() 我有这个

aButton3 = (ImageButton) findViewById(R.id.imageButton3);
SharedPreferences sharedPreferences = getSharedPreferences("NAME", Context.MODE_PRIVATE);
Boolean e = sharedPreferences.getBoolean("clicked3", false);

当有人单击 ImageButton 时执行以下操作

The following gets executed when someone clicks the ImageButton

public void buttonClick2(View v) {
    SharedPreferences sharedPreferences = getSharedPreferences("NAME", Context.MODE_PRIVATE);
    Boolean d = sharedPreferences.getBoolean("clicked2", false);
    if (!d) {
        toggleSound.start();
        aButton2.setImageResource(R.drawable.on);
        sharedPreferences = getSharedPreferences("NAME", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("clicked2", true);
        editor.commit();
    }
    if(d){
        toggleSound.start();
        aButton2.setImageResource(R.drawable.off);
        sharedPreferences = getSharedPreferences("NAME", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor = sharedPreferences.edit();
        editor.putBoolean("clicked2", false);
        editor.commit();
    }
}

推荐答案

我想出了一个方法,而且效果非常好.首先我定义了全局变量

I figured out a way and it was working absolutely fine. First I defined global variable

AnimationDrawable myFrameAnimation;

然后在 onCreate() 中,我用一个变量定义了按钮.

Then in onCreate() i defined the button with a variable.

aButton3 = (ImageButton) findViewById(R.id.imageButton3);

然后为该按钮创建一个 onClickListener() 方法.

Then create a onClickListener() method for that button.

aButton3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SharedPreferences sharedPreferences = getSharedPreferences("NAME", Context.MODE_PRIVATE);
            Boolean e = sharedPreferences.getBoolean("clicked3", false);
            if (!e) {
                toggleSound.start();
                aButton3.setImageResource(R.drawable.trans);
                aButton3.setBackgroundResource(R.drawable.frame_animation);
                myFrameAnimation=(AnimationDrawable) aButton3.getBackground();
                myFrameAnimation.start();
                aButton4.setImageResource(R.drawable.reg1);
                def=1;
                count=1;
                sharedPreferences = getSharedPreferences("NAME", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putBoolean("clicked3", true);
                editor.commit();
                editor.putInt("clicked5", def);
                editor.commit();
                editor.putInt("clicked4", count);
                editor.commit();
            }
            if(e){
                toggleSound.start();

                aButton3.setBackgroundResource(R.drawable.frame_animation2);
                myFrameAnimation=(AnimationDrawable) aButton3.getBackground();
                myFrameAnimation.start();
                aButton3.setImageResource(R.drawable.newoff);
                aButton4.setImageResource(R.drawable.reg0);
                count=0;
                def=0;
                sharedPreferences = getSharedPreferences("NAME", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor = sharedPreferences.edit();
                editor.putBoolean("clicked3", false);
                editor.commit();
                editor.putInt("clicked4", count);
                editor.commit();
                editor.putInt("clicked6", def);
                editor.commit();
            }
        }
    });

这里的trans"是一个纯透明的图片,所以我点击前的图片在点击后不会显示.

Here "trans" is a purely transparent image so that my previous image before click does not show after click.

在frame_animation"中我定义了图像的所有帧以获得完美的动画图像

In "frame_animation" I defined all the frames of the image to get a perfect animated image

<animation-list android:oneshot="false" xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/pic1" android:duration="5" />
<item android:drawable="@drawable/pic2" android:duration="5" />
<item android:drawable="@drawable/pic3" android:duration="5" />
<item android:drawable="@drawable/pic4" android:duration="5" />
<item android:drawable="@drawable/pic5" android:duration="5" />
<item android:drawable="@drawable/pic6" android:duration="5" />
<item android:drawable="@drawable/pic7" android:duration="5" />
<item android:drawable="@drawable/pic8" android:duration="5" />
<item android:drawable="@drawable/pic9" android:duration="5" />

这篇关于ImageButton中gif的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆