Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 自定義 View 實戰:仿 vivo i管家病毒掃描動畫效果

編輯:關於 Android 程式設計

技術之路永無止境,如果真心熱愛,就勇敢堅持下去。每當遇到瓶頸、覺得程式枯燥時,我總會捫心自問是否真的熱愛這門技術,而答案始終堅定。

今天我們繼續聊 Android 自定義 View。先看成果:

這是手機殺毒軟體中的掃描動畫,類似雷達搜索,用途相當廣泛。理解它的邏輯與寫法,對技術提升很有幫助。

先分析畫面元素:主要有四個圓、一個扇形,以及八條虛線。掌握這些之後,就可以開始動手了。

先看用到的變數:

 private float mWidth; private float mHeight; private Paint mPaint, mPaint2, mPaint3, mPaint4, mLinePaint; private RectF mRectF; private float startAngle = 360; private float radius1, radius2, radius3, radius4; private Path path;

變數不多:六個畫筆、寬與高、起始角度、四個圓的半徑、一個矩形,以及繪製虛線用的 Path。接著是初始化操作。

 private void initData() { mPaint = new Paint(); mPaint.setStrokeWidth(1); mPaint.setAntiAlias(true); mPaint.setStyle(Style.STROKE); mPaint.setColor(Color.parseColor("#ffffff")); mPaint2 = new Paint(); mPaint2.setStrokeWidth(5); mPaint2.setColor(Color.parseColor("#00ff00")); mPaint3 = new Paint(); mPaint3.setStrokeWidth(3); mPaint3.setAntiAlias(true); mPaint3.setStyle(Style.STROKE); mPaint3.setColor(Color.parseColor("#ffffff")); mPaint4 = new Paint(); mPaint4.setStrokeWidth(2); mPaint4.setAntiAlias(true); mPaint4.setStyle(Style.STROKE); mPaint4.setColor(Color.parseColor("#ffffff")); mLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG); mLinePaint.setStrokeWidth(1); mLinePaint.setStyle(Paint.Style.STROKE); mLinePaint.setAntiAlias(true); mLinePaint.setColor(Color.parseColor("#ffffff")); path = new Path(); }

這裡包含五個畫筆與一個路徑的初始化。注意每個畫筆的樣式都不同,方便呈現不同效果。

初始化完成後,接著為變數賦值。我們选择在 onSizeChanged 中進行:

 @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); mWidth = getWidth(); mHeight = getHeight(); mRectF = new RectF((float) (mWidth * 0.1), (float) (mWidth * 0.1), (float) (mWidth * 0.9), (float) (mWidth * 0.9)); // 繪製漸變效果 LinearGradient gradient = new LinearGradient((float) (mWidth * 0.3), (float) (mWidth * 0.9), (float) (mWidth * 0.1), (float) (mWidth * 0.5), new int[] { Color.parseColor("#458EFD"), Color.GREEN, Color.parseColor("#458EFD"), Color.WHITE, Color.parseColor("#458EFD") }, null, Shader.TileMode.CLAMP); mPaint2.setShader(gradient); radius1 = (float) (mWidth * 0.4); radius2 = (float) (mWidth * 0.3); radius3 = (float) (mWidth * 0.2); radius4 = (float) (mWidth * 0.1); }

在前面幾個自定義 View 中,幾乎所有變數初值都寫在這個方法裡。它是系統回呼方法,會在 View 大小改變時被呼叫,而且執行時機在 onDraw 之前,因此特別適合用來初始化與尺寸相關的變數。畫面中的漸變,正是這裡 LinearGradient 類別的傑作。

準備工作完成,開始真正的繪圖。先看 onDraw:

 @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvasArc(canvas); canvasArc2(canvas); canvasCircle(canvas); canvasLine(canvas); }

畫圓、畫扇形、畫線都在這裡實現。逐一來看:

第一個旋轉扇形:

 private void canvasArc(Canvas canvas) { canvas.drawArc(mRectF, startAngle, 60, true, mPaint2); }

第二個扇形類似,但角度只有 1:

 private void canvasArc2(Canvas canvas) { canvas.drawArc(mRectF, startAngle, 1, true, mPaint3); }

這裡只需要它呈現旋轉角度的效果,不需要多寬,因此角度設為 1。實作自定義 View 時,也要學會活學活用。

四個圓:

 private void canvasCircle(Canvas canvas) { canvas.drawCircle(mWidth / 2, mHeight / 2, radius1, mPaint3); canvas.drawCircle(mWidth / 2, mHeight / 2, radius2, mPaint); canvas.drawCircle(mWidth / 2, mHeight / 2, radius3, mPaint); canvas.drawCircle(mWidth / 2, mHeight / 2, radius4, mPaint4); }

四個圓就是四行程式碼。要注意的是,它們所用的畫筆略有不同,這裡是容易忽略的地方。

最後是我斟酌許久的虛線繪製,這裡一度讓我卡關:

 private void canvasLine(Canvas canvas) { int lineCount = 8; for (int i = 0; i < lineCount; i++) { path.moveTo(mWidth / 2, mHeight / 2); path.lineTo(radius1, radius4); PathEffect effects = new DashPathEffect(new float[] { (float) (mWidth * 0.005), (float) (mWidth * 0.02), (float) (mWidth * 0.005), (float) (mWidth * 0.02) }, 0); mLinePaint.setPathEffect(effects); canvas.drawPath(path, mLinePaint); canvas.rotate(45, mWidth / 2, mHeight / 2); } }

繪製虛線本質也是畫線,但一開始用 canvas.drawLine 卻沒有生效,查資料後判斷可能是版本差異,於是改用 Path 才成功。

至此效果已全部呈現。為了讓它動起來,我的思路是:定義一個執行緒,透過改變扇形的起始角度來實現動畫。

 class MyThread extends Thread { @Override public void run() { while (true) { if (running) { SystemClock.sleep(200); handler.sendEmptyMessage(2); } } } } private boolean running = true; public Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { synchronized (this) { if (startAngle < 1) { startAngle = 360; } else { startAngle--; invalidate(); } } }; };

為了讓控制項更容易被外部使用,我們對外暴露了一些方法:

 // 開啟動畫 public void setStartAngle() { thread = new MyThread(); thread.start(); } // 重新開啟動畫 public void startAnge() { running = true; } // 暫停動畫 public void stopAnge() { running = false; } // 是否在運動 public boolean isRunning() { return running; }

最後看看 MainActivity 的用法:

 public class MainActivity extends ActionBarActivity { private VirusKilling mVirusKilling; float mVirusKillingValue = 360; private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); mVirusKilling = (VirusKilling) findViewById(R.id.Virus_Killing); tv = (TextView) findViewById(R.id.tv); mVirusKilling.setTextView(tv); mVirusKilling.setStartAngle(); } public void onStop(View v) { TextView tv = (TextView) v; if (mVirusKilling.isRunning()) { tv.setText("繼續掃描"); mVirusKilling.stopAnge(); } else { tv.setText("停止掃描"); mVirusKilling.startAnge(); } } @Override protected void onPause() { mVirusKilling.stopAnge(); super.onPause(); }; @Override protected void onStart() { mVirusKilling.startAnge(); super.onStart(); } }

版面配置檔:

              

背景效果的程式碼:

     

至此,所有效果都完成了。程式碼不多,多看幾次就能理解。技術也許還不夠高端,但日子還長,期待與大家一起進步。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved