刷新动画效果

来源:互联网 发布:淘宝韩国代购真假 编辑:程序博客网 时间:2024/05/17 01:52
由于android系统自带的ProgressBar配合刷新按钮展示的时候并不总能让我们满意,有时候就需要自定义刷新的动画效果。首先在XML中定义一个ProgressBar:

<ProgressBar                android:id="@+id/comm_progress"                style="?android:attr/progressBarStyleLarge"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_centerVertical="true"                android:layout_marginLeft="100dip"                android:indeterminateBehavior="repeat"                android:indeterminateDrawable="@drawable/progressbar"                android:indeterminateOnly="true"                android:max="100"                android:progress="0" />   <TextView                android:id="@+id/background_text"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_centerVertical="true"                android:layout_marginLeft="7dip"                android:layout_toRightOf="@id/comm_progress"                android:text="加载中。。。。"                android:textColor="#0000"                android:textSize="16sp"                android:textStyle="bold" />

因为不知道进度执行的具体时间,因此使用android:indeterminateBehavior、android:indeterminateDrawable、android:indeterminateOnly属性来定义进度的执行情况。具体的动画特效在drawable文件中,实际上特效是通过一系列连续图片叠加起来才表现出来的效果。@drawable/progressbar中progressbar是个XML文件:
<?xml version="1.0" encoding="utf-8"?><rotate xmlns:android="http://schemas.android.com/apk/res/android"  android:drawable="@drawable/loading_animation"  android:pivotX="50%"  android:pivotY="50%"  android:fromDegrees="0"  android:toDegrees="360"  />



<?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/progress_01" android:duration="125" />    <item android:drawable="@drawable/progress_02" android:duration="125" />    <item android:drawable="@drawable/progress_03" android:duration="125" />    <item android:drawable="@drawable/progress_04" android:duration="125" />    <item android:drawable="@drawable/progress_05" android:duration="125" />    <item android:drawable="@drawable/progress_06" android:duration="125" />    <item android:drawable="@drawable/progress_07" android:duration="125" />    <item android:drawable="@drawable/progress_08" android:duration="125" />    <item android:drawable="@drawable/progress_09" android:duration="125" />    <item android:drawable="@drawable/progress_10" android:duration="125" />    <item android:drawable="@drawable/progress_11" android:duration="125" />    <item android:drawable="@drawable/progress_12" android:duration="125" />    <item android:drawable="@drawable/progress_13" android:duration="125" />    <item android:drawable="@drawable/progress_14" android:duration="125" />    <item android:drawable="@drawable/progress_15" android:duration="125" />    <item android:drawable="@drawable/progress_16" android:duration="125" />    <item android:drawable="@drawable/progress_17" android:duration="125" />    <item android:drawable="@drawable/progress_18" android:duration="125" />    <item android:drawable="@drawable/progress_19" android:duration="125" />    <item android:drawable="@drawable/progress_20" android:duration="125" />    <item android:drawable="@drawable/progress_21" android:duration="125" />    <item android:drawable="@drawable/progress_22" android:duration="125" />    <item android:drawable="@drawable/progress_23" android:duration="125" />    <item android:drawable="@drawable/progress_24" android:duration="125" /></animation-list>

mAppcomment = LayoutInflater.from(this).inflate(R.layout.app_item_comment_list, null);mLoadingBackground = mAppcomment.findViewById(R.id.loading_bg);


效果图:

素材:

有了这信息后,就可以创建一个自定义的progressBar了,从代码中可以看出,只要它处于可见状态,进度条就会一直旋转,无限循环下去。当后台任务完成后,可以在代码中调用它,把可见性设置成GONE。

那么,在实际的应用中如何在代码中完成这一系列的操作呢?这里还要用到子线程和handler对象,代码如下:

protected void refreshWeiboList() {lv.setVisibility(View.GONE);Log.v(TAG, "LV is GONE");progressBar.setVisibility(View.VISIBLE);new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubLog.v(TAG, "startAsyTask");mAdpter = setAdapter(isSina, isTencent, mCurFooterTab);message = new Message();message.what = 1;mHandler.sendMessage(message);Log.v(TAG, "finishAsyTask");}}).start();}class MyHandler extends Handler {@Overridepublic void handleMessage(Message msg) {if (msg.what == 1) {Log.v(TAG, "startHandleMessage");progressBar.setVisibility(View.GONE);Log.v(TAG, "adapter is isEmpty:  "+mAdpter.isEmpty());lv.setAdapter(mAdpter);lv.setVisibility(View.VISIBLE);Log.v(TAG, "finishHandleMessage");}super.handleMessage(msg);}}
具体的调用顺序,我都打了log,根据它就可以清楚地看到程序的执行顺序,log如下:
09-01 06:10:59.872: V/BaseTimeLine(3554): LV is GONE09-01 06:10:59.892: V/BaseTimeLine(3554): startAsyTask09-01 06:11:02.092: V/BaseTimeLine(3554): startHandleMessage09-01 06:11:02.092: V/BaseTimeLine(3554): adapter is isEmpty:  false09-01 06:11:02.112: V/BaseTimeLine(3554): finishAsyTask09-01 06:11:02.132: V/BaseTimeLine(3554): finishHandleMessage
从log中可以看到,handler是最后结束的,因为他负责更新UI组件,只有子线程的任务完成后更新UI才有意义。



(完)

原创粉丝点击