如何在不裁剪图像的情况下进行圆转换?我已经尝试了2天而且一直都在失败。这是我到目前为止最接近的,但正如你所看到的,图像不是在圆圈内居中。
@Override
public Bitmap transform(Bitmap source) {
int size = Math.min(source.getWidth(), source.getHeight());
Bitmap squaredBitmap = Bitmap.createScaledBitmap(source,size-50, size-50,true);
if (squaredBitmap != source) {
source.recycle();
}
Bitmap bitmap = Bitmap.createBitmap(size, size
, source.getConfig());
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
BitmapShader shader = new BitmapShader(squaredBitmap,
BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
paint.setShader(shader);
paint.setAntiAlias(true);
float r = size / 2f;
canvas.drawCircle(r, r, r, paint);
squaredBitmap.recycle();
return bitmap;
}
结果如下:
答案 0 :(得分:1)
使用此CircleImageView
:
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/image1"
android:layout_width="96dp"
android:layout_height="96dp"
android:src="@drawable/menu1"
app:border_width="2dp"
app:border_color="@color/white"
/>