Hencode练习题一自定义通用圆

HenCode 练习题一,定义通用的圆的自定义View

首先在res/values/ 目录中新建attr.xml



    
        
        
        
        
        
        
        
        
        
        
        
        
    

自定义通用的圆

package com.diabin.hencode.draw;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.diabin.hencode.R;

/**
 * Created by Administrator on 2017/10/17.
 */

public class CircleView extends View {
    private Paint mPaint;
    private float circleRadio;//半径
    private int circleColor;//圆的颜色
    private float circleStrokeWidth;//圆环的宽度
    private boolean isSolid;//是否时实心还是圆环
    private float fx,fy;//圆心坐标
    private Context mContext;

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

    public CircleView(Context context, @Nullable AttributeSet attrs) {
        this(context,attrs,0);
    }

    public CircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initViews(context,attrs);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int measuredWidth = getMeasuredWidth();
        int measuredHeight = getMeasuredHeight();
        //获取圆心的x坐标
        fx = measuredWidth/2;
        //获取圆心的y坐标
        fy = measuredHeight/2;

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //绘制圆
        canvas.drawCircle(fx,fy,circleRadio,mPaint);
    }

    private void initViews(Context context, AttributeSet attrs) {
        mContext = context;
        //获取xml中的属性值
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircleView);

        circleRadio = typedArray.getFloat(R.styleable.CircleView_circleRadio,0);
        circleColor = typedArray.getColor(R.styleable.CircleView_circleColor, Color.RED);
        circleStrokeWidth = typedArray.getFloat(R.styleable.CircleView_circleStrokeWidth,0);
        isSolid = typedArray.getBoolean(R.styleable.CircleView_isSolid,false);
//        fx = typedArray.getFloat(R.styleable.CircleView_circleFx,0);
//        fy = typedArray.getFloat(R.styleable.CircleView_circleFy,0);
        //别忘了回收资源
        typedArray.recycle();
        //初始化画笔
        initPaint();


    }


    /**
     * 初始化画笔
     */
    private void initPaint() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);//抗锯齿
        mPaint.setColor(circleColor);
        mPaint.setStrokeWidth(circleStrokeWidth);
        if (isSolid){
            mPaint.setStyle(Paint.Style.FILL);//实心
        }else {
            mPaint.setStyle(Paint.Style.STROKE);//圆环
        }


//        if (fx == 0 && fy == 0){
//
//        }

    }
}


使用:





    




Hencode练习题一自定义通用圆_第1张图片
image.png

你可能感兴趣的:(Hencode练习题一自定义通用圆)