[英]Crop Image as circle in Android
Does anyone know how to crop an image\\bitmap to a circle?有谁知道如何将图像\\位图裁剪成圆形? I can not find any solution, sorry ..我找不到任何解决方案,抱歉..
For having rounded corners for ImageView, convert your image into bitmap and then try following code :对于 ImageView 的圆角,将图像转换为位图,然后尝试以下代码:
private Bitmap getRoundedCroppedBitmap(Bitmap bitmap) {
int widthLight = bitmap.getWidth();
int heightLight = bitmap.getHeight();
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
Config.ARGB_8888);
Canvas canvas = new Canvas(output);
Paint paintColor = new Paint();
paintColor.setFlags(Paint.ANTI_ALIAS_FLAG);
RectF rectF = new RectF(new Rect(0, 0, widthLight, heightLight));
canvas.drawRoundRect(rectF, widthLight / 2, heightLight / 2, paintColor);
Paint paintImage = new Paint();
paintImage.setXfermode(new PorterDuffXfermode(Mode.SRC_ATOP));
canvas.drawBitmap(bitmap, 0, 0, paintImage);
return output;
}
Romain Guy, formerly an engineer on the Android team at Google, posted an excellent article on drawing images with rounded corners . Romain Guy 曾是 Google Android 团队的一名工程师,他发表了一篇关于绘制圆角图像的优秀文章。 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()
.为了生成圆角图像,我简单地编写了一个自定义Drawable
,它使用Canvas.drawRoundRect()
绘制一个圆角矩形。 The trick is to use aPaint
with aBitmapShader
to fill the rounded rectangle with a texture instead of a simple color.诀窍是使用带有BitmapShader
的Paint
用纹理而不是简单的颜色填充圆角矩形。 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);
Class:班级:
public Bitmap getRoundedShape(Bitmap scaleBitmapImage) {
int targetWidth = 50;
int targetHeight = 50;
Bitmap targetBitmap = Bitmap.createBitmap(targetWidth,
targetHeight,Bitmap.Config.ARGB_8888);
canvas = new Canvas(targetBitmap);
Path path = new Path();
path.addCircle(((float) targetWidth - 1) / 2,
((float) targetHeight - 1) / 2,
(Math.min(((float) targetWidth),
((float) targetHeight)) / 2),
Path.Direction.CCW);
canvas.clipPath(path);
Bitmap sourceBitmap = scaleBitmapImage;
canvas.drawBitmap(sourceBitmap,
new Rect(0, 0, sourceBitmap.getWidth(),
sourceBitmap.getHeight()),
new Rect(0, 0, targetWidth,
targetHeight), null);
return targetBitmap;
}
View:看法:
<ImageView
android:id="@+id/imgView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/btnEdit"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:background="@drawable/rounded"
android:adjustViewBounds="true"
android:gravity="center"
android:src="@drawable/happy"/>
Additional styles:附加样式:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="@android:color/white" />
<stroke
android:width="3dip"
android:color="#FF0000" />
<corners android:radius="10dp" />
<padding
android:bottom="0dp"
android:left="0dp"
android:right="0dp"
android:top="0dp" />
Wiseman Designs, have an open source Circular ImageView ready for use Wiseman Designs,有一个开源的 Circular ImageView 可供使用
https://github.com/wisemandesigns/CircularImageView https://github.com/wisemandesigns/CircularImageView
This uses XML in your layouts, which makes life easier.这会在您的布局中使用 XML,这让生活更轻松。 You can set the source in XML or with some minor modification could easily use a Bitmap.您可以在 XML 中设置源,或者稍作修改就可以轻松使用位图。
Disclaimer: I work for Wiseman Designs免责声明:我为 Wiseman Designs 工作
Try with below code :尝试使用以下代码:
public Bitmap getRoundedShape(Bitmap scaleBitmapImage) {
// TODO Auto-generated method stub
int targetWidth = 50;
int targetHeight = 50;
Bitmap targetBitmap = Bitmap.createBitmap(targetWidth,
targetHeight,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(targetBitmap);
Path path = new Path();
path.addCircle(((float) targetWidth - 1) / 2,
((float) targetHeight - 1) / 2,
(Math.min(((float) targetWidth),
((float) targetHeight)) / 2),
Path.Direction.CCW);
canvas.clipPath(path);
Bitmap sourceBitmap = scaleBitmapImage;
canvas.drawBitmap(sourceBitmap,
new Rect(0, 0, sourceBitmap.getWidth(),
sourceBitmap.getHeight()),
new Rect(0, 0, targetWidth,
targetHeight), null);
return targetBitmap;
}
finalBitmapShader shader = newBitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
mPaint.setShader(shader);
mBitmapWidth=mBitmap.getWidth();
mBitmapHeight=mBitmap.getHeight();
}
@Override
public void draw(Canvas canvas{
canvas.drawOval(mRectF,mPaint);
}
@Override
protected void onBoundsChange(Rect bounds) {
super.onBoundsChange(bounds);
mRectF.set(bounds);
}
here I found a sample tutorial of it at http://androidgreeve.blogspot.in/2014/09/facebook-messanger-like-profile-image.html?m=1我在这里找到了它的示例教程http://androidgreeve.blogspot.in/2014/09/facebook-messanger-like-profile-image.html?m=1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.