
如果有人点击它,如何在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所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)