布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
xmlns:openxu="http://schemas.android.com/apk/res-auto"
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="com.bwie.test.myview.MainActivity">
<com.bwie.test.myview.MyView
android:layout_width="200dip"
android:layout_height="100dip"
openxu:mTextSize="25sp"
openxu:mText="自定义属性"
openxu:mTextColor="#0000ff"
android:background="#f00"
/>
</RelativeLayout>
别忘了写这句,否则不可以调用自定义的属性值
xmlns:openxu="http://schemas.android.com/apk/res-auto"
在Values文件夹下创建attrs.xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="mText" format="string"/>
<attr name="mTextColor" format="color" />
<attr name="mTextSize" format="dimension" />
<declare-styleable name="MyView">
<attr name="mText"/>
<attr name="mTextColor"/>
<attr name="mTextSize"/>
</declare-styleable>
</resources>
自定义Class类继承View,实现的代码:
public class MyView extends View{
/*文字*/
private String mText;
private int mTextColor;
private int mTextSize;
// 绘制文本范围
private Rect mbound;
private Paint mPaint;
public MyView(Context context) {
this(context,null);
}
public MyView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
/*构造方法里边实例化画笔*/
public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
/* // 自定义布局
mText="文字";
mTextColor= Color.BLACK;
mTextSize=100;
mPaint = new Paint();
mPaint.setTextSize(mTextSize);
mPaint.setColor(mTextColor);
*//* 获得绘制文本的宽s和高*//*
mbound = new Rect();
mPaint.getTextBounds(mText,0,mText.length(),mbound);*/
//获取自定义属性的值
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MyView, defStyleAttr, 0);
mText=a.getString(R.styleable.MyView_mText);
mTextColor=a.getColor(R.styleable.MyView_mTextColor,Color.BLACK);
// mTextSize=(int) a.getDimension(R.styleable.MyView_mTextSize,100); // int,float----可以强转为int
mTextSize=a.getDimensionPixelSize(R.styleable.MyView_mTextSize,100); //int ,int
a.recycle();//回收
mPaint = new Paint();
mPaint.setTextSize(mTextSize);
mPaint.setColor(mTextColor);
// 获得绘制文本的宽和高
mbound = new Rect();
mPaint.getTextBounds(mText,0,mText.length(),mbound);
}
public MyView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
/*测量*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
/*绘制*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
/* 绘制文字*/
canvas.drawText(mText,getWidth()/2-mbound.width()/2,getHeight()/2-mbound.height()/2,mPaint);
}
//定位
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
}
/*点击*/
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_DOWN:
/*随机数*/
float a=(float) (Math.random()*10000);
mText=""+a;
invalidate(); //更新视图
break;
case MotionEvent.ACTION_UP:
Log.d("main","抬起");
break;
}
return super.onTouchEvent(event);
}
最后的效果图