package bwie.day02view;
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 Zhengaihua on 2016/11/1.
*/
public class CustomCircular extends View {
private Paint paint;//画笔
private int mColor;
private float radius;
private TypedArray typedArray;
private String mText;
private float mRadius;
private float mTextSize;
private Rect rect;
private int widthMode;
private int widthSize;
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);
typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomCircular, defStyleAttr, 0);
radius = 40;
mText = typedArray.getString(R.styleable.CustomCircular_ztext);//获得文本
mColor = typedArray.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 = typedArray.getDimension(R.styleable.CustomCircular_radius, px);//获取半径
int px1 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 20, getResources().getDisplayMetrics());
mTextSize = typedArray.getDimension(R.styleable.CustomCircular_ztextSzie, px1);
typedArray.recycle();//释放资源
paint = new Paint();
rect = new Rect();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
widthMode = MeasureSpec.getMode(widthMeasureSpec);
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(str, 0, str.length(), rect);
/**
* 处理字体大小
*/
if (rect.width() > radius) {
paint.setTextSize(20);
paint.getTextBounds(str, 0, str.length(), rect);
}
paint.setColor(Color.BLACK);
canvas.drawText(str, width / 2 - rect.width() / 2, height / 2 + rect.height() / 2, paint);
}
}
<?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.day02view.MainActivity"> <bwie.day02view.CustomCircular android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:ztext="nihao" app:ztextSzie="20dp" /> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<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>
</resources>