How to draw a circular picture in Android

1. Use Xfermode intersection mode

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

2. By cropping the canvas area

The Canvas class provides ClipPath, ClipRect, ClipRegion and other methods to crop the canvas. Through their different combinations, you can get a canvas of any shape, and then draw on this area to get a View of the corresponding shape. However, using the cropped canvas method to achieve a circular avatar will have jagged edges, and the edges are not as smooth as other methods. code show as below:

//为了保证绘制出来的View为圆形,如果图片的长宽不一致,长的部分会被截断@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 the brush Paint. In essence, this method is actually drawing a circle, but the image we set is used for rendering.

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

There are three values ​​for the parameter TileMode:
- CLAMP stretching: the last pixel of the picture is stretched
- REPEAT: repeating the bitmap
horizontally and vertically - MIRROR mirroring: horizontal and vertical flipping and repeating

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) {
    //得到原bitmap
    Bitmap src = ((BitmapDrawable) getDrawable()).getBitmap();
    if (src == null) {
        super.onDraw(canvas);
        return;
    }
    //把bitmap缩小为和View大小一致
    Bitmap newBitmp = Bitmap.createScaledBitmap(src, mSize, mSize, false);
    if (newBitmp == null) {
        return;
    }
    //将缩小后的bitmap设置为画笔的shader
    mBitmapShader = new BitmapShader(newBitmp, Shader.TileMode.REPEAT,
            Shader.TileMode.REPEAT);
    //生成用来绘图的bitmap,并在其上用画笔绘图
    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);
    //将最后生成的bitmap绘制到View的canvas上
    canvas.drawBitmap(dest, 0, 0, paint);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325773839&siteId=291194637