问题
How do I make Circle Transformation without cropping the image? I've been trying for 2 days and I keep failing. This is the closest I've come so far, but as you can see, the image is not centered inside the circle.
@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;
}
This is the result:
回答1:
Use this 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"
/>
For more information: https://github.com/hdodenhof/CircleImageView
来源:https://stackoverflow.com/questions/45996891/picasso-circle-transformation-without-cropping-image