0

Как переключаться между анимированным GIF-изображением и статическим в src ImageButton, когда кто-то нажимает на него?Использование gif в ImageButton

в onCreate() я это

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

Следующая запускается на выполнение, когда кто-то нажимает 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(); 
    } 
} 
+0

Это не работает ?? – Jas

+0

Нет его отображения статического изображения при нажатии – Swarnveer

+0

Не знаю, будет ли это работать, но вы можете проверить это: http://stackoverflow.com/a/9494859/5985522 – sander338

ответ

0

Я придумал, как и она работает абсолютно нормально. Сначала я определил глобальную переменную

AnimationDrawable myFrameAnimation; 

Тогда в OnCreate() я определил кнопку с переменной.

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

Затем создайте для этой кнопки метод onClickListener().

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

Здесь «транс» чисто прозрачное изображение так, что мое предыдущее изображение до щелчка не отображается после щелчка.

В «frame_animation» я определил все кадры изображения, чтобы получить идеальный анимированное изображение

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

+0

Просто заметьте, вам не нужно 'editor.commit();' для каждой вещи, которую вы помещаете в SharedPreferences. Вы можете просто сделать это один раз после последнего значения. –

+0

Хорошо спасибо за обновление, я совершенно новый для android и думаю, что это необходимо. Спасибо за разъяснение – Swarnveer

+0

Нет, не требуется. Из документации «Все изменения, внесенные в редактор, выгружаются и не копируются обратно в исходные SharedPreferences до тех пор, пока вы не назовете commit()», поэтому, поскольку вы ничего не делаете между изменениями, вам нужно только позвонить после последнего put. –

Смежные вопросы