自定義View-太極旋轉

主要注意的坑就是,畫布進行旋轉或者平移之後是不會對之前畫的畫進行更改的。所以,想要有畫布變化效果,畫布的變化操作是不能放在最後一句的。

上兩張效果圖,不知道怎麼錄動圖,還有一點,選擇的左邊系很重要,我這裏取的座標系已經很複雜了感覺。







直接貼代碼:

public class TaiJiCustomView extends View {
    Paint whitePaint,blackPaint;

    public TaiJiCustomView(Context context) {
        super(context);
        init();
    }

    public TaiJiCustomView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TaiJiCustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    private void init(){
        whitePaint=new Paint();
        blackPaint=new Paint();

        whitePaint.setColor(Color.WHITE);
        whitePaint.setAntiAlias(true);

        blackPaint.setColor(Color.BLACK);
        blackPaint.setAntiAlias(true);
    }

    float angle;
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int height=getHeight();
        int width=getWidth();
        int bigRadio=Math.min(height,width);
        //畫背景顏色
        canvas.drawColor(Color.GRAY);
        //高度移動到中心
        canvas.translate(0,(height-bigRadio)/2);
        //旋轉畫布
        canvas.rotate(angle,bigRadio/2,bigRadio/2);                   //因爲畫布旋轉對之前的繪畫沒有影響,所以得放在前面,如果放在最後,沒有效果
        angle++;
        //畫圓狐區域
        RectF rectF=new RectF(0,0,bigRadio,bigRadio);
        canvas.drawArc(rectF,90,180,true,blackPaint);
        canvas.drawArc(rectF,-90,180,true,whitePaint);

        //畫半圓,(只是被遮擋,實際是一個圓)
        canvas.drawCircle(bigRadio/2,bigRadio/2+bigRadio/4,bigRadio/4,whitePaint);
        canvas.drawCircle(bigRadio/2,bigRadio/2-bigRadio/4,bigRadio/4,blackPaint);
        //畫最後兩個小圓
        canvas.drawCircle(bigRadio/2-bigRadio/8,bigRadio/2+bigRadio/4,bigRadio/16,blackPaint);
        canvas.drawCircle(bigRadio/2+bigRadio/8,bigRadio/2-bigRadio/4,bigRadio/16,whitePaint);


        postInvalidateDelayed(1);
    }


}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章