java – 在ImageButton中使用gif
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 在ImageButton中使用gif,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3493字,纯文字阅读大概需要5分钟。
内容图文
![java – 在ImageButton中使用gif](/upload/InfoBanner/zyjiaocheng/703/bd8c2762cdf9452da5e23d57fd1de0ea.jpg)
如果有人点击它,如何在ImageButton的src中切换动画GIF图像和静态图像?
在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();
}
}
解决方法:
我想出了一个方法,它工作得非常好.
首先我定义了全局变量
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();
}
}
});
这里“trans”是一个纯粹透明的图像,因此单击之前我之前的图像不会显示.
在“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" />
内容总结
以上是互联网集市为您收集整理的java – 在ImageButton中使用gif全部内容,希望文章能够帮你解决java – 在ImageButton中使用gif所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。