在ImageButton的GIF的使用 [英] Usage of gif in ImageButton
问题描述
如何动画GIF图像和静态的之间的ImageButton的src当有人点击其切换?
在的onCreate()
我有这种
aButton3 =(的ImageButton)findViewById(R.id.imageButton3);
共享preferences共享preferences = getShared preferences(NAME,Context.MODE_PRIVATE);
布尔E =共享preferences.getBoolean(clicked3,FALSE);
当有人点击的ImageButton下被执行
公共无效buttonClick2(视图v){
共享preferences共享preferences = getShared preferences(NAME,Context.MODE_PRIVATE);
布尔D =共享preferences.getBoolean(clicked2,FALSE);
如果(!D){
toggleSound.start();
aButton2.setImageResource(R.drawable.on);
共享preferences = getShared preferences(NAME,Context.MODE_PRIVATE);
共享preferences.Editor编辑=共享preferences.edit();
editor.putBoolean(clicked2,真正的);
editor.commit();
}
如果(D){
toggleSound.start();
aButton2.setImageResource(R.drawable.off);
共享preferences = getShared preferences(NAME,Context.MODE_PRIVATE);
共享preferences.Editor编辑=共享preferences.edit();
编辑=共享preferences.edit();
editor.putBoolean(clicked2,FALSE);
editor.commit();
}
}
我想出了一个办法,这是工作精绝。
首先,我定义的全局变量
AnimationDrawable myFrameAnimation;
然后在OnCreate()中我用变量定义的按钮。
aButton3 =(的ImageButton)findViewById(R.id.imageButton3);
然后创建该按钮的onClickListener()方法。
aButton3.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
共享preferences共享preferences = getShared preferences(NAME,Context.MODE_PRIVATE);
布尔E =共享preferences.getBoolean(clicked3,FALSE);
如果(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;
数= 1;
共享preferences = getShared preferences(NAME,Context.MODE_PRIVATE);
共享preferences.Editor编辑=共享preferences.edit();
editor.putBoolean(clicked3,真正的);
editor.commit();
editor.putInt(clicked5,DEF);
editor.commit();
editor.putInt(clicked4,计数);
editor.commit();
}
如果(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);
计数= 0;
高清= 0;
共享preferences = getShared preferences(NAME,Context.MODE_PRIVATE);
共享preferences.Editor编辑=共享preferences.edit();
编辑=共享preferences.edit();
editor.putBoolean(clicked3,FALSE);
editor.commit();
editor.putInt(clicked4,计数);
editor.commit();
editor.putInt(clicked6,DEF);
editor.commit();
}
}
});
下面跨是一个纯粹的透明图像,使前点击我的previous图像不点击后显示。
在frame_animation我定义的图像的所有帧得到一个完美的动画形象
<动画列表的android:单稳=假的xmlns:机器人=http://schemas.android.com/apk/res/android>
<项目机器人:可绘制=@绘制/ PIC1机器人:时间=10/>
<项目机器人:可绘制=@绘制/ PIC2机器人:时间=10/>
<项目机器人:可绘制=@绘制/ PIC3机器人:时间=10/>
<项目机器人:可绘制=@绘制/ pic4机器人:时间=10/>
<项目机器人:可绘制=@绘制/ PIC5机器人:时间=10/>
<项目机器人:可绘制=@绘制/ pic6机器人:时间=10/>
<项目机器人:可绘制=@绘制/ pic7机器人:时间=10/>
<项目机器人:可绘制=@绘制/ PIC8机器人:时间=10/>
<项目机器人:可绘制=@绘制/ pic9机器人:时间=10/>
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();
}
}
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" />
这篇关于在ImageButton的GIF的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!