转载请注明出入:http://blog.csdn.net/forwardyzk/article/details/42640031
模拟扫描文件的效果,模拟雷达扫描。
思路:
1.使用旋转动画和渐变动画的结合。
2.使用线程和Handler进行消息的传递,刷新界面
不要在主线程上做耗时操作,不要在主线程上刷新界面。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="120dip"
android:orientation="horizontal" >
<FrameLayout
android:layout_width="120dip"
android:layout_height="120dip" >
<ImageView
android:layout_width="80dip"
android:layout_height="80dip"
android:layout_gravity="center"
android:src="@drawable/act_radar_bg" />
<ImageView
android:id="@+id/im_scan"
android:layout_width="65dip"
android:layout_height="65dip"
android:layout_gravity="center"
android:src="@drawable/act_radar_scanning_03" />
<ImageView
android:id="@+id/im_dian"
android:layout_width="65dip"
android:layout_height="65dip"
android:layout_gravity="center"
android:src="@drawable/act_radar_dian_03" />
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_lodingApk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="正在扫描:XXX.Apk"
android:textSize="12sp" />
<ProgressBar
android:id="@+id/pb_loding"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginRight="15dip"
android:progressDrawable="@drawable/progress_horizontal" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="@+id/tv_count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="已经扫描了XX文件" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/ll_scanText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="文件" />
</LinearLayout>
</ScrollView>
</LinearLayout>
MainActivity.java
初始化动画
animation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(2000);
animation.setRepeatCount(Animation.INFINITE);
animation2 = new AlphaAnimation(0.0f, 1.0f);
animation2.setDuration(3000);
animation2.setRepeatCount(Animation.INFINITE);
im_scan.startAnimation(animation);
im_dian.startAnimation(animation2);
开启线程
thread = new Thread() {
public void run() {
pb_loding.setMax(200);
for (count = 1; count <= pb_loding.getMax(); count++) {
Message msg = Message.obtain();
msg.what = SCAN_LODING;
handler.sendMessage(msg);
pb_loding.setProgress(count);
SystemClock.sleep(200);
}
Message msg = Message.obtain();
msg.what = FINSH_SCAN;
handler.sendMessage(msg);
};
};
thread.start();
刷新界面
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case SCAN_LODING:
TextView tv = new TextView(MainActivity.this);
tv.setTextSize(14);
tv_count.setText("已扫描:" + count + "个文件");
tv_lodingApk.setText("正在扫描:第" + count + "个文件");
if (count - 1 != 0)
tv.setText("第" + (count - 1) + "个文件已经扫描");
ll_scanText.addView(tv, 0);
break;
case FINSH_SCAN:
tv_lodingApk.setText("扫描完毕");
TextView tv1 = new TextView(MainActivity.this);
tv1.setTextSize(14);
tv1.setText("第" + count + "个文件已经扫描");
ll_scanText.addView(tv1, 0);
im_scan.clearAnimation();// 清除此ImageView身上的动画
im_dian.clearAnimation();// 清除此ImageView身上的动画
break;
}
};
};
源码下载: http://download.csdn.net/detail/forwardyzk/8352683
效果图: