自定义View练习2

来源:互联网 发布:淘宝便利店入口 编辑:程序博客网 时间:2024/05/07 05:23
package com.bwie.customview;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Picture;import android.util.AttributeSet;import android.view.View;/** * */public class ExerciseView extends View {    Picture picture = new Picture();    private int mWidth;    private int mHeight;    public ExerciseView(Context context) {        super(context);    }    public ExerciseView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public ExerciseView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);//        recording();//        //1//        picture.draw(canvas);//        //2//        canvas.drawPicture(picture,new RectF(0,0,picture.getWidth(),200));//        //3//        //包装成为Drawable//        PictureDrawable drawable = new PictureDrawable(picture);//        //设置绘制区域。 不会缩放//        drawable.setBounds(0,0,picture.getWidth(),picture.getHeight());//        drawable.draw(canvas);        //bitmap绘制//        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.aaa);//        canvas.drawBitmap(bitmap,new Matrix(),new Paint());//        canvas.drawBitmap(bitmap,200,500,new Paint());//        //显示在中间位置//        canvas.translate(mWidth/2,mHeight/2);//        //指定图片绘制区域//        Rect src = new Rect(0,0,bitmap.getWidth()/2,bitmap.getHeight()/2);//        //指定图片在屏幕上显示的区域//        Rect dst = new Rect(0,0,200,400);//        //绘制图片//        canvas.drawBitmap(bitmap,src,dst,null);//        //绘制文字//        Paint painttext = new Paint();//        painttext.setColor(Color.BLACK);//        painttext.setStyle(Paint.Style.FILL);//        painttext.setTextSize(50);//        String str = "ABCDEFG";//        //参数为  文本,基数x,基数y,画笔////        canvas.drawText(str,200,500,painttext);//        //截取字符串,包左不包右//        canvas.drawText(str,1,3,200,500,painttext);//        //字符数组//        // 参数为 (字符数组 起始坐标 截取长度 基线x 基线y 画笔)//        char[]chars = "ABCDEFG".toCharArray();//        canvas.drawText(chars,1,3,200,500,painttext);//        //给每个字符都指定一个位置//        String str = "SLOOP";//        canvas.drawPosText(str,new float[]{//                100,100,//                200,200,//                300,300,//                400,400,//                500,500,////        },painttext);    }    @Override    protected void onSizeChanged(int w, int h, int oldw, int oldh) {        super.onSizeChanged(w, h, oldw, oldh);        mWidth = w;        mHeight = h;    }    /**     *  录制内容方法     */    private void recording() {        //开始录制        Canvas canvas = picture.beginRecording(500, 500);        //创建一个画笔        Paint paint = new Paint();        paint.setColor(Color.BLUE);        paint.setStyle(Paint.Style.FILL);        //在Canvas中具体操作        //位移        canvas.translate(250,250);        //绘制一个圆        canvas.drawCircle(0,0,100,paint);        picture.endRecording();    }}

Path

package com.bwie.customview;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Path;import android.util.AttributeSet;import android.view.View;/** *  * 路径类 */public class PathView extends View {    private int mWidth;    private int mHeight;    private Paint mpaint;    public PathView(Context context) {        super(context);    }    public PathView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public PathView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        //绘制文字        mpaint = new Paint();        mpaint.setColor(Color.BLACK);        mpaint.setStyle(Paint.Style.STROKE);        mpaint.setTextSize(10);        //        canvas.translate(mWidth/2,mHeight/2);        //        Path path = new Path();        //        path.lineTo(200,200);        //        path.lineTo(200,0);        //        canvas.drawPath(path,mpaint);        //moveTo        //        canvas.translate(mWidth/2,mHeight/2);        //        Path path = new Path();        //        path.lineTo(200,200);        //        //moveTo只改变下次操作的起点        //        path.moveTo(200,100);        //        path.lineTo(200,0);        //        canvas.drawPath(path,mpaint);        //setLastPoint        //重置上一次操作的最后一个点        //        canvas.translate(mWidth/2,mHeight/2);        //        Path path = new Path();        //        path.lineTo(200,200);        //        path.setLastPoint(200,100);        //        path.lineTo(200,0);        //        canvas.drawPath(path,mpaint);        //close        //close的作用是封闭路径,将图形封闭成一个完整的图形        //        canvas.translate(mWidth / 2, mHeight / 2);        //        Path path = new Path();        //        path.lineTo(200,200);        //        path.lineTo(200,0);        //        path.close();        //        canvas.drawPath(path,mpaint);        //addXxx        // 第一类:添加基本图形        //        canvas.translate(mWidth / 2, mHeight / 2);        //        Path path = new Path();        //        //CW顺时针,CCW逆时针        //        path.addRect(-200,-200,200,200, Path.Direction.CW);        //        path.setLastPoint(-300,300);        //        canvas.drawPath(path,mpaint);        //第二类:Path        //        canvas.translate(mWidth / 2, mHeight / 2);        //        canvas.scale(1,-1);//翻转Y坐标轴        //        //        Path path = new Path();        //        Path src = new Path();        //        //        path.addRect(-200,-200,200,200,Path.Direction.CW);        //        src.addCircle(0,0,100, Path.Direction.CW);        //        //包含        //        path.addPath(src,0,200);        //        canvas.drawPath(path,mpaint);        //第三类 addArc 与 arcTo  添加一个圆弧到path        //addArc        //        canvas.translate(mWidth / 2, mHeight / 2);        //        canvas.scale(1, -1);        //        Path path = new Path();        //        path.lineTo(100, 100);        //        //        RectF oval = new RectF(0, 0, 300, 300);        //        path.addArc(oval, 0, 270);        //        //这个变量意思为“是否强制使用moveTo”,也就是说,是否使用moveTo将变量移动到圆弧的起点位移,也就意味着:        //        //        path.arcTo(oval,0,270,true); //与上面一句作用等价        //        canvas.drawPath(path, mpaint);        //isEmpty、 isRect、isConvex、 set 和 offset        //isEmpty  判断path中是否包含内容        //        Path path = new Path();        //        Log.e("1",path.isEmpty()+"");        //        //        path.lineTo(100,100);        //        Log.e("2",path.isEmpty()+"");        //log输出结果:        //        com.sloop.canvas E/1: true        //        com.sloop.canvas E/2: false        //isRect  判断path是否是一个矩形,如果是一个矩形的话,会将矩形的信息存放进参数rect中。        //        Path path = new Path();        //        path.lineTo(0, 400);        //        path.lineTo(400, 400);        //        path.lineTo(400, 0);        //        path.lineTo(0, 0);        //        //        RectF rect = new RectF();        //        boolean b = path.isRect(rect);        //        Log.e("Rect", "isRect:" + b + "| left:" + rect.left + "| top:" + rect.top + "| right:" + rect.right + "| bottom:" + rect.bottom);        //   log 输出结果:        //        com.sloop.canvas E/Rect: isRect:true| left:0.0| top:0.0| right:400.0| bottom:400.0        //set  将新的path赋值到现有path。//        canvas.translate(mWidth / 2, mHeight / 2);  // 移动坐标系到屏幕中心//        canvas.scale(1, -1);                         // <-- 注意 翻转y坐标轴////        Path path = new Path();                     // path添加一个矩形//        path.addRect(-200, -200, 200, 200, Path.Direction.CW);////        Path src = new Path();                      // src添加一个圆//        src.addCircle(0, 0, 100, Path.Direction.CW);////        path.set(src);                              // 大致相当于 path = src;////        canvas.drawPath(path, mpaint);        //offset        canvas.translate(mWidth / 2, mHeight / 2);  // 移动坐标系到屏幕中心        canvas.scale(1,-1);        Path path = new Path();        //添加一个圆形        path.addCircle(0,0,100, Path.Direction.CW);        //添加一个矩形        Path dst = new Path();        dst.addRect(-200,-200,200,200, Path.Direction.CW);        //从运行效果图可以看出,虽然我们在dst中添加了一个矩形,但是并没有表现出来,所以,当dst中存在内容时,dst中原有的内容会被清空,而存放平移后的path        path.offset(300,0,dst);        canvas.drawPath(path,mpaint);        mpaint.setColor(Color.BLUE);        canvas.drawPath(dst,mpaint);    }    @Override    protected void onSizeChanged(int w, int h, int oldw, int oldh) {        super.onSizeChanged(w, h, oldw, oldh);        mWidth = w;        mHeight = h;    }}


import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Path;import android.util.AttributeSet;import android.view.View;/** * Created by 樊健翔 on 2017/9/29. * 创建雷达图 */public class RadarView extends View {    private int count = 6;                //数据个数    private float angle = (float) (Math.PI*2/count);    private float radius;                   //网格最大半径    private int centerX;                  //中心X    private int centerY;                  //中心Y    private String[] titles = {"a","b","c","d","e","f"};    private double[] data = {100,60,60,60,100,50,10,20}; //各维度分值    private float maxValue = 100;             //数据最大值    private Paint mainPaint;                //雷达区画笔    private Paint valuePaint;               //数据区画笔    private Paint textPaint;                //文本画笔    public RadarView(Context context) {        super(context);    }    public RadarView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public RadarView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    @Override    protected void onSizeChanged(int w, int h, int oldw, int oldh) {        radius = Math.min(h, w)/2*0.9f;        //中心坐标        centerX = w/2;        centerY = h/2;        postInvalidate();        super.onSizeChanged(w, h, oldw, oldh);    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        mainPaint = new Paint();        valuePaint = new Paint();        textPaint = new Paint();        mainPaint.setColor(Color.BLACK);        //只描边        mainPaint.setStyle(Paint.Style.STROKE);        //修改宽度        mainPaint.setStrokeWidth(10);        valuePaint.setColor(Color.BLUE);        valuePaint.setStyle(Paint.Style.FILL_AND_STROKE);        textPaint.setTextSize(26);        textPaint.setColor(Color.BLACK);        //调用方法        drawPolygon(canvas);        drawLines(canvas);        drawText(canvas);        drawRegion(canvas);    }    /**     * 绘制正多边形     */    private void drawPolygon(Canvas canvas){        Path path = new Path();        float r = radius / (count - 1);//r是蜘蛛丝之间的间距,每一个六边形之间的间距        for (int i=1;i=0&&angle*i<=Math.PI/2){//第4象限                canvas.drawText(titles[i], x,y,textPaint);            }else if(angle*i>=3*Math.PI/2&&angle*i<=Math.PI*2){//第3象限                canvas.drawText(titles[i], x,y,textPaint);            }else if(angle*i>Math.PI/2&&angle*i<=Math.PI){//第2象限                float dis = textPaint.measureText(titles[i]);//文本长度                canvas.drawText(titles[i], x-dis,y,textPaint);            }else if(angle*i>=Math.PI&&angle*i<3*Math.PI/2){//第1象限                float dis = textPaint.measureText(titles[i]);//文本长度                canvas.drawText(titles[i], x-dis,y,textPaint);            }        }    }    /**     * 绘制区域     * @param canvas     */    private void drawRegion(Canvas canvas){        Path path = new Path();        valuePaint.setAlpha(255);        for(int i=0;i


                                                                            



原创粉丝点击