大家好,又见面了,我是全栈君。
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<!-- 背景是雷达图片 -->
<FrameLayout
android:layout_width="80dp"
android:layout_height="match_parent"
android:background="@drawable/ic_scanner_malware">
<ImageView
android:id="@+id/iv_main_scan"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/act_scanning_03" />
</FrameLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_vertical">
<TextView
android:id="@+id/tv_main_scan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="杀毒引擎待命..." />
<ProgressBar
android:id="@+id/pb_main_scan"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progressDrawable="<span style="color:#ff0000;">@drawable/my_progress</span>"/>
<!-- progressDrawable : 指定进度背景和进度图片-->
</LinearLayout>
</LinearLayout>
</LinearLayout>
my_progress.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 指定进度条的背景图片 -->
<item android:id="@android:id/background"
android:drawable="@drawable/security_progress_bg"></item>
<!-- 指定进度条的进度图片 -->
<item android:id="@android:id/progress"
android:drawable="@drawable/security_progress"></item>
</layer-list>
MainActivity.java
package com.atguigu.l10_app;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ImageView iv_main_scan;
private TextView tv_main_scan;
private ProgressBar pb_main_scan;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 旋转的图片
iv_main_scan = (ImageView) findViewById(R.id.iv_main_scan);
// 字体提示
tv_main_scan = (TextView) findViewById(R.id.tv_main_scan);
// 进度条
pb_main_scan = (ProgressBar) findViewById(R.id.pb_main_scan);
// 启动扫描动画
startScanAnimation();
// 開始扫描应用
startScan();
}
/**
* 启动分线程扫描应用
*/
private void startScan() {
new AsyncTask<Void, Void, Void>() {
// 更新进度条之前先进行友好提示
@Override
protected void onPreExecute() {
tv_main_scan.setText("開始扫描杀毒!");
}
@Override
protected Void doInBackground(Void... params) {
int appCount = 100;
for (int i = 0; i < appCount; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 通知更新进度条
publishProgress();
}
return null;
}
// 同步进度的显示
protected void onProgressUpdate(Void[] values) {
pb_main_scan.incrementProgressBy(1);
}
// 清除动画效果
protected void onPostExecute(Void result) {
tv_main_scan.setText("没有病毒, 请放心使用!");
Toast.makeText(MainActivity.this, "扫描完毕, 没有发现病毒!", 0).show();
// 停止扫描动画
iv_main_scan.clearAnimation();
}
}.execute();
}
/**
* 启动扫描动画
*/
private void startScanAnimation() {
RotateAnimation animation = new RotateAnimation(0f, 360f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
animation.setDuration(1000);
animation.setRepeatCount(Animation.INFINITE);// 不限定反复次数
// 旋转的图片启动动画效果
iv_main_scan.startAnimation(animation);
}
}
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/115350.html原文链接:https://javaforall.cn