欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android 畫(huà)一個(gè)太極圖實(shí)例代碼

 更新時(shí)間:2016年09月22日 14:47:56   作者:白一辰  
這篇文章主要介紹了Android 畫(huà)一個(gè)太極圖實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下

今天練手一下,一起來(lái)畫(huà)個(gè)太極圖吧~

最終效果如下:

最終效果

一般都是先講原理,我就反其道而行,先講實(shí)現(xiàn)吧。

1.繼承實(shí)現(xiàn)初始化方法

繼承View,實(shí)現(xiàn)基本的構(gòu)造函數(shù):

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()方法中,進(jìn)行初始化操作,這里初始化一下畫(huà)筆就好。

private Paint mPaint;

private void init() {
  initPaint();
}

/**
 * 初始化畫(huà)筆
 */
private void initPaint() {
  mPaint = new Paint();        //創(chuàng)建畫(huà)筆對(duì)象
  mPaint.setColor(Color.BLACK);    //設(shè)置畫(huà)筆顏色
  mPaint.setStyle(Paint.Style.FILL); //設(shè)置畫(huà)筆模式為填充
  mPaint.setStrokeWidth(10f);     //設(shè)置畫(huà)筆寬度為10px
  mPaint.setAntiAlias(true);     //設(shè)置抗鋸齒
  mPaint.setAlpha(255);        //設(shè)置畫(huà)筆透明度
}

在onSizeChanged()方法中獲取高寬,便于之后繪制計(jì)算。

private int mWidth;
private int mHeight;  

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

創(chuàng)建兩個(gè)路徑,一下計(jì)算就在這兩個(gè)路徑中進(jìn)行。

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

然后到最關(guān)鍵的onDraw()方法了,這里會(huì)分幾步來(lái)演示。

1.移動(dòng)布局到中間

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

ps:為了簡(jiǎn)潔,之后的代碼都是在onDraw()中逐層增加的,之后就不寫(xiě)onDraw()的外出括號(hào)了。

2.畫(huà)背景黃色  

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

第二步.png

3.畫(huà)白色圓背景,即太極圖的白魚(yú)部分。

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

4.畫(huà)黑色圓背景,即太極圖的黑魚(yú)部分,和白魚(yú)一樣大小位置,只是把白魚(yú)蓋住了,這里就需要用一些boolean運(yùn)算進(jìn)行繪制了。

//白魚(yú)的背景
mPaint.setColor(0xffffffff);
path0.rewind();
path0.addCircle(0, 0, 200, Path.Direction.CW);
canvas.drawPath(path0, mPaint);

//黑魚(yú)的背景
mPaint.setColor(0xff000000);
path1.addCircle(0, 0, 200, Path.Direction.CW);
canvas.drawPath(path0, mPaint);//這一段注意,之后要?jiǎng)h除

第四步.png

5.對(duì)黑魚(yú)(path1)進(jìn)行boolean計(jì)算,把不需要的部分去掉。這里就是要把圓的右半邊消除,這里就需要用到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);//這一段注意,之后要?jiǎng)h除

第五步.png

6.這時(shí)候我們已經(jīng)把不需要的另一半黑色去掉了,但是黑魚(yú)應(yīng)該有個(gè)圓的頭,那么我們就拼接一個(gè)頭給它。

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);//這一段注意,之后要?jiǎng)h除

第六步.png

7.到這里,我們看到,只需要在繪制一個(gè)白魚(yú)的頭就可以了,那么也和第五步一樣,使用一個(gè)boolean運(yùn)算把多余的黑色去掉即可。

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.至此,已經(jīng)繪制好了八卦圖的背景了,只需要在繪制魚(yú)的眼睛即可。

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

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

第八步.png

完成,最后上完整的代碼。代碼寫(xiě)得有點(diǎn)亂,不過(guò)也是練習(xí)而已,哈哈。至于其中的boolean運(yùn)算什么的,之后在我的自定義View的筆記中在寫(xiě)吧。

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();
  }

  /**
   * 初始化畫(huà)筆
   */
  private void initPaint() {
    mPaint = new Paint();        //創(chuàng)建畫(huà)筆對(duì)象
    mPaint.setColor(Color.BLACK);    //設(shè)置畫(huà)筆顏色
    mPaint.setStyle(Paint.Style.FILL); //設(shè)置畫(huà)筆模式為填充
    mPaint.setStrokeWidth(10f);     //設(shè)置畫(huà)筆寬度為10px
    mPaint.setAntiAlias(true);     //設(shè)置抗鋸齒
    mPaint.setAlpha(255);        //設(shè)置畫(huà)筆透明度
  }

  @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);
    //移動(dòng)布局到中間
    canvas.translate(mWidth / 2, mHeight / 2);

    //畫(huà)大背景顏色
    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);

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

    //畫(huà)白色小圓
    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

以上就是對(duì)Android 實(shí)現(xiàn)太極的實(shí)例代碼,有興趣朋友可以參考下,謝謝大家對(duì)本站的支持!

相關(guān)文章

  • Android UI控件RatingBar實(shí)現(xiàn)自定義星星評(píng)分效果

    Android UI控件RatingBar實(shí)現(xiàn)自定義星星評(píng)分效果

    這篇文章主要為大家詳細(xì)介紹了Android UI控件RatingBar實(shí)現(xiàn)自定義星星評(píng)分效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • 詳解android與服務(wù)端交互的兩種方式

    詳解android與服務(wù)端交互的兩種方式

    這篇文章主要介紹了詳解android與服務(wù)端交互的兩種方式,此處介紹兩種方式:使用Google原生的Gson解析json數(shù)據(jù),使用JSONObject解析json數(shù)據(jù),有興趣的可以了解一下
    2017-07-07
  • Android基于reclyview實(shí)現(xiàn)列表回彈動(dòng)畫(huà)效果

    Android基于reclyview實(shí)現(xiàn)列表回彈動(dòng)畫(huà)效果

    這篇文章主要為大家詳細(xì)介紹了Android基于reclyview實(shí)現(xiàn)列表回彈動(dòng)畫(huà)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • 詳解Android內(nèi)存泄露及優(yōu)化方案

    詳解Android內(nèi)存泄露及優(yōu)化方案

    這篇文章主要介紹了詳解Android內(nèi)存泄露及優(yōu)化方案,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-09-09
  • android藍(lán)牙簡(jiǎn)單開(kāi)發(fā)示例教程

    android藍(lán)牙簡(jiǎn)單開(kāi)發(fā)示例教程

    大家好,本篇文章主要講的是android藍(lán)牙簡(jiǎn)單開(kāi)發(fā)示例教程,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下
    2021-12-12
  • Android實(shí)現(xiàn)后臺(tái)開(kāi)啟服務(wù)默默拍照功能

    Android實(shí)現(xiàn)后臺(tái)開(kāi)啟服務(wù)默默拍照功能

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)后臺(tái)開(kāi)啟服務(wù)默默拍照功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • Templates實(shí)戰(zhàn)之更優(yōu)雅實(shí)現(xiàn)自定義View構(gòu)造方法詳解

    Templates實(shí)戰(zhàn)之更優(yōu)雅實(shí)現(xiàn)自定義View構(gòu)造方法詳解

    本篇文章介紹如何利用Android Studio提供的Live Templates更優(yōu)雅實(shí)現(xiàn)自定義View的構(gòu)造方法,說(shuō)句人話就是:簡(jiǎn)化自定義View構(gòu)造參數(shù)模板代碼的編寫(xiě),實(shí)現(xiàn)自動(dòng)生成,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • Android View實(shí)現(xiàn)圓形進(jìn)度條

    Android View實(shí)現(xiàn)圓形進(jìn)度條

    這篇文章主要為大家詳細(xì)介紹了Android View實(shí)現(xiàn)圓形進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Android開(kāi)發(fā)常見(jiàn)問(wèn)題總結(jié)

    Android開(kāi)發(fā)常見(jiàn)問(wèn)題總結(jié)

    這篇文章主要介紹了Android開(kāi)發(fā)常見(jiàn)問(wèn)題,總結(jié)分析了諸如界面設(shè)計(jì)、多媒體調(diào)用、圖片、動(dòng)畫(huà)操作等開(kāi)發(fā)中常見(jiàn)的問(wèn)題解決方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2016-08-08
  • Android自定義可標(biāo)記日歷效果

    Android自定義可標(biāo)記日歷效果

    這篇文章主要為大家詳細(xì)介紹了Android自定義可標(biāo)記日歷效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05

最新評(píng)論