编写的时候遇到的一些问题:
1 之前的时候用的是eclipse,把所有的东西都清空后直接能把线性布局给拉过来,但是android studio 不行,得自己写,同时编写第一个的时候不会有提示。但是第二个之后就会有提示。
2 这里的进度条用的不是系统给的,所以在drawable文件中写一个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的编写主要分两部分: 1 左边图形的旋转:即用的是旋转动画,详解请看上一篇博客 private void showAnimation() { RotateAnimation rotateAnimation = new RotateAnimation(0,360, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); rotateAnimation.setDuration(1000); rotateAnimation.setRepeatCount(Animation.INFINITE); rotateAnimation.setInterpolator(new LinearInterpolator()); iv_main.startAnimation(rotateAnimation);}2 右边进度条:这时需使用异步任务来做:
1 主线程 显示提示视图 onPreExecute()
2 分线程 做长时间的工作(扫描应用,进度条前进)doInBackground(Integer... voids) onProgressUpdate(Double... values)
3 主视图更新界面 onPostExecute(String aVoid)
具体程序见下:
private void showScan() { final int appCount = 60; //异步任务 new AsyncTask<Integer,Double,String>(){ //1 主线程 显示提示视图 @Override protected void onPreExecute() { tv_main.setText("正在扫描中。。。"); pb_main.setMax(60); } //2 分线程 做长时间的工作(扫描应用) @Override protected String doInBackground(Integer... voids) { for(int i = 0;i<appCount;i++){ SystemClock.sleep(40); publishProgress(i + 0.0); } return "成功"; } //主线程 更新界面 @Override protected void onPostExecute(String aVoid) { pb_main.setVisibility(View.GONE); tv_main.setText("扫描完成,没有病毒"); iv_main.clearAnimation(); Toast.makeText(MainActivity.this,aVoid,Toast.LENGTH_SHORT).show(); } @Override protected void onProgressUpdate(Double... values) {// pb_main.incrementProgressBy(1); pb_main.setProgress(values[0].intValue()); }// Pararms[] params }.execute(); } 以上。