将图像裁剪成圆形
Posted
技术标签:
【中文标题】将图像裁剪成圆形【英文标题】:Cropping image into circle 【发布时间】:2014-06-18 21:06:26 【问题描述】:我想从我的原始图像中裁剪出一个圆形图像。我正在使用 Picasso 库进行图像显示。试过http://yasiradnan.com/circle-transformation-with-android-image-downloading-and-caching-library-picasso/,但它只是将整个图像转换成一个圆形,所以图像变形了。我不想转换图像,我只想用圆形裁剪图像。
【问题讨论】:
我不知道如何使用毕加索,但这可能会对你有所帮助:***.com/questions/12944275/… 【参考方案1】:要完成你想要做的事情,你可以继承ImageView
并使其实现Picasso
的Target
接口。加载位图时,只需使用将位图中心裁剪为正方形,然后将图像着色为圆形的方法。例如:
public class ImageViewTarget extends ImageView implements Target
//constructors
@Override
public void onBitmapFailed(Drawable drawable)
//TODO
@Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom loadFrom)
bitmap = cropCircle(bitmap.isMutable() ? bitmap : bitmap.copy(Config.ARGB_8888, true));
setImageBitmap(bitmap);
@Override
public void onPrepareLoad(Drawable arg0)
//TODO
public Bitmap cropCricle(Bitmap bm)
int width = bm.getWidth();
int height = bm.getHeight();
Bitmap cropped_bitmap;
/* Crop the bitmap so it'll display well as a circle. */
if (width > height)
cropped_bitmap = Bitmap.createBitmap(bm,
(width / 2) - (height / 2), 0, height, height);
else
cropped_bitmap = Bitmap.createBitmap(bm, 0, (height / 2)
- (width / 2), width, width);
BitmapShader shader = new BitmapShader(cropped_bitmap, TileMode.CLAMP, TileMode.CLAMP);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(shader);
height = cropped_bitmap.getHeight();
width = cropped_bitmap.getWidth();
Bitmap mCanvasBitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(mCanvasBitmap);
canvas.drawCircle(width/2, height/2, width/2, paint);
return mCanvasBitmap;
处理cropCircle(Bitmap bitmap);
方法的原因可能更好,但上述方法有时可以优化/构建。
【讨论】:
【参考方案2】:您可以使用以下代码获取圆角位图...这可能对您有帮助 ....
private Bitmap getRoundedCroppedImage(Bitmap bmp)
int widthLight = bmp.getWidth();
int heightLight = bmp.getHeight();
Bitmap output = Bitmap.createBitmap(widthLight, heightLight,Config.ARGB_8888);
Canvas canvas = new Canvas(output);
Paint paint = new Paint();
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
RectF rectF = new RectF(new Rect(0, 0, widthLight, heightLight));
canvas.drawRoundRect(rectF, widthLight / 2 ,heightLight / 2,paint);
Paint paintImage = new Paint();
paintImage.setXfermode(new PorterDuffXfermode(Mode.SRC_ATOP));
canvas.drawBitmap(bmp, 0, 0, paintImage);
return output;
谢谢...
【讨论】:
【参考方案3】:我根据that answer做了一些决定 您可以制作没有库的自定义 ImageView
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
public class CircleImageView extends ImageView
public CircleImageView(Context ctx, AttributeSet attrs)
super(ctx, attrs);
@Override
public void setImageDrawable(Drawable aDrawable)
Bitmap bitmap=getCircleCroppedBitmap(((BitmapDrawable) aDrawable).getBitmap());
super.setImageDrawable(new BitmapDrawable(getResources(),bitmap));
private static Bitmap getCircleCroppedBitmap(Bitmap bitmap)
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
// canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
bitmap.getWidth() / 2, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
//Bitmap _bmp = Bitmap.createScaledBitmap(output, 60, 60, false);
//return _bmp;
return output;
【讨论】:
以上是关于将图像裁剪成圆形的主要内容,如果未能解决你的问题,请参考以下文章