-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyTestView.java
122 lines (82 loc) · 3 KB
/
MyTestView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package com.suphu.demo.widget;
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.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.suphu.demo.R;
import java.util.Random;
/**
* Created by huchao on 2017/8/7.
* textview
*/
public class MyTestView extends View implements View.OnClickListener{
private int textSize;
private String text;
private int textColor;
private Paint mPaint;
private Rect rect;
public MyTestView(Context context) {
this(context,null);
}
public MyTestView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public MyTestView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray td=context.obtainStyledAttributes(attrs, R.styleable.MyTestView);
textSize= (int) td.getDimension(R.styleable.MyTestView_text_size,14);
text=td.getString(R.styleable.MyTestView_text_value);
textColor=td.getColor(R.styleable.MyTestView_text_color, Color.BLACK);
td.recycle();
mPaint=new Paint();
rect=new Rect();
mPaint.setTextSize(textSize);
mPaint.getTextBounds(text,0,text.length(),rect);
setOnClickListener(this);
}
@Override
protected void onDraw(Canvas canvas) {
mPaint.setColor(Color.GREEN);
canvas.drawRect(0,0,getWidth(),getHeight(),mPaint);
mPaint.setColor(textColor);
mPaint.setAntiAlias(true);
canvas.drawText(text,getWidth()/2-rect.width()/2,getHeight()/2+rect.height()/2,mPaint);
}
@Override
protected void onMeasure(int widthMeasureSpec, int 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=rect.width()+getPaddingLeft()+getPaddingRight();
}
if(heightMode==MeasureSpec.EXACTLY){
height=heightSize;
}else{
height=rect.height()+getPaddingTop()+getPaddingBottom();
}
Log.d("MyTestView","width:"+width+",height:"+height);
setMeasuredDimension(width,height);
}
@Override
public void onClick(View view) {
Random random= new Random();
// Toast.makeText(getContext(),"click"+random.nextInt(),Toast.LENGTH_SHORT).show();
text=String.valueOf(Math.abs(random.nextInt()));
mPaint.getTextBounds(text,0,text.length(),rect);
// postInvalidate();
requestLayout();
}
}