1.目标效果
旋转动画+病毒查杀效果
2.xml布局文件
(1)activity_kill_virus.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".KillVirusActivity"> <TextView
style="@style/TitleStyle"
android:text="手机杀毒" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"> <RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@mipmap/ic_scanner_malware" /> <ImageView
android:id="@+id/ivKV_scaning"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@mipmap/act_scanning_03" />
</RelativeLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="@+id/tvKV_scaning"
android:text="扫描过程"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <!--自定义进度条图片(3种类型)-->
<ProgressBar
android:id="@+id/pbKV_bar"
android:progressDrawable="@drawable/progress_bg"
style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout> <!--ScrollView只有一个直接子节点-->
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/ll_add_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"> </LinearLayout>
</ScrollView>
</LinearLayout>
(2)自定义样式的进度条
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!--进度条为0,使用的图片-->
<item android:id="@android:id/background" android:drawable="@drawable/security_progress_bg"/>
<!--进度条为0-100%之间,使用的图片-->
<item android:id="@android:id/secondaryProgress" android:drawable="@drawable/security_progress"/>
<!--进度条为100%时,使用的图片-->
<item android:id="@android:id/progress" android:drawable="@drawable/security_progress"/>
</layer-list>
3.java后台
package com.example.administrator.test62360safeguard; import android.annotation.SuppressLint;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView; import com.example.administrator.test62360safeguard.Utils.MD5Util;
import com.example.administrator.test62360safeguard.engine.VirusDao; import java.util.ArrayList;
import java.util.List;
import java.util.Random; public class KillVirusActivity extends AppCompatActivity {
ImageView ivKV_scaning;
TextView tvKV_scaning;
ProgressBar pbKV_bar;
LinearLayout ll_add_textview;
protected static final int SCANING = 100;
protected static final int SCAN_FINISH = 101;
private List<ScanResultInfoBean> virusScanInfoList;
private int index = 0; @SuppressLint("HandlerLeak")
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case SCANING:
//1,显示正在扫描应用的名称
ScanResultInfoBean info = (ScanResultInfoBean)msg.obj;
tvKV_scaning.setText(info.name);
//2,在线性布局中添加一个正在扫描应用的TextView
TextView textView = new TextView(getApplicationContext());
if(info.isVirus){
//是病毒
textView.setTextColor(Color.RED);
textView.setText("发现病毒:"+info.name);
}else{
//不是病毒
textView.setTextColor(Color.BLACK);
textView.setText("扫描安全:"+info.name);
}
ll_add_textview.addView(textView, 0); //参数2:设置为0,使得控件放在LinearLayout的顶部
break;
case SCAN_FINISH:
tvKV_scaning.setText("扫描完成");
//停止真正执行的旋转动画
ivKV_scaning.clearAnimation();
break;
}
}
}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kill_virus); initUI();
initAnimation();
checkVirus();
} /**
* 病毒扫描过程
*/
private void checkVirus() {
//耗时操作,开启一个线程
new Thread(){
public void run() {
//1.获取数据库中所有的病毒的md5码
List<String> virusList = VirusDao.getVirusList();
//获取手机上面的所有应用程序签名文件的md5码
//2.1获取包管理者对象
PackageManager pm = getPackageManager();
//2.2获取所有应用程序签名文件(PackageManager.GET_SIGNATURES 已安装应用的签名文件+)
//PackageManager.GET_UNINSTALLED_PACKAGES 卸载完了的应用,残余的文件
List<PackageInfo> packageInfoList = pm.getInstalledPackages(
PackageManager.GET_SIGNATURES + PackageManager.GET_UNINSTALLED_PACKAGES); //2.3创建记录病毒的集合
virusScanInfoList = new ArrayList<>(); //设置进度条的最大值
pbKV_bar.setMax(packageInfoList.size()); //3.遍历应用集合
for (PackageInfo packageInfo : packageInfoList) {
//获取签名文件的数组
Signature[] signatures = packageInfo.signatures;
//获取签名文件数组的第一位,然后进行md5,将此md5和数据库中的md5比对
Signature signature = signatures[0];
String string = signature.toCharsString();
//32位字符串,16进制字符(0-f)
String encoder = MD5Util.encoder(string);
//创建内部类ScanResultInfoBean的javabean用来存储一个应用经过扫描后的某些属性
ScanResultInfoBean scanResultInfoBean = new ScanResultInfoBean(); //4,比对应用是否为病毒
if(virusList.contains(encoder)){
//5.记录病毒
scanResultInfoBean.isVirus = true;
virusScanInfoList.add(scanResultInfoBean);
}else{
scanResultInfoBean.isVirus = false;
}
//6,维护对象的包名,以及应用名称
scanResultInfoBean.packageName = packageInfo.packageName;
scanResultInfoBean.name = packageInfo.applicationInfo.loadLabel(pm).toString(); //7.在扫描的过程中,需要更新进度条
index++;
pbKV_bar.setProgress(index); //给当前的子线程设置一段睡眠时间,模拟病毒扫描过程(即让扫描过程不要太快,达到肉眼可见的效果)
try {
Thread.sleep(50+new Random().nextInt(100));
} catch (InterruptedException e) {
e.printStackTrace();
} //8.在子线程中发送消息,告知主线程更新UI(1:顶部扫描应用的名称2:扫描过程中往线性布局中添加view)
Message msg = Message.obtain();
msg.what = SCANING;
msg.obj = scanResultInfoBean;
handler.sendMessage(msg);
}
Message msg = Message.obtain();
msg.what = SCAN_FINISH;
handler.sendMessage(msg);
}
}.start();
} /**
* 此javabean用来记录扫描结果中需要存储的应用相关信息
* isVirus 应用是否为病毒
* packageName 包名
* name 应用名称
*/
class ScanResultInfoBean{
public boolean isVirus;
public String packageName;
public String name;
} private void initAnimation() {
//参数1,2设置旋转的角度
RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(1000);
//设置动画一直旋转
rotateAnimation.setRepeatMode(Animation.INFINITE);
//保存动画执行结束后的状态
rotateAnimation.setFillAfter(true);
//开始执行动画
ivKV_scaning.startAnimation(rotateAnimation);
} private void initUI() {
ivKV_scaning=findViewById(R.id.ivKV_scaning);
tvKV_scaning=findViewById(R.id.tvKV_scaning);
pbKV_bar=findViewById(R.id.pbKV_bar);
ll_add_textview=findViewById(R.id.ll_add_textview);
}
}
4.效果图