Crop Image as circle in Android

前端 未结 6 888
不思量自难忘°
不思量自难忘° 2020-12-16 02:30

Does anyone know how to crop an image\\bitmap to a circle? I can not find any solution, sorry ..

6条回答
  •  伪装坚强ぢ
    2020-12-16 03:18

    Romain Guy, formerly an engineer on the Android team at Google, posted an excellent article on drawing images with rounded corners. This idea could be easily extended to a circle, for example, by changing the rounded rectangle radius so that it creates a complete circle.

    From the article:

    To generate the rounded images I simply wrote a custom Drawable that draws a rounded rectangle using Canvas.drawRoundRect(). The trick is to use a Paint with a BitmapShader to fill the rounded rectangle with a texture instead of a simple color. Here is what the code looks like:

    BitmapShader shader; shader = new BitmapShader(bitmap,
    Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    
    Paint paint = new Paint(); paint.setAntiAlias(true);
    paint.setShader(shader);
    
    RectF rect = new RectF(0.0f, 0.0f, width, height);
    
    // rect contains the bounds of the shape
    // radius is the radius in pixels of the rounded corners
    // paint contains the shader that will texture the shape
    canvas.drawRoundRect(rect, radius, radius, paint);
    

提交回复
热议问题