安卓Canvas绘制圆角矩形,带文字旋转

项目中一个简单需求,用自定义写了一下,做个笔记。
效果图:
安卓Canvas绘制圆角矩形,带文字旋转_第1张图片

源码:

package com.demo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;


public class MyView extends View {

    private int viewWidth;
    private int viewHeight;

    private Paint paint;

    private TextPaint textPaint;

    private float ascent;
    private float descent;

    private float textOffset;

    private float startX = 0;
    private float startY = 0;
    private float endX = 0;
    private float endY = 0;

    private RectF rectF;

    public MyView(Context context) {
        this(context, null);
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init();
    }


    private void init() {

        Log.e("MyView", "init");

        /*
         * 画笔样式分三种:
         * 1.Paint.Style.STROKE:描边
         * 2.Paint.Style.FILL_AND_STROKE:描边并填充
         * 3.Paint.Style.FILL:填充
         */

        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(5);
        paint.setColor(0xff000000);

        //初始化文字画笔
        textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setTextSize(50);
        textPaint.setColor(0xff000000);
        textPaint.setTextAlign(Paint.Align.CENTER);

        //文字的上坡度和下坡度。用于计算偏移量
        ascent = textPaint.ascent();
        descent = textPaint.descent();

        //偏移量,用于辅助文字居中
        textOffset = (ascent + descent) / 2;

        startX = 10;
        startY = 10;

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        Log.e("MyView", "onSizeChanged");

        viewWidth = w;
        viewHeight = h;

        endX = viewWidth - 10;
        endY = viewHeight - 10;

        rectF = new RectF(startX, startY, endX, endY);
    }


    @Override
    protected void onDraw(Canvas canvas) {

        Log.e("MyView", "onDraw");

        canvas.save();

        // 平移和旋转画布
        canvas.translate(viewWidth / 2, viewHeight / 2 - textOffset);
        canvas.rotate(-30);

        //绘制文字。因为这个时候,画布已经移动到控件中心点了,如果在中心位置写字,字的中心坐标,就是(0,0)
        canvas.drawText("该课程已下架", 0, 0, textPaint);

        // 释放画布
        canvas.restore();

        canvas.drawRoundRect(rectF, 50, 50, paint);

    }
}

最后,布局代码中使用:




    


你可能感兴趣的:(Android笔记)