有谁知道如何将图像\位图裁剪成圆形? 我找不到任何解决方案,抱歉..在Android中将图像裁剪为圆形
回答
与下面的代码试试:
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;
}
不工作。请建议我。 – Arun
类:
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;
}
查看:
<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"/>
其他样式:
<?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" />
Romain Guy,以前是谷歌Android团队的工程师,发表了一篇关于drawing images with rounded corners的优秀文章。这个想法可以很容易地扩展到一个圆,例如,通过改变圆角的矩形半径,以便创建一个完整的圆。
从文章:
要生成圆形图片,我只是写了一个定制
Drawable
是 绘制使用Canvas.drawRoundRect()
一个圆角矩形。窍门是 使用Paint
与BitmapShader
填充圆角矩形 纹理而不是简单的颜色。下面是代码的样子: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);
Canvas也暴露了一个[drawCircle](http://developer.android.com/reference/android/graphics/Canvas.html)方法,它可能更适合这种情况。 – greg7gkb
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);
}
在这里我找到了一个示例教程在 http://androidgreeve.blogspot.in/2014/09/facebook-messanger-like-profile-image.html?m=1
怀斯曼设计,有一个开放源码的通知ImageView的准备使用
https://github.com/wisemandesigns/CircularImageView
这将在您的布局中使用XML,它让生活更轻松。你可以用XML来设置源代码,或者稍加修改就可以很容易地使用Bitmap。
声明:我怀斯曼工作设计
对于有圆角的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;
}
- 1. 圆形裁剪图像
- 2. 如何裁剪圆形图像openvc android
- 3. 将图像裁剪成圆形
- 4. 如何将图像裁剪成圆形?
- 5. 将图像裁剪为正方形 - Android
- 6. CSS圆形裁剪矩形图像
- 7. imagemagick中的图像圆形裁剪
- 8. 在Android中将很多图片裁剪为圆形
- 9. 如何在Android中以圆形裁剪图像?
- 10. 从原始UIImage中裁剪圆形或椭圆形图像
- 11. Android位图裁剪椭圆形
- 12. 将方形图像裁剪成圆形 - 以编程方式
- 13. 在android中裁剪图像
- 14. 在android中裁剪图像
- 15. Bootstrap 4 - 将图像裁剪成圆形 - 风景照片未被正确裁剪
- 16. 如何在Android中裁剪椭圆形或面部形状的图像?
- 17. 裁剪android图像
- 18. 如何在Titanium中以圆形裁剪图像?
- 19. SVG - 如何将图像裁剪成圆形?
- 20. 将图像裁剪成圆形并添加边框
- 21. 不能将图像裁剪成真实的圆形
- 22. 如何为滑动的圆形裁剪图像制作边框
- 23. android中的图像裁剪
- 24. Android位图:居中裁剪+创建位图的圆形
- 25. 如何将图像裁剪为方形 - android?
- 26. 在matlab中裁剪正方形图像
- 27. 将图像裁剪为矢量或覆盖图形的形状
- 28. 如何在Android中的矩形内裁剪圆形路径
- 29. 在图像视图中通过绘制圆来裁剪图像
- 30. 如何将图像裁剪为最大图像矩形?
@ user1281750阅读问题详细准确!它不重复任何方式! –
可能的重复[裁剪圆形区域从位图在Android](http://stackoverflow.com/questions/11932805/cropping-circular-area-from-bitmap-in-android) – Adinia