时间:2021-01-30 10:26:19 | 栏目:Android代码 | 点击:次
来自同事_Smile的封装应用,记录学习一下,分享给大家,具体如下:
1、AnimImageViewLoader类的封装
/** * 适用于animation-list 动画 * Created by _Smile on 2016/6/13. */ public class AnimImageViewLoader extends ImageView { private AnimationDrawable frameAnimation; public AnimImageViewLoader(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } public AnimImageViewLoader(Context context, AttributeSet attrs) { super(context, attrs); init(); } public AnimImageViewLoader(Context context) { super(context); init(); } /** * 初始化动画信息 */ private void init() { frameAnimation = (AnimationDrawable) getBackground(); post(new Runnable(){ public void run(){ frameAnimation.start(); } }); } /** * 开启动画显示 */ public void startAnimation() { frameAnimation = (AnimationDrawable) getBackground(); post(new Runnable() { @Override public void run() { frameAnimation.start(); } }); } /** * 关闭动画显示 */ public void stopAnimation() { post(new Runnable() { @Override public void run() { frameAnimation.stop(); } }); } }
2、布局文件
<com.tecsun.tsb.res.widget.AnimImageViewLoader android:id="@+id/aiv_take_id_card_pic" style="@style/style_wrap_content" android:layout_alignTop="@id/iv_id_card_take_pic_main" android:background="@drawable/anim_id_card_positive_pic" />
3、在代码中的调用
AnimImageViewLoader aivIdCardPic = xFindViewById(R.id.aiv_take_id_card_pic); aivIdCardPic.setBackgroundResource(R.drawable.anim_id_card_negative_pic); aivIdCardPic.startAnimation();
4、R.drawable.anim_id_card_negative_pic文件内容
<?xml version="1.0" encoding="utf-8"?> <!--图片动画--> <animation-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@mipmap/ic_id_card_negative_01" android:duration="300"/> <item android:drawable="@mipmap/ic_id_card_negative_02" android:duration="300"/> <item android:drawable="@mipmap/ic_id_card_negative_03" android:duration="300"/> <item android:drawable="@mipmap/ic_id_card_negative_04" android:duration="300"/> <item android:drawable="@mipmap/ic_id_card_negative_05" android:duration="300"/> <item android:drawable="@mipmap/ic_id_card_negative_06" android:duration="300"/> </animation-list>