package bwie.day02;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
/**
* Created by Gaojian on 2016/11/1.
*/
public class CustomCircular extends View {
private Paint paint;
private float radius;
private TypedArray a;
private Rect rect;
private String mText;
private int mColor;
private float mRadius;
private float mTextSize;
public CustomCircular(Context context) {
this(context, null);
}
public CustomCircular(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomCircular(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomCircular, defStyleAttr, 0);
radius = 40;
mText = a.getString(R.styleable.CustomCircular_ztext);//获得文本
mColor = a.getInt(R.styleable.CustomCircular_zcolor, Color.GREEN);//获得颜色
/**
* 单位的互换,
其中,第一个参数是你想要得到的单位,第二个参数是你想得到的单位的数值,
比如:我要得到一个20sp,
那么我就用TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 25,getResources().getDisplayMetrics()),
返回的就是25sp对应的px数值了。
*/
int px = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP, 20, getResources().getDisplayMetrics());
mRadius = a.getDimension(R.styleable.CustomCircular_radius, px);//获取半径
int px1 = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP, 20, getResources().getDisplayMetrics());
mTextSize = a.getDimension(R.styleable.CustomCircular_ztextSzie, px1);
a.recycle();
paint = new Paint();
rect = new Rect();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int width = 0;
int height = 0;
if (widthMode == MeasureSpec.EXACTLY) {
width = widthSize;
} else {
width = (int) (4 * radius);
}
if (heightMode == MeasureSpec.EXACTLY) {
height = heightSize;
} else {
height = (int) (4 * radius);
}
setMeasuredDimension(width, height);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int width = getWidth();
int height = getHeight();
//画一个大圆
paint.setColor(Color.YELLOW);
canvas.drawCircle(width / 2, height / 2, radius * 2, paint);
//画一个小圆
paint.setColor(Color.WHITE);
canvas.drawCircle(width / 2, height / 2, radius, paint);
//
// String str = "gao";
paint.setTextSize(mTextSize);
paint.getTextBounds(mText, 0, mText.length(), rect);
/**
* 处理字体大小
*/
if (rect.width() > radius) {
paint.setTextSize(20);
paint.getTextBounds(mText, 0, mText.length(), rect);
}
paint.setColor(Color.BLACK);
canvas.drawText(mText, width / 2 - rect.width() / 2, height / 2 + rect.height() / 2, paint);
}
}
<attr name="ztext" format="string" />
<attr name="ztextSzie" format="dimension" />
<attr name="radius" format="dimension" />
<attr name="zcolor" format="color" />
<declare-styleable name="CustomCircular">
<attr name="ztext" />
<attr name="ztextSzie" />
<attr name="radius" />
<attr name="zcolor" />
</declare-styleable>
布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="bwie.day02.MainActivity">
<bwie.day02.CustomCircular
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:ztextSzie="20dp"
app:ztext="gao" />
</RelativeLayout>