自定义View的多个情况

来源:互联网 发布:log4j 只输出sql 编辑:程序博客网 时间:2024/06/05 15:04
package com.bwie.attrsview;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Path;import android.graphics.Rect;import android.graphics.RectF;import android.graphics.Typeface;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;/** * Created by T_baby on 17/09/28. */public class myview extends View {    private Paint paint;    private String text;    private int color, weight;    private float size;    private Rect rect;    Context context;    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);        paint = new Paint();        TypedArray type = context.obtainStyledAttributes(attrs, R.styleable.mytext, defStyleAttr, 0);        text = type.getString(R.styleable.mytext_mText);        color = type.getColor(R.styleable.mytext_mTextColor, Color.RED);        size = type.getDimension(R.styleable.mytext_mTextSize, 100);        weight = type.getInteger(R.styleable.mytext_mTextweight, 10);        type.recycle();        paint.setTextSize(size);        paint.setColor(color);        //获得文本的宽高        rect = new Rect();        paint.getTextBounds(text, 0, text.length(), rect);    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        canvas.drawColor(Color.GRAY);        //圆角边框        Paint paint2 = new Paint();        //设置圆角空心        paint2.setStyle(Paint.Style.STROKE);        //圆环宽度        paint2.setStrokeWidth(10);        //锯齿        paint2.setAntiAlias(true);        //背景颜色        paint2.setColor(Color.WHITE);        //圆角边框左上右下的坐标        RectF rec = new RectF(canvas.getWidth() / 2 - rect.width() / 2 - 20, canvas.getHeight() / 2 - rect.height(), canvas.getWidth() / 2 + rect.width() / 2 + 20, canvas.getHeight() / 2 + rect.height() * 3 / 2);        //画圆角边框        canvas.drawRoundRect(rec, weight, weight, paint2);        //内圆角边框        paint2.setColor(Color.YELLOW);        //填充        paint2.setStyle(Paint.Style.FILL);        //内圆角边框左上右下的坐标        RectF rec2 = new RectF(canvas.getWidth() / 2 - rect.width() / 2 - 17, canvas.getHeight() / 2 - rect.height() + 3, canvas.getWidth() / 2 + rect.width() / 2 + 17, canvas.getHeight() / 2 + rect.height() * 3 / 2 - 2);        //画内圆角边框        canvas.drawRoundRect(rec2, weight, weight, paint2);        //文字        canvas.drawText(text, canvas.getWidth() / 2 - rect.width() / 2, canvas.getHeight() / 2 + rect.height() / 2, paint);        //矩形        canvas.drawRect(0, 0, 100, 100, paint2);        //        canvas.drawCircle(200, 200, 100, paint2);        //直线        canvas.drawLine(0,250,500,300,paint2);        //弧线,扇形        paint2.setStyle(Paint.Style.STROKE);//设置空心        //范围        RectF oval1=new RectF(0,300,180,480);        //范围,开始角度,划过角度,是否绘制边,画笔        canvas.drawArc(oval1, 0, 120, false, paint2);//弧形        //椭圆        paint2.setStyle(Paint.Style.FILL);        RectF tuoyuan=new RectF(150,0,300,80); //新建一个椭圆外接矩形        canvas.drawOval(tuoyuan, paint2);        //多边形        Path path = new Path();        path.moveTo(380, 200);// 此点为多边形的起点        path.lineTo(520, 350);        path.lineTo(380, 350);        path.close(); // 使这些点构成封闭的多边形        canvas.drawPath(path, paint2);        paint2.setColor(Color.RED);        //画点        canvas.drawPoint(60, 390, paint2);//画一个点        canvas.drawPoints(new float[]{60,400,65,400,70,400}, paint2);        //贝塞尔曲线        Path path2=new Path();        path2.moveTo(100, 320);//设置Path的起点        path2.quadTo(150, 310, 170, 400); //设置贝塞尔曲线的控制点坐标和终点坐标        canvas.drawPath(path2, paint2);//画出贝塞尔曲线        //绘制图片        //画图片,就是贴图        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);        canvas.drawBitmap(bitmap, 250,360, paint2);    }    @Override    public boolean onTouchEvent(MotionEvent event) {        if (event.getAction() == MotionEvent.ACTION_DOWN) {            color = Color.BLACK;            int a = (int) (Math.random() * 10000);            text = a + "";            invalidate();            //设置为粗体            paint.setFakeBoldText(true);            //设置字体对齐            paint.setTextAlign(Paint.Align.LEFT);            //文本缩放            paint.setTextScaleX(3.0f);            //设置字体            paint.setTypeface(Typeface.DEFAULT_BOLD);        }        return super.onTouchEvent(event);    }}
原创粉丝点击