public class CircleView extends View { |
private int bigRedius; |
private int smallRedius; |
private String text; |
private int textSize; |
private int color; |
Paint paint; |
public CircleView(Context context) { |
super (context); |
initView(context, null ); |
} |
public CircleView(Context context, AttributeSet attrs) { |
super (context, attrs); |
initView(context, attrs); |
} |
private void initView(Context context, AttributeSet attrs) { |
//获取自定义属性 |
TypedArray typearray = context.obtainStyledAttributes(attrs,R.styleable.CircleView); |
/**外圆半径*/ |
bigRedius = ( int )typearray.getDimension(R.styleable.CircleView_BigRedius, 150 ); |
/** 内圆半径*/ |
smallRedius = ( int ) typearray.getDimension(R.styleable.CircleView_smallRedius, 100 ); |
/** 字体*/ |
text = typearray.getString(R.styleable.CircleView_text); |
/** 字体大小*/ |
textSize = ( int ) typearray.getDimension(R.styleable.CircleView_textSize, 50 ); |
/** 圆环颜色*/ |
color = typearray.getColor(R.styleable.CircleView_color, Color.YELLOW); |
} |
@Override |
protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec) { |
super .onMeasure(widthMeasureSpec, heightMeasureSpec); |
//重新侧量 |
setMeasuredDimension(bigRedius* 2 ,bigRedius* 2 ); |
} |
@Override |
protected void onDraw(Canvas canvas) { |
super .onDraw(canvas); |
//初始化画笔 |
paint = new Paint(); |
//设置画笔颜色 |
paint.setColor(color); |
//绘制外圆 |
canvas.drawCircle(getWidth()/ 2 ,getHeight()/ 2 ,bigRedius,paint); |
paint.setColor(Color.WHITE); |
//绘制内圆 |
canvas.drawCircle(getWidth()/ 2 ,getHeight()/ 2 ,smallRedius,paint); |
//设置字体大小 颜色 |
paint.setTextSize(textSize); |
paint.setColor(Color.BLACK); |
//绘制文本内容 首先获取字体的宽,高 |
Rect rect = new Rect(); |
paint.getTextBounds(text, 0 ,text.length(),rect); |
//获取文本的宽高 |
int textWidth = rect.width(); |
int textHeight = rect.height(); |
//进行绘制 |
canvas.drawText(text, getWidth()/ 2 - (textWidth / 2 ), getHeight()/ 2 + (textHeight/ 2 ),paint); |
} |
@Override |
public boolean onTouchEvent(MotionEvent event) { |
switch (event.getAction()){ |
case MotionEvent.ACTION_DOWN: |
//获取按下时的位置 |
int downX = ( int ) event.getX(); |
int downY = ( int ) event.getY(); |
//对位置进行判断 |
judegDown(downX,downY); |
break ; |
} |
return true ; |
} |
private void judegDown( int downX, int downY) { |
//根据勾股定理获取点击位置到圆中心点的距离来判断点击的位置 |
int absX = Math.abs(getWidth()/ 2 - downX); |
int absY = Math.abs(getHeight()/ 2 - downY); |
double sqrt = Math.sqrt(absX * absX + absY * absY); |
if (sqrt <= bigRedius){ |
Random rand = new Random(); |
text = "" +rand.nextInt( 9999 ); |
postInvalidate(); |
} |
} |
} |
///////////////////////////////////////// |
<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:id= "@+id/activity_main" |
android:layout_width= "match_parent" |
android:layout_height= "match_parent" |
tools:context= "com.bwei.wenhuan20170213.MainActivity" > |
<com.bwei.wenhuan20170213.view.CircleView |
android:id= "@+id/circleview" |
android:layout_centerInParent= "true" |
android:layout_width= "wrap_content" |
android:layout_height= "wrap_content" |
app:text= "1234" |
app:BigRedius= "60dp" |
app:smallRedius= "30dp" |
app:textSize= "15sp" |
app:color= "@color/yellow" /> |
</RelativeLayout> |
////////////////////// |
attrs |
<resources> |
<declare-styleable name= "CircleView" > |
<attr name= "color" format= "color" /> |
<attr name= "BigRedius" format= "dimension" /> |
<attr name= "smallRedius" format= "dimension" /> |
<attr name= "text" format= "string" /> |
<attr name= "textSize" format= "dimension" /> |
</declare-styleable> |
</resources> |
by: 发表于:2017-09-27 09:59:34 顶(0) | 踩(0) 回复
??
回复评论