Android实现下拉加载动画的效果
程序员文章站
2023-11-14 08:04:04
具体实现方法就不多介绍了先附上源码,相信大家都容易看的懂:
这里为了让这个动画效果可被复用,于是就继承了imageview 去实现某些方法
pa...
具体实现方法就不多介绍了先附上源码,相信大家都容易看的懂:
这里为了让这个动画效果可被复用,于是就继承了imageview 去实现某些方法
package com.example.loading_drawable; import android.content.context; import android.graphics.drawable.animationdrawable; import android.util.attributeset; import android.util.log; import android.view.view; import android.view.animation.animation; import android.widget.imageview; public class myimgview extends imageview { // 动画图层类 private animationdrawable bg_anim; public myimgview(context context) { super(context, null); initview(); } public myimgview(context context, attributeset attrs) { super(context, attrs, 0); } public myimgview(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); } //初始化 private void initview() { setbackgroundresource(r.drawable.flash_anim); bg_anim = (animationdrawable) getbackground(); log.i("aaa", "iniview"); } /** * 开启动画效果 */ public void startanim() { if (bg_anim != null) { bg_anim.start(); } } /** * 停止动画效果 */ public void stopanim() { if (bg_anim != null && bg_anim.isrunning()) { bg_anim.stop(); } } /* * (non-javadoc) * * @see android.widget.imageview#setvisibility(int) 当控件被显示时就调用 开启动画效果,反之 */ @override public void setvisibility(int visibility) { super.setvisibility(visibility); if (visibility == view.visible) { startanim(); } else { stopanim(); } } }
接下来就是:在res文件夹下新建 drawable文件夹,再此文件夹下新建 flash_anim.xml文件,具体如下:
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/a01_02" android:duration="50"/> <item android:drawable="@drawable/a01_04" android:duration="50"/> <item android:drawable="@drawable/a01_06" android:duration="50"/> <item android:drawable="@drawable/a01_08" android:duration="50"/> <item android:drawable="@drawable/a01_10" android:duration="50"/> <item android:drawable="@drawable/a01_12" android:duration="50"/> <item android:drawable="@drawable/a01_14" android:duration="50"/> <item android:drawable="@drawable/a01_16" android:duration="50"/> <item android:drawable="@drawable/a01_25" android:duration="50"/> <item android:drawable="@drawable/a01_26" android:duration="50"/> <item android:drawable="@drawable/a01_27" android:duration="50"/> <item android:drawable="@drawable/a01_28" android:duration="50"/> <item android:drawable="@drawable/a01_30" android:duration="50"/> <item android:drawable="@drawable/a01_31" android:duration="50"/> <item android:drawable="@drawable/a01_32" android:duration="50"/> <item android:drawable="@drawable/a01_41" android:duration="50"/> <item android:drawable="@drawable/a01_42" android:duration="50"/> <item android:drawable="@drawable/a01_43" android:duration="50"/> <item android:drawable="@drawable/a01_44" android:duration="50"/> <item android:drawable="@drawable/a01_45" android:duration="50"/> <item android:drawable="@drawable/a01_46" android:duration="50"/> <item android:drawable="@drawable/a01_47" android:duration="50"/> <item android:drawable="@drawable/a01_48" android:duration="50"/> <item android:drawable="@drawable/a01_57" android:duration="50"/> <item android:drawable="@drawable/a01_58" android:duration="50"/> <item android:drawable="@drawable/a01_59" android:duration="50"/> <item android:drawable="@drawable/a01_60" android:duration="50"/> <item android:drawable="@drawable/a01_61" android:duration="50"/> <item android:drawable="@drawable/a01_62" android:duration="50"/> <item android:drawable="@drawable/a01_63" android:duration="50"/> <item android:drawable="@drawable/a01_64" android:duration="50"/> </animation-list>
这样就基本搞定了,接下来就要在main中调用自定义的main就可以;如下:
package com.example.loading_drawable; import android.app.activity; import android.os.bundle; import android.view.gravity; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.linearlayout; import android.widget.linearlayout.layoutparams; /** * @author administrator 慕课网下拉刷新进度显示控件 * */ public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); linearlayout rootlayout = new linearlayout(this); rootlayout.setorientation(linearlayout.vertical); rootlayout.setlayoutparams(new linearlayout.layoutparams( linearlayout.layoutparams.match_parent, linearlayout.layoutparams.match_parent)); rootlayout.setgravity(gravity.center); button btn = new button(this); btn.settext("展现动画"); final myimgview imgview = new myimgview(mainactivity.this); imgview.setlayoutparams(new linearlayout.layoutparams( linearlayout.layoutparams.wrap_content, linearlayout.layoutparams.wrap_content)); imgview.setvisibility(view.gone); rootlayout.addview(btn); rootlayout.addview(imgview); setcontentview(rootlayout); btn.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { imgview.setvisibility(view.visible); } }); } }
这里是用自定义代码布局文件做的,布局方便,插件代码整合,如上所述,这个动画就完成了,只在需要的地方设置imgview为显示,动画就会开启,隐藏动画就会被关闭。
具体内容到此为止,希望大家能够喜欢。
下一篇: LZW数据压缩算法的原理分析