有谁知道如何将图像\位图裁剪成圆圈? 我找不到任何解决方案,抱歉..
答案 0 :(得分:2)
类别:
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" />
答案 1 :(得分:2)
谷歌Android团队的前任工程师罗曼·盖伊发表了一篇关于drawing images with rounded corners的精彩文章。这个想法可以很容易地扩展到一个圆圈,例如,通过改变圆角矩形半径,使它创建一个完整的圆。
来自文章:
要生成圆角图像,我只需编写一个自定义
Drawable
即可 使用Canvas.drawRoundRect()
绘制圆角矩形。诀窍是 使用带有Paint
的{{1}}来填充圆角矩形 纹理而不是简单的颜色。这是代码的样子:BitmapShader
答案 2 :(得分:2)
对于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;
}
答案 3 :(得分:1)
尝试使用以下代码:
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;
}
答案 4 :(得分:1)
Wiseman Designs,有一个开源的Circular ImageView可供使用
https://github.com/wisemandesigns/CircularImageView
这在您的布局中使用XML,这使生活更轻松。您可以使用XML设置源代码,或者通过一些小修改可以轻松使用位图。
免责声明:我为Wiseman Designs工作
答案 5 :(得分:0)
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