概述
最新在研究自定义view,刚好最近心情比较浮躁,所以想起了八卦图,遂有了自己实现八卦图的想法,记录以备忘,贴出代码:
package com.example.customviewdemo;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
/**
* Created by thompson on 16-11-2.
*/
public class MyView extends View {
private int defaultSize;
private int width;
private int height;
public MyView(Context context) {
super(context);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec,heightMeasureSpec);
int width = getSize(widthMeasureSpec);
int height = getSize(heightMeasureSpec);
setMeasuredDimension(width, height);
}
private int getSize(int measureSpec) {
int defaultNum = defaultSize;
int mode = MeasureSpec.getMode(measureSpec);
int size = MeasureSpec.getSize(measureSpec);
switch(mode){
case MeasureSpec.UNSPECIFIED:
defaultNum = defaultSize;
break;
case MeasureSpec.AT_MOST:
case MeasureSpec.EXACTLY:
defaultNum = size;
break;
}
return defaultNum;
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
width = w;
height = h;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//将坐标原点移位到画布中心
canvas.translate(width/2,height/2);
//canvas.rotate(-20);
int length = (int)(Math.min(width, height)*0.8)/2;
//画笔设置
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(2);
paint.setColor(Color.BLACK);
//太极图最外层stroke
canvas.drawCircle(0,0,length,paint);
//外切矩形
RectF rectOut = new RectF(-length,-length,length,length);
//左半圆
paint.setStyle(Paint.Style.FILL);
canvas.drawArc(rectOut,90,180,true,paint);
//右半圆
paint.setColor(Color.WHITE);
canvas.drawArc(rectOut,-90,180,false,paint);
//上半圆
paint.setColor(Color.BLACK);
RectF rect1 = new RectF(-length/2, -length, length/2, 0);
canvas.drawArc(rect1, -90, 180, true, paint);
//下半圆
paint.setColor(Color.WHITE);
RectF rect2 = new RectF(-length/2, 0, length/2, length);
canvas.drawArc(rect2, 90, 180, true, paint);
//太极点
canvas.drawCircle(0, -length/2, 10, paint);
paint.setColor(Color.BLACK);
canvas.drawCircle(0, length/2, 10, paint);
}
}
贴图:
最后
以上就是自然店员为你收集整理的太极八卦图实现的全部内容,希望文章能够帮你解决太极八卦图实现所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复