Android 畫一個太極圖執行個體代碼_Android

來源:互聯網
上載者:User

今天練手一下,一起來畫個太極圖吧~

最終效果如下:

最終效果

一般都是先講原理,我就反其道而行,先講實現吧。

1.繼承實現初始化方法

繼承View,實現基本的建構函式:

public TestView(Context context) {  this(context, null);}public TestView(Context context, AttributeSet attrs) {  this(context, attrs, 0);}public TestView(Context context, AttributeSet attrs, int defStyleAttr) {  this(context, attrs, defStyleAttr, 0);}@TargetApi(Build.VERSION_CODES.LOLLIPOP)public TestView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {  super(context, attrs, defStyleAttr, defStyleRes);  init();}

在init()方法中,進行初始化操作,這裡初始化一下畫筆就好。

private Paint mPaint;private void init() {  initPaint();}/** * 初始化畫筆 */private void initPaint() {  mPaint = new Paint();        //建立畫筆對象  mPaint.setColor(Color.BLACK);    //設定畫筆顏色  mPaint.setStyle(Paint.Style.FILL); //設定畫筆模式為填充  mPaint.setStrokeWidth(10f);     //設定畫筆寬度為10px  mPaint.setAntiAlias(true);     //設定消除鋸齒  mPaint.setAlpha(255);        //設定畫筆透明度}

在onSizeChanged()方法中擷取高寬,便於之後繪製計算。

private int mWidth;private int mHeight;  @Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) {  super.onSizeChanged(w, h, oldw, oldh);  mWidth = w;  mHeight = h;}

建立兩個路徑,一下計算就在這兩個路徑中進行。

private Path path0 = new Path();private Path path1 = new Path();

然後到最關鍵的onDraw()方法了,這裡會分幾步來示範。

1.移動布局到中間

@Overrideprotected void onDraw(Canvas canvas) {  super.onDraw(canvas);  //移動布局到中間  canvas.translate(mWidth / 2, mHeight / 2);}

ps:為了簡潔,之後的代碼都是在onDraw()中逐層增加的,之後就不寫onDraw()的外出括弧了。

2.畫背景黃色  

mPaint.setColor(0xffffff00);  path0.addRect(-400, -400, 400, 400, Path.Direction.CW);  canvas.drawPath(path0, mPaint);

第二步.png

3.畫白色圓背景,即太極圖的白魚部分。

mPaint.setColor(0xffffffff);path0.rewind();path0.addCircle(0, 0, 200, Path.Direction.CW);canvas.drawPath(path0, mPaint);

4.畫黑色圓背景,即太極圖的黑魚部分,和白魚一樣大小位置,只是把白魚蓋住了,這裡就需要用一些boolean運算進行繪製了。

//白魚的背景mPaint.setColor(0xffffffff);path0.rewind();path0.addCircle(0, 0, 200, Path.Direction.CW);canvas.drawPath(path0, mPaint);//黑魚的背景mPaint.setColor(0xff000000);path1.addCircle(0, 0, 200, Path.Direction.CW);canvas.drawPath(path0, mPaint);//這一段注意,之後要刪除

第四步.png

5.對黑魚(path1)進行boolean計算,把不需要的部分去掉。這裡就是要把圓的右半邊消除,這裡就需要用到path.op()方法了。

mPaint.setColor(0xffffffff);path0.rewind();path0.addCircle(0, 0, 200, Path.Direction.CW);canvas.drawPath(path0, mPaint);mPaint.setColor(0xff000000);path1.addCircle(0, 0, 200, Path.Direction.CW);path0.rewind();path0.addRect(0, -200, 200, 200, Path.Direction.CW);path1.op(path0, Path.Op.DIFFERENCE);canvas.drawPath(path0, mPaint);//這一段注意,之後要刪除

第五步.png

6.這時候我們已經把不需要的另一半黑色去掉了,但是黑魚應該有個圓的頭,那麼我們就拼接一個頭給它。

mPaint.setColor(0xffffffff);path0.rewind();path0.addCircle(0, 0, 200, Path.Direction.CW);canvas.drawPath(path0, mPaint);mPaint.setColor(0xff000000);path1.addCircle(0, 0, 200, Path.Direction.CW);path0.rewind();path0.addRect(0, -200, 200, 200, Path.Direction.CW);path1.op(path0, Path.Op.DIFFERENCE);path0.rewind();path0.addCircle(0, -100, 100, Path.Direction.CW);path1.op(path0, Path.Op.UNION);canvas.drawPath(path1, mPaint);//這一段注意,之後要刪除

第六步.png

7.到這裡,我們看到,只需要在繪製一個白魚的頭就可以了,那麼也和第五步一樣,使用一個boolean運算把多餘的黑色去掉即可。

mPaint.setColor(0xffffffff);path0.rewind();path0.addCircle(0, 0, 200, Path.Direction.CW);canvas.drawPath(path0, mPaint);mPaint.setColor(0xff000000);path1.addCircle(0, 0, 200, Path.Direction.CW);path0.rewind();path0.addRect(0, -200, 200, 200, Path.Direction.CW);path1.op(path0, Path.Op.DIFFERENCE);path0.rewind();path0.addCircle(0, -100, 100, Path.Direction.CW);path1.op(path0, Path.Op.UNION);path0.rewind();path0.addCircle(0, 100, 100, Path.Direction.CW);path1.op(path0, Path.Op.DIFFERENCE);canvas.drawPath(path1, mPaint);

第七步.png

8.至此,已經繪製好了八卦圖的背景了,只需要在繪製魚的眼睛即可。

//畫黑色小圓path0.rewind();path0.addCircle(0, 100, 50, Path.Direction.CW);mPaint.setColor(0xff000000);canvas.drawPath(path0, mPaint);//畫白色小圓path0.rewind();path0.addCircle(0, -100, 50, Path.Direction.CW);mPaint.setColor(0xffffffff);canvas.drawPath(path0, mPaint);

第八步.png

完成,最後上完整的代碼。代碼寫得有點亂,不過也是練習而已,哈哈。至於其中的boolean運算什麼的,之後在我的自訂View的筆記中在寫吧。

import android.annotation.TargetApi;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Path;import android.os.Build;import android.util.AttributeSet;import android.view.View;/** * Created by Whitelaning on 2016/6/28. * Email: whitelaning@qq.com */public class TestView extends View {  private Paint mPaint;  private int mWidth;  private int mHeight;  public TestView(Context context) {    this(context, null);  }  public TestView(Context context, AttributeSet attrs) {    this(context, attrs, 0);  }  public TestView(Context context, AttributeSet attrs, int defStyleAttr) {    this(context, attrs, defStyleAttr, 0);  }  @TargetApi(Build.VERSION_CODES.LOLLIPOP)  public TestView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {    super(context, attrs, defStyleAttr, defStyleRes);    init();  }  private void init() {    initPaint();  }  /**   * 初始化畫筆   */  private void initPaint() {    mPaint = new Paint();        //建立畫筆對象    mPaint.setColor(Color.BLACK);    //設定畫筆顏色    mPaint.setStyle(Paint.Style.FILL); //設定畫筆模式為填充    mPaint.setStrokeWidth(10f);     //設定畫筆寬度為10px    mPaint.setAntiAlias(true);     //設定消除鋸齒    mPaint.setAlpha(255);        //設定畫筆透明度  }  @Override  protected void onSizeChanged(int w, int h, int oldw, int oldh) {    super.onSizeChanged(w, h, oldw, oldh);    mWidth = w;    mHeight = h;  }  private Path path0 = new Path();  private Path path1 = new Path();  @Override  protected void onDraw(Canvas canvas) {    super.onDraw(canvas);    //移動布局到中間    canvas.translate(mWidth / 2, mHeight / 2);    //畫大背景顏色    mPaint.setColor(0xffffff00);    path0.addRect(-400, -400, 400, 400, Path.Direction.CW);    canvas.drawPath(path0, mPaint);    mPaint.setColor(0xffffffff);    path0.rewind();    path0.addCircle(0, 0, 200, Path.Direction.CW);    canvas.drawPath(path0, mPaint);    mPaint.setColor(0xff000000);    path1.addCircle(0, 0, 200, Path.Direction.CW);    path0.rewind();    path0.addRect(0, -200, 200, 200, Path.Direction.CW);    path1.op(path0, Path.Op.DIFFERENCE);    path0.rewind();    path0.addCircle(0, -100, 100, Path.Direction.CW);    path1.op(path0, Path.Op.UNION);    path0.rewind();    path0.addCircle(0, 100, 100, Path.Direction.CW);    path1.op(path0, Path.Op.DIFFERENCE);    canvas.drawPath(path1, mPaint);    //畫黑色小圓    path0.rewind();    path0.addCircle(0, 100, 50, Path.Direction.CW);    mPaint.setColor(0xff000000);    canvas.drawPath(path0, mPaint);    //畫白色小圓    path0.rewind();    path0.addCircle(0, -100, 50, Path.Direction.CW);    mPaint.setColor(0xffffffff);    canvas.drawPath(path0, mPaint);  }}

Whitelaning
It's very easy to be different but very difficult to be better

以上就是對Android 實現太極的執行個體代碼,有興趣朋友可以參考下,謝謝大家對本站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.