最近在做一个项目,需要用到一些自定义的图形,在网上找了资料,参考了大神们的思路和做法,自己做了一下总结:
1.首先自定义View要继承View;并实现它的构造方法:
有三个
public CustomView(Context context) { this(context,null); } public CustomView(Context context, AttributeSet attrs) { this(context, attrs,0); } public CustomView(Context context, AttributeSet attrs, int defStyleAttr){super(context, attrs, defStyleAttr);}
2.接下来是定义属性,在value文件下创建attrs文件,并定义属性,比如定义了字体的大小和字体颜色、字体:
<resources> <attr name="customText" format="string"/> <attr name="customTextColor" format="color"/> <attr name="customTextSize" format="dimension"/> <declare-styleable name="CustomView"> <attr name="customText"/> <attr name="customTextColor"/> <attr name="customTextSize"/> </declare-styleable> </resources>
以上的format后面的值为属性的单位
3.在Java类中(即自定义的View)定义属性的值*获取所有自定义属性
TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomView, defStyleAttr, 0);*设置文字的属性
typedArray.getString(attr)
*设置颜色的属性
typedArray.getColor(attr, Color.RED);//默认设为红色*设置大小的属性
typedArray.getDimensionPixelSize(attr, (int) TypedValue .applyDimension(TypedValue .COMPLEX_UNIT_SP,16,getResources() .getDisplayMetrics()));*初始化画笔和绘制的宽高
mPaint = new Paint(); mPaint.setTextSize(mCustomTextSize); // mPaint.setColor(mTitleTextColor); mBound = new Rect(); mPaint.getTextBounds(mCustomText, 0, mCustomText.length(), mBound);4.在onDraw方法作画
mPaint.setColor(Color.RED); canvas.drawCircle(getMeasuredWidth()/2, getMeasuredHeight()/2, getMeasuredHeight()/2, mPaint); mPaint.setColor(mCustomTextColor); canvas.drawText(mCustomText, getWidth() / 2 - mBound.width() / 2, getHeight() / 2 + mBound.height() / 2, mPaint);如果画方形、直线等,则可以分别用canvas.drawRect()、drawLines()、等
还可以设置Paint(画笔)的属性:
setColor()我设置颜色、setStyle()设置风格等;
最后上参照大神们的代码,其中还设置了点击监听
public class CustomView extends View { /** * 文本 */ private String mCustomText; /** * 文本的颜色 */ private int mCustomTextColor; /** * 文本的大小 */ private int mCustomTextSize; /** * 绘制时控制文本绘制的范围 */ private Rect mBound; private Paint mPaint; private boolean isRed=true; public CustomView(Context context) { this(context,null); } public CustomView(Context context, AttributeSet attrs) { this(context, attrs,0); } public CustomView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); /** * 获取所定义的自定义属性 */ TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomView, defStyleAttr, 0); int n = typedArray.getIndexCount(); for (int i = 0; i < n; i++) { int attr = typedArray.getIndex(i); switch (attr){ case R.styleable.CustomView_customText: mCustomText=typedArray.getString(attr); break; case R.styleable.CustomView_customTextColor: mCustomTextColor=typedArray.getColor(attr, Color.RED);//默认设为红色 break; case R.styleable.CustomView_customTextSize: mCustomTextSize=typedArray.getDimensionPixelSize(attr, (int) TypedValue .applyDimension(TypedValue .COMPLEX_UNIT_SP,16,getResources() .getDisplayMetrics())); break; } } typedArray.recycle(); /** * 获得绘制文本的宽和高 */ mPaint = new Paint(); mPaint.setTextSize(mCustomTextSize); // mPaint.setColor(mTitleTextColor); mBound = new Rect(); mPaint.getTextBounds(mCustomText, 0, mCustomText.length(), mBound); /** * 设置监听 */ this.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (isRed){ mPaint.setStyle(Paint.Style.STROKE);//设置空心 mCustomTextColor=Color.RED; Log.i("TEST", "onClick: "+"hahah"); isRed=false; }else { mPaint.setStyle(Paint.Style.FILL);//设置填满 mCustomTextColor=Color.WHITE; isRed=true; } postInvalidate(); } }); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } @Override protected void onDraw(Canvas canvas) { mPaint.setColor(Color.RED); canvas.drawCircle(getMeasuredWidth()/2, getMeasuredHeight()/2, getMeasuredHeight()/2, mPaint); mPaint.setColor(mCustomTextColor); canvas.drawText(mCustomText, getWidth() / 2 - mBound.width() / 2, getHeight() / 2 + mBound.height() / 2, mPaint); } }