自定義view基礎(chǔ)知識——drawArc()

重要:自定義view時刻牢記的模式:

1 MeasureSpec.EXACTLY:

使用measureSpec中size的值作為寬高的精確值當(dāng)我們將控件的layout_width或layout_height指定為具體數(shù)值時如andorid:layout_width="50dip",或者為FILL_PARENT是,都是控件大小已經(jīng)確定的情況,都是精確尺寸。

2 MeasureSpec.AT_MOST:

使用measureSpec中size的值作為最大值,采用不超過這個值的最大允許值
當(dāng)控件的layout_width或layout_height指定為WRAP_CONTENT時,控件大小一般隨著控件的子空間或內(nèi)容進(jìn)行變化,此時控件尺寸只要不超過父控件允許的最大尺寸即可。因此,此時的mode是AT_MOST,size給出了父控件允許的最大尺寸。

3 MeasureSpec.UNSPECIFIED是未指定尺寸,這種情況不多

<br /><br />

開始正文

繪制圓弧函數(shù)詳細(xì)分析--drawArc()

public void drawArc (RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)

參數(shù):(中文)  
oval -     用于確定圓弧形狀與尺寸的橢圓邊界(即橢圓外切矩形)  
startAngle - 開始角度(以時鐘3點的方向為0°,逆時針為正方向)  
sweepAngle - *** 掃過角度***(以時鐘3點的方向為0°,逆時針為正方向)  
useCenter - 是否包含圓心  
paint - 繪制圓弧的畫筆  2.繪制圓弧的原理

kotlin版本

class Taiji @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : View(context, attrs, defStyleAttr) {

    private var whitePaint: Paint? = null   //白色畫筆
    private var blackPaing: Paint? = null   //黑色畫筆
    private var degrees = 0f

    //初始化畫筆函數(shù)
    private fun initPaints() {
        whitePaint = Paint()
        whitePaint!!.isAntiAlias = true
        whitePaint!!.color = Color.WHITE

        blackPaing = Paint(whitePaint)
        blackPaing!!.color = Color.BLACK
    }

    init {
        initPaints()
    }


    fun setDegrees(degrees: Float) {
        this.degrees = degrees
        invalidate()
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        val measuredHeight = measuredHeight
    }

    override fun onDraw(canvas: Canvas) {
        val width = width                          //畫布寬度   view設(shè)置的寬度
        val height = height                        //畫布高度     view設(shè)置的高度

        canvas.translate((width / 2).toFloat(), (height / 2).toFloat())                     //將畫布移動到中心
        canvas.drawColor(Color.GRAY)                           //繪制背景色

        //在繪制完背景色后調(diào)用即可
        canvas.rotate(degrees)

        //半徑
        val radius = Math.min(width, height) / 2 - 100

        val rf = RectF((-radius).toFloat(), (-radius).toFloat(), radius.toFloat(), radius.toFloat())

        canvas.drawArc(rf, -90f, 180f, true, whitePaint!!)
        canvas.drawArc(rf, 90f, 180f, true, blackPaing!!)


        //繪制兩個小圓
        val smallRadius = radius / 2    //小圓半徑為大圓的一般
        canvas.drawCircle(0f, (-smallRadius).toFloat(), smallRadius.toFloat(), blackPaing!!)
        canvas.drawCircle(0f, smallRadius.toFloat(), smallRadius.toFloat(), whitePaint!!)

        //太極的小圓
        val smallcicleRadius = smallRadius / 4    //小圓半徑為大圓的一般
        canvas.drawCircle(0f, (-smallRadius).toFloat(), smallcicleRadius.toFloat(), whitePaint!!)
        canvas.drawCircle(0f, smallRadius.toFloat(), smallcicleRadius.toFloat(), blackPaing!!)
    }
}

java版本

package com.viewtest;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Administrator on 2017/6/19.
 */

public class TaijiView extends View{


    private Paint whitePaint;   //白色畫筆
    private Paint blackPaing;   //黑色畫筆
    private float degrees = 0;

 
    public TaijiView(Context context) {
        this(context, null);
    }

    public TaijiView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TaijiView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaints();
    }


  //初始化畫筆函數(shù)
    private void initPaints() {
        whitePaint = new Paint();
        whitePaint.setAntiAlias(true);
        whitePaint.setColor(Color.WHITE);

        blackPaing = new Paint(whitePaint);
        blackPaing.setColor(Color.BLACK);
    }

    public void setDegrees(float degrees) {
        this.degrees = degrees;
        invalidate();
    }

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

    @Override
    protected void onDraw(Canvas canvas) {
        int width = getWidth();                          //畫布寬度   view設(shè)置的寬度
        int height = getHeight();                        //畫布高度     view設(shè)置的高度

        canvas.translate(width/2,height/2);                     //將畫布移動到中心
        canvas.drawColor(Color.GRAY);                           //繪制背景色

        //在繪制完背景色后調(diào)用即可,旋轉(zhuǎn)的關(guān)鍵
        canvas.rotate(degrees);

        //半徑
        int radius = Math.min(width, height) / 2 - 100;

        RectF rf = new RectF(-radius,-radius,radius,radius);

        canvas.drawArc(rf,-90,180,true,whitePaint);
        canvas.drawArc(rf,90,180,true,blackPaing);


        //繪制兩個小圓
        int smallRadius = radius / 2;   //小圓半徑為大圓的一般
        canvas.drawCircle(0, -smallRadius, smallRadius, blackPaing);
        canvas.drawCircle(0, smallRadius, smallRadius, whitePaint);

        //太極的小圓
        int smallcicleRadius = smallRadius / 4; //小圓半徑為大圓的一般
        canvas.drawCircle(0, -smallRadius, smallcicleRadius, whitePaint);
        canvas.drawCircle(0, smallRadius, smallcicleRadius, blackPaing);
    }
}

測試activity:

class TaijiActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_taiji)
        var handler: Handler = object : Handler() {
            var degree: Float = 0f
            override fun handleMessage(msg: Message) {
                degree += 10f
                taiji.setDegrees(degree)
                this.sendEmptyMessageDelayed(0, 80)
            }

        }
        handler.sendEmptyMessageDelayed(0, 20)
    }

}

自定義view的最佳開始示例
效果圖:

熟悉cavas的方法:drawArc

GIF_20170619_155609.gif

參考鏈接:http://www.gcssloop.com/customview/taiji
這位大神寫的自定義view的教程還是很清晰明了的,感謝大佬拭心

<br /><br />

最后歡迎加入Kotlin QQ群,一起討論學(xué)習(xí):

Paste_Image.png

歡迎關(guān)注 微信公號

android頻道
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容