attrs.xml内容
<resources> <declare-styleable name="Myview"> <attr name="textcolor" format="color" /> <attr name="text" format="string" /> <attr name="textsize" format="dimension" /> <attr name="textdrawable" format="reference" /> </declare-styleable> </resources>
自定义view内容
/** * Created by 李韦 on 2016/10/31 14:44 */ public class Mycircle extends View implements View.OnClickListener { private Paint paint; private String text; private int color; private int resourceId; private Bitmap bitmap; private float dimension; private Rect rect; private OnClickListener my; private OnClickListener my1; // private float current_x=4.0f; // private float current_y=4.0f; public Mycircle(Context context) { this(context, null); } public Mycircle(Context context, AttributeSet attrs) { this(context, attrs, 0); } public Mycircle(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.Myview); int indexCount = typedArray.getIndexCount(); for (int i = 0; i < indexCount; i++) { int index = typedArray.getIndex(i); switch (index) { case R.styleable.Myview_text: text = typedArray.getString(R.styleable.Myview_text); break; case R.styleable.Myview_textsize: //这里COMPLEX_UNIT_SP是单位,20是数值,也就是20sp,意为将16sp转换为16px int px = (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()); dimension = typedArray.getDimension(R.styleable.Myview_textsize, px); break; case R.styleable.Myview_textcolor: color = typedArray.getColor(R.styleable.Myview_textcolor, 003344); break; case R.styleable.Myview_textdrawable: resourceId = typedArray.getResourceId(R.styleable.Myview_textdrawable, -1); bitmap = BitmapFactory.decodeResource(getResources(), resourceId); break; } } paint = new Paint(); paint.setAntiAlias(true); paint.setColor(color); paint.setTextSize(dimension); rect = new Rect(); typedArray.recycle(); // this.setOnClickListener(new OnClickListener() { // @Override // public void onClick(View v) { // random(); // // requestLayout(); // postInvalidate(); // // // // } // }); //这个可以执行点击方法 } @Override public void setOnClickListener(OnClickListener l) { my1 = l; super.setOnClickListener(this); } // @Override // public boolean onTouchEvent(MotionEvent event) { // super.onTouchEvent(event); // switch (event.getAction()){ // case MotionEvent.ACTION_DOWN: // float rawX1 = event.getRawX(); // float rawY1 = event.getRawY(); // // break; // case MotionEvent.ACTION_MOVE: // float rawX2 = event.getX(); // float rawY2 = event.getY(); // current_x = rawX2; // current_y = rawY2; // // // break; // case MotionEvent.ACTION_UP: // // // break; // case MotionEvent.ACTION_CANCEL: // // // break; // // // // // // // // } // invalidate(); // // return true; // } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // super.onMeasure(widthMeasureSpec, heightMeasureSpec); // int i = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); // MeasureSpec.getMode(MeasureSpec.AT_MOST)wrap // MeasureSpec.getMode(MeasureSpec.UNSPECIFIED)想多大就多大listview // MeasureSpec.getMode(MeasureSpec.EXACTLY)准确值100dp match int real_width ; int real_height; int width_mode = MeasureSpec.getMode(widthMeasureSpec); int width_size = MeasureSpec.getSize(widthMeasureSpec); int height_mode = MeasureSpec.getMode(heightMeasureSpec); int height_size = MeasureSpec.getSize(heightMeasureSpec); // if(width_mode==MeasureSpec.EXACTLY){ real_width = width_size; }else{ paint.getTextBounds(text,0,text.length(),rect); int width = rect.width(); real_width = getPaddingLeft()+getPaddingRight()+width; } if(height_mode==MeasureSpec.EXACTLY){ real_height = height_size; }else { paint.getTextBounds(text,0,text.length(),rect); int height = rect.height(); real_height = getPaddingBottom()+getPaddingTop()+height; } setMeasuredDimension(real_width,real_height); } //requestlayout onmeasure //invialdate ondraw @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // canvas.drawCircle(current_x, current_y, 10.0f, paint); paint.setColor(Color.RED); canvas.drawRect(0,0,getMeasuredWidth(),getMeasuredHeight(),paint); canvas.drawBitmap(bitmap,getMeasuredWidth()/2-bitmap.getWidth()/2,getMeasuredHeight()/2-bitmap.getHeight()/2,paint); paint.setColor(Color.BLACK); canvas.drawText(text,getMeasuredWidth()/2-rect.width()/2,getMeasuredHeight()/2+rect.height()/2,paint); } public void random(){ StringBuilder stringBuilder = new StringBuilder(); Random random = new Random(); for (int i = 0; i < 4; i++) { int a = random.nextInt(10); stringBuilder.append(a+""); } text=stringBuilder.toString(); } @Override public void onClick(View v) { my1.onClick(v); random(); requestLayout(); invalidate(); }
mainactivity内容
Mycircle circle = (Mycircle) findViewById(R.id.aa); circle.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getApplicationContext(),"adgadg",0).show(); } });