欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android中Paint画图(线、矩形、圆形、三角形、旋转文字)

程序员文章站 2023-01-31 12:28:21
线private void drawLine(Canvas canvas, float startX,float startY,float stopX,float stopY, float width,int color){ Paint paint = new Paint(); paint.setAntiAlias(true);//抗锯齿 paint.setStyle(Pai...

线

private void drawLine(Canvas canvas,
                          float startX,float startY,float stopX,float stopY,
                          float width,int color){
    Paint paint = new Paint();
    paint.setAntiAlias(true);//抗锯齿
    paint.setStyle(Paint.Style.STROKE);//不加这个不显示
    paint.setColor(color);
    paint.setStrokeWidth(width);
    Path mPath = new Path();
    mPath.moveTo(startX, startY);
    mPath.lineTo(stopX, stopY);
    mPath.close();



    //***************旋转线
    //1.
    canvas.save();
    canvas.translate(startX, startY);//根据指定坐标旋转
    //水平翻转
    canvas.scale(-1,1);
    //垂直翻转
    canvas.scale(1,-1);
    canvas.translate(-startX, -startY);
    //2.
    canvas.rotate(90, startX, startY);//根据指定角度、坐标来旋转

    canvas.drawPath(mPath, paint);

    canvas.restore();
}

矩形

private void drawRectangle(Canvas canvas,boolean choose,
                               float left,float top,float right,float bottom,int color){
    Paint paint = new Paint();
    paint.setAntiAlias(true);//抗锯齿
    paint.setColor(color);
    if (choose){
        //设置实心矩形
        paint.setStyle(Paint.Style.FILL);
    }else{
       //设置空心矩形
       paint.setStyle(Paint.Style.STROKE);
    }
    //使用画笔在画布上画矩形
    canvas.drawRect(left,top,right,bottom,paint);
}

圆形

private void drawYuan(Canvas canvas,boolean choose,
                          float startX,float startY,float radius,int color){
    Paint paint = new Paint();
    paint.setAntiAlias(true);//抗锯齿
    paint.setColor(color);
    if (choose){
       //设置实心圆
       paint.setStyle(Paint.Style.FILL);
    }else{
       //设置空心圆
       paint.setStyle(Paint.Style.STROKE);
    }
    //X、Y坐标,radius半径
   canvas.drawCircle(startX, startY, radius, paint);
}

三角形

private void drawTriangle(Canvas canvas,boolean choose,
                              float startX,float startY,
                              float afterX,float afterY,
                              float stopX,float stopY,
                              float width,int color){
    Paint paint = new Paint();
    paint.setAntiAlias(true);//抗锯齿
    paint.setStrokeWidth(width);
    paint.setColor(color);
    if (choose){
       //设置实心三角形
       paint.setStyle(Paint.Style.FILL);
    }else{
       //设置空心三角形
       paint.setStyle(Paint.Style.STROKE);
    }
    Path mPath = new Path();
    mPath.moveTo(startX, startY);
    mPath.lineTo(afterX, afterY);
    mPath.lineTo(stopX, stopY);
    //将mpath封闭,也可以写 mpath.lineTo(100, 100);代替
    mPath.close();
    //绘制path路径
    canvas.drawPath(mPath, paint);
}

旋转文字

private void drawText(Canvas canvas,String str,int size,float degrees,float startX,float             
                     startY,int color){
    Paint paint = new Paint();
    paint.setAntiAlias(true);//抗锯齿        
    paint.setColor(color);
    paint.setTypeface(Typeface.DEFAULT_BOLD);//字体
    Typeface font = Typeface.create(Typeface.SANS_SERIF, Typeface.NORMAL);//字体风格
    paint.setTypeface(font);
    paint.setTextSize(size);
    //Paint设置水平居中
    paint.setTextAlign(Paint.Align.CENTER);
    float length = paint.measureText(str);//获取字体的长度
    float ncenterx = startX + (size - length) / 2;
    if (degrees!=0){
       //以指定坐标点旋转指定角度
       canvas.rotate(degrees, ncenterx, startY);
     }
     canvas.drawText(str, ncenterx,startY,paint);
}

 

本文地址:https://blog.csdn.net/aidou1314/article/details/108993121