效果如图:
下面根据这个效果讲解一下自定义View的实现:
首先自定义属性,在values文件夹下建立attrs.xml,在这里自定义属性;
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--
format="string" 该属性的取值类型,共有
string,color,demension,integer,enum,reference,float,boolean,fraction,flag
几种类型
-->
<attr name="textstring" format="string"></attr>
<attr name="textcolor" format="color"></attr>
<attr name="textsize" format="dimension"></attr>
<declare-styleable name="MyTextView">
<attr name="textstring"></attr>
<attr name="textcolor"></attr>
<attr name="textsize"></attr>
</declare-styleable>
</resources>
下面是我的布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res/com.sunrui.customview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- 第三行是定义命名空间,后面为包名 -->
<com.sunrui.customview.MyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="12dp"
custom:textstring="6254"
custom:textcolor="#ff0000"
custom:textsize="40sp"/>
<!-- 这里使用了自己的命名空间 -->
</RelativeLayout>
下面是自定义View的一些成员属性
private String mTextString;//文字
private int mTextColor;//文字颜色
private int mTextSize;//字体大小
private Paint mTextPaint;//画笔
private Rect mRect;
需要重写View的构造方法,获取自定义属性
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.getTheme().obtainStyledAttributes(
attrs, R.styleable.MyTextView, 0, 0);
int n = typedArray.getIndexCount();
for (int i = 0; i < n; i++) {
int attr = typedArray.getIndex(i);
switch (attr) {
case R.styleable.MyTextView_textstring:
mTextString = typedArray.getString(attr);
break;
case R.styleable.MyTextView_textcolor:
//设置默认颜色
mTextColor = typedArray.getColor(attr, Color.BLACK);
break;
case R.styleable.MyTextView_textsize:
//字体大小单位布局中指定的是sp,也可以指定为px,dp
mTextSize = typedArray.getDimensionPixelSize(attr,
(int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP, 16, getResources()
.getDisplayMetrics()));
break;
default:
break;
}
}
typedArray.recycle();
mTextPaint = new Paint();
mTextPaint.setTextSize(mTextSize);
mRect = new Rect();
mTextPaint.getTextBounds(mTextString, 0, mTextString.length(), mRect);
//绑定点击事件
this.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//点击后随机改变文字
mTextString = RandomText();
postInvalidate();
}
});
}
private String RandomText(){
StringBuffer sb = new StringBuffer();
Random random = new Random();
List<Integer> set = new ArrayList<Integer>();
for (int i = 0; i < 4; i++) {
set.add(random.nextInt(10));
}
for (Integer i : set) {
sb.append(i+"");
}
return sb.toString();
}
重新ondraw()绘制控件
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mTextPaint.setColor(Color.YELLOW);
canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(),
mTextPaint);
mTextPaint.setColor(mTextColor);
canvas.drawText(mTextString, getWidth() / 2 - mRect.width() / 2,
getHeight() / 2 + mRect.height() / 2, mTextPaint);
mTextPaint.setColor(Color.BLACK);
mTextPaint.setStrokeWidth(2);
canvas.drawLine(0, getRandomY(), getMeasuredWidth(), getRandomY(), mTextPaint);
mTextPaint.setColor(Color.BLUE);
canvas.drawLine(0, getRandomY(), getMeasuredWidth(), getRandomY(), mTextPaint);
mTextPaint.setColor(Color.CYAN);
canvas.drawLine(0, getRandomY(), getMeasuredWidth(), getRandomY(), mTextPaint);
mTextPaint.setColor(Color.DKGRAY);
canvas.drawLine(0, getRandomY(), getMeasuredWidth(), getRandomY(), mTextPaint);
mTextPaint.setColor(Color.GREEN);
canvas.drawLine(0, getRandomY(), getMeasuredWidth(), getRandomY(), mTextPaint);
mTextPaint.setColor(Color.RED);
canvas.drawLine(0, getRandomY(), getMeasuredWidth(), getRandomY(), mTextPaint);
mTextPaint.setColor(Color.YELLOW);
canvas.drawLine(0, getRandomY(), getMeasuredWidth(), getRandomY(), mTextPaint);
}
private int getRandomY(){
Random random = new Random();
return random.nextInt(getMeasuredHeight() + 1);
}
源码地址:http://download.csdn.net/detail/qq_18833399/8527783