参考了多个大神的博客,自己也就做了个。
自定义一个控件,一般需要准备以下内容
1.设置对应属性
2.继承View(一般情况下,需求不一样也可以是别的)
3.重写几个函数
做完以上三点,基本一个个性化的控件就完成了
先来第一步
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <com.tools.CT android:layout_width="wrap_content" android:layout_height="wrap_content" custom:Ctext="你好" android:padding="10dp" android:layout_centerInParent="true" custom:Ccolor="#555555" custom:Csize="20dp" /> </LinearLayout>其中
com.tools.CT 部分就是自定义控件的布局部分
注意:
xmlns:custom="http://schemas.android.com/apk/res-auto"是必不可少的部分,这也是前提
而com.tools.CT 就是指向 继承View的CT类
package com.tools; //import android.app.Activity; 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.util.AttributeSet; import android.util.TypedValue; import android.view.View; //import android.view.View; //import android.widget.TextView; import com.example.administrator.myapplication.R; import java.util.HashSet; import java.util.Random; import java.util.Set; /** * Created by Administrator on 2015/9/12. */ public class CT extends View{ private String ctext; private int ccolor; private int csize; private Rect mbound; private Paint mpaint; public CT(Context context, AttributeSet attrs){ this(context,attrs,0); } public CT(Context context){ this(context,null); } public CT(Context context,AttributeSet attrs,int style){ super(context,attrs,style); TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CT, style, 0); int n = a.getIndexCount(); for (int i = 0; i < n; i++) { int attr = a.getIndex(i); switch (attr) { case R.styleable.CT_Ctext: ctext = a.getString(attr); break; case R.styleable.CT_Ccolor: // 默认颜色设置为黑色 ccolor = a.getColor(attr, Color.BLACK); break; case R.styleable.CT_Csize: // 默认设置为16sp,TypeValue也可以把sp转化为px csize = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics())); break; } } a.recycle(); /** * 获得绘制文本的宽和高 */ mpaint = new Paint(); mpaint.setTextSize(csize); // mPaint.setColor(mTitleTextColor); mbound = new Rect(); mpaint.getTextBounds(ctext, 0, ctext.length(), mbound); this.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ctext = randomText(); postInvalidate(); } }); } private String randomText(){ Random random = new Random(); Set<Integer> set = new HashSet<Integer>(); while (set.size() < 4) { int randomInt = random.nextInt(10); set.add(randomInt); } StringBuffer sb = new StringBuffer(); for (Integer i : set) { sb.append("" + i); } return sb.toString(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int widthMode = MeasureSpec.getMode(widthMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); int width; int height ; if (widthMode == MeasureSpec.EXACTLY) { width = widthSize; } else { mpaint.setTextSize(csize); mpaint.getTextBounds(ctext, 0, ctext.length(), mbound); float textWidth = mbound.width(); int desired = (int) (getPaddingLeft() + textWidth + getPaddingRight()); width = desired; } if (heightMode == MeasureSpec.EXACTLY) { height = heightSize; } else { mpaint.setTextSize(csize); mpaint.getTextBounds(ctext, 0, ctext.length(), mbound); float textHeight = mbound.height(); int desired = (int) (getPaddingTop() + textHeight + getPaddingBottom()); height = desired; } setMeasuredDimension(width, height); } @Override protected void onDraw(Canvas canvas) { mpaint.setColor(Color.BLUE); canvas.drawRect(0,0,getMeasuredWidth(),getMeasuredHeight(),mpaint); mpaint.setColor(ccolor); canvas.drawText(ctext,getWidth()/2-mbound.width()/2,getHeight()/2+mbound.height()/2,mpaint); } }
重写
onMeasure 和 onDraw 是重写的主要内容,包含着属性的设置
</pre><pre name="code" class="java">
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'宋体';font-size:9.0pt;"><span style="color:#cc7832;">this</span>.setOnClickListener(<span style="color:#cc7832;">new </span>OnClickListener() { <span style="color:#bbb529;">@Override </span><span style="color:#bbb529;"> </span><span style="color:#cc7832;">public void </span><span style="color:#ffc66d;">onClick</span>(View v) { <span style="color:#9876aa;">ctext </span>= randomText()<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>postInvalidate()<span style="color:#cc7832;">; </span><span style="color:#cc7832;"> </span>} })<span style="color:#cc7832;">;</span>其中加入点击监听,以此响应随机函数
进而随机显示内容
从而完成随机按钮