裁切图像作为圈中的Android [英] Crop Image as circle in Android
问题描述
有谁知道如何裁剪图像\位图转了一圈? 我找不到任何解决方案,抱歉..
罗曼盖伊,以前在Android团队在谷歌一名工程师,贴的绘制一个圆角图片。这个想法可以容易地扩展到一个圆,例如,通过改变圆角矩形半径,使得它创建了一个完整的圆。
从文章:
要生成圆形图像,我只是写了一个自定义的
绘制对象
的 通过绘制一个圆角矩形Canvas.drawRoundRect()
。诀窍是 使用油漆
与BitmapShader
来填充的圆角矩形 纹理,而不是一个简单的色彩。这是什么code是这样的:BitmapShader着色器;着色器=新BitmapShader(位图, Shader.TileMode.CLAMP,Shader.TileMode.CLAMP); 涂料粉刷=新的油漆(); paint.setAntiAlias(真正的); paint.setShader(着色); RectF RECT =新RectF(0.0,0.0,宽,高); //矩形包含形状的边界 //半径在圆角像素半径 //涂料包含将纹理化形状着色器 canvas.drawRoundRect(矩形,半径,半径,油漆);
Does anyone know how to crop an image\bitmap to a circle? I can not find any solution, sorry ..
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 usingCanvas.drawRoundRect()
. The trick is to use aPaint
with aBitmapShader
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);
这篇关于裁切图像作为圈中的Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!