有人知道如何使用Glide显示圆角图片吗?
我正在使用Glide加载一张图片,但是不知道如何将圆角参数传递给这个库。
我需要像以下示例一样显示图片:
有人知道如何使用Glide显示圆角图片吗?
我正在使用Glide加载一张图片,但是不知道如何将圆角参数传递给这个库。
我需要像以下示例一样显示图片:
Glide V4:
Glide.with(context)
.load(url)
.circleCrop()
.into(imageView);
Glide V3:
您可以使用RoundedBitmapDrawable
和Glide实现圆形图像,不需要自定义ImageView。
Glide.with(context).load(url).asBitmap().centerCrop().into(new BitmapImageViewTarget(imageView) {
@Override
protected void setResource(Bitmap resource) {
RoundedBitmapDrawable circularBitmapDrawable =
RoundedBitmapDrawableFactory.create(context.getResources(), resource);
circularBitmapDrawable.setCircular(true);
imageView.setImageDrawable(circularBitmapDrawable);
}
});
.circleCrop()
代替.centerCrop()
。 - Thanasis Kapelonis看看这篇文章,glide vs picasso...
编辑:链接的文章没有指出这些库中一个重要的区别。Glide 会自动回收。请参阅TWiStErRob 的评论了解更多。
Glide.with(this).load(URL).transform(new CircleTransform(context)).into(imageView);
public static class CircleTransform extends BitmapTransformation {
public CircleTransform(Context context) {
super(context);
}
@Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
return circleCrop(pool, toTransform);
}
private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {
if (source == null) return null;
int size = Math.min(source.getWidth(), source.getHeight());
int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2;
// TODO this could be acquired from the pool too
Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);
Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
if (result == null) {
result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
paint.setAntiAlias(true);
float r = size / 2f;
canvas.drawCircle(r, r, r, paint);
return result;
}
@Override public String getId() {
return getClass().getName();
}
}
public String getId()
,因为它会为所有图像返回相同的ID,这可能会导致Glide在没有转换的情况下设置旧的圆形图像,而正确的图像则不会被设置!我不知道Glide是如何工作的,但似乎它会缓存图像转换(为了避免硬计算)。而ID被用作转换后图像的ID。我在构造函数中添加了一个图像的URL,并使提到的方法返回结果ID,如:this.id = String.format("%s:%s",this.getClass().getSimpleName(),id);
- Stan最简单的方式(需要Glide 4.x.x)
Glide.with(context).load(uri).apply(RequestOptions.circleCropTransform()).into(imageView)
.apply()
在.load()
之后。 - Johnny Five尝试这种方法
Glide.with(this)
.load(R.drawable.thumbnail)
.bitmapTransform(new CropCircleTransformation(this))
.into(mProfile);
<ImageView
android:id="@+id/img_profile"
android:layout_width="76dp"
android:layout_height="76dp"
android:background="@drawable/all_circle_white_bg"
android:padding="1dp"/>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="@android:color/white"/>
</shape>
</item>
</selector>
非常简单,我看过 Glide 库,它是一个非常好的库。同时,也有一个基于 Google 的 Volley 库的文章。
使用这个库来创建圆形图片视图。
https://github.com/hdodenhof/CircleImageView
//对于一个简单的视图:
@Override
public void onCreate(Bundle savedInstanceState) {
...
CircleImageView civProfilePic = (CircleImageView)findViewById(R.id.ivProfile);
Glide.load("http://goo.gl/h8qOq7").into(civProfilePic);
}
//对于列表:
@Override
public View getView(int position, View recycled, ViewGroup container) {
final ImageView myImageView;
if (recycled == null) {
myImageView = (CircleImageView) inflater.inflate(R.layout.my_image_view,
container, false);
} else {
myImageView = (CircleImageView) recycled;
}
String url = myUrls.get(position);
Glide.load(url)
.centerCrop()
.placeholder(R.drawable.loading_spinner)
.animate(R.anim.fade_in)
.into(myImageView);
return myImageView;
}
以及在XML中
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/ivProfile
android:layout_width="160dp"
android:layout_height="160dp"
android:layout_centerInParent="true"
android:src="@drawable/hugh"
app:border_width="2dp"
app:border_color="@color/dark" />
真的很有趣,在瞎摸索后,我发现了关于圆角和圆形的Fresco库页面,其中列出了基本上相同的限制,并得出以下声明:
在Android上没有真正好的圆角解决方案,人们必须在前述的权衡之间选择
难以置信,在现在这个时候我们仍然没有真正的解决方案。我有一个基于我上面提到的链接的备选方案。这种方法的缺点是它假定你的背景是纯色的(边角并不是真正透明的)。你可以像这样使用它:
<RoundedCornerLayout ...>
<ImageView ...>
</RoundedCornerLayout>
要点在这里,完整代码在此处:
public class RoundedCornerLayout extends RelativeLayout {
private Bitmap maskBitmap;
private Paint paint;
private float cornerRadius;
public RoundedCornerLayout(Context context) {
super(context);
init(context, null, 0);
}
public RoundedCornerLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs, 0);
}
public RoundedCornerLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs, defStyle);
}
private void init(Context context, AttributeSet attrs, int defStyle) {
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
setWillNotDraw(false);
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
if (maskBitmap == null) {
// This corner radius assumes the image width == height and you want it to be circular
// Otherwise, customize the radius as needed
cornerRadius = canvas.getWidth() / 2;
maskBitmap = createMask(canvas.getWidth(), canvas.getHeight());
}
canvas.drawBitmap(maskBitmap, 0f, 0f, paint);
}
private Bitmap createMask(int width, int height) {
Bitmap mask = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(mask);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.WHITE); // TODO set your background color as needed
canvas.drawRect(0, 0, width, height, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
canvas.drawRoundRect(new RectF(0, 0, width, height), cornerRadius, cornerRadius, paint);
return mask;
}
}
Glide.with(context).load(uri).apply(RequestOptions().circleCrop()).into(imageView)
Java:
Glide.with(context).load(uri).apply(new RequestOptions().circleCrop()).into(imageView)
这适用于 Glide 4.X.X 版本。
现在在Glide V4中,您可以直接使用CircleCrop()。
Glide.with(fragment)
.load(url)
.circleCrop()
.into(imageView);
内置类型
使用这个转换,它会正常工作。
public class CircleTransform extends BitmapTransformation {
public CircleTransform(Context context) {
super(context);
}
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
return circleCrop(pool, toTransform);
}
private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {
if (source == null) return null;
int borderColor = ColorUtils.setAlphaComponent(Color.WHITE, 0xFF);
int borderRadius = 3;
int size = Math.min(source.getWidth(), source.getHeight());
int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2;
// TODO this could be acquired from the pool too
Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);
if (squared != source) {
source.recycle();
}
Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
if (result == null) {
result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
paint.setAntiAlias(true);
float r = size / 2f;
canvas.drawCircle(r, r, r, paint);
// Prepare the background
Paint paintBg = new Paint();
paintBg.setColor(borderColor);
paintBg.setAntiAlias(true);
// Draw the background circle
canvas.drawCircle(r, r, r, paintBg);
// Draw the image smaller than the background so a little border will be seen
canvas.drawCircle(r, r, r - borderRadius, paint);
squared.recycle();
return result;
}
@Override
public String getId() {
return getClass().getName();
}}
对于 Glide 4.x.x 版本:
使用
Glide
.with(context)
.load(uri)
.apply(
RequestOptions()
.circleCrop())
.into(imageView)
根据 doc 中的说明:
圆形图片:CircleImageView/CircularImageView/RoundedImageView已知在使用TransitionDrawable(.crossFade() with .thumbnail() or .placeholder())和动态GIF时存在问题,建议使用BitmapTransformation(.circleCrop() 将在 v4 版本中提供)或 .dontAnimate() 来解决这个问题。