0

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

in onCreate() I have this

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

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();
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Swarnveer
  • 490
  • 5
  • 23

1 Answers1

1

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

AnimationDrawable myFrameAnimation;

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

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

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

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

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" />

Swarnveer
  • 490
  • 5
  • 23
  • 1
    Just a note, you don't need `editor.commit();` for each thing you put into the SharedPreferences. You can just do it once after the last value is inserted. – OneCricketeer Mar 04 '16 at 19:43
  • Ok thanks for the update, I am quite new to android and think it should be required. Thanks for the clarification – Swarnveer Mar 04 '16 at 20:02
  • No, not required. From the documentation "All changes you make in an editor are batched, and not copied back to the original SharedPreferences until you call commit()", so since you are not doing anything between changes, you only need to call after the last put. – OneCricketeer Mar 04 '16 at 20:05