Android method of drawing circle picture

1. Use Xfermode intersection mode

See: http://blog.csdn.net/zcn596785154/article/details/79180145

2. By clipping canvas area

The Canvas class provides clippath, cliprect, clipragion and other methods to crop the Canvas. Through their different combinations, you can get a Canvas of any shape, and then draw a picture on this area to get the View of the corresponding shape. However, using the method of clipping Canvas to achieve circular head image will have sawtooth, and the edge is not as smooth as other methods. The code is as follows:

//To ensure that the drawn View is circular, if the length and width of the picture are inconsistent, the long part will be truncated@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int mSize = Math.min(getMeasuredWidth(), getMeasuredHeight());
    mRadius = mSize / 2;
    setMeasuredDimension(mSize, mSize);
}

@Override
protected void onDraw(Canvas canvas) {
    mPath.addCircle(mRadius, mRadius, mRadius, Path.Direction.CW);
    canvas.clipPath(mPath);
    super.onDraw(canvas);
}

3. Use BitmapShader

Shader is the renderer of paintbrush Paint. In essence, this method is actually to draw circles, only the pictures we set are used for rendering.

BitmapShader is a subclass of Shader, which can be set through Paint.setShader (Shader shader). Then when painting with this Paint, the painting area will be colored according to the TileMode you set. BitmapShader construction method:
mBitmapShader = new BitmapShader(bitmap, TileMode.CLAMP, TileMode.CLAMP);

There are three values for the parameter TileMode:
-CLAMP stretch: the last pixel of the picture is stretched
-REPEAT: REPEAT the bitmap horizontally and vertically
-MIRROR image: turning and repeating horizontally and vertically

The code example is as follows:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    mSize = Math.min(getMeasuredWidth(), getMeasuredHeight());
    mRadius = mSize / 2;
    setMeasuredDimension(mSize, mSize);
}

@Override
protected void onDraw(Canvas canvas) {
    //Get the original bitmap
    Bitmap src = ((BitmapDrawable) getDrawable()).getBitmap();
    if (src == null) {
        super.onDraw(canvas);
        return;
    }
    //Reduce the size of bitmap to the same size as View
    Bitmap newBitmp = Bitmap.createScaledBitmap(src, mSize, mSize, false);
    if (newBitmp == null) {
        return;
    }
    //Set the reduced bitmap to the shader of the brush
    mBitmapShader = new BitmapShader(newBitmp, Shader.TileMode.REPEAT,
            Shader.TileMode.REPEAT);
    //Generate a bitmap for drawing and draw on it with a brush
    Bitmap dest = Bitmap.createBitmap(mSize, mSize, Bitmap.Config.ARGB_8888);
    if (dest == null) {
        return;
    }
    Canvas c = new Canvas(dest);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setShader(mBitmapShader);
    c.drawCircle(mRadius, mRadius, mRadius, paint);
    //Draw the last generated bitmap to the canvas of View
    canvas.drawBitmap(dest, 0, 0, paint);
}

Posted by JVassie on Fri, 01 May 2020 22:30:59 -0700