有人知道如何用Glide显示圆角图像吗? 我正在用Glide加载图像,但我不知道如何将圆角参数传递给这个库。
我需要显示图像像下面的例子:
有人知道如何用Glide显示圆角图像吗? 我正在用Glide加载图像,但我不知道如何将圆角参数传递给这个库。
我需要显示图像像下面的例子:
当前回答
试试这个方法
code
Glide.with(this)
.load(R.drawable.thumbnail)
.bitmapTransform(new CropCircleTransformation(this))
.into(mProfile);
XML
<ImageView
android:id="@+id/img_profile"
android:layout_width="76dp"
android:layout_height="76dp"
android:background="@drawable/all_circle_white_bg"
android:padding="1dp"/>
all_circle_white_bg.xml
<?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>
我用这个变换库。 - > https://github.com/wasabeef/glide-transformations 圆圈描边宽度是ImageView的填充
其他回答
为了Glide 4.x.x
use
Glide
.with(context)
.load(uri)
.apply(
RequestOptions()
.circleCrop())
.into(imageView)
医生说
圆形图片:CircleImageView/CircularImageView/RoundedImageView 已知与TransitionDrawable (.crossFade()有问题 .thumbnail()或.placeholder())和动画gif,使用 BitmapTransformation (.circleCrop()将在v4中可用)或 .dontAnimate()来修复这个问题
private void setContactImage(@NonNull ViewHolder holder, ClsContactDetails clsContactDetails) {
Glide.with(context).load(clsContactDetails.getPic())
.apply(new RequestOptions().centerCrop().circleCrop().placeholder(R.mipmap.ic_launcher)).into(holder.ivPersonImage);
}
这里有一个更模块化和更清晰的方法来在Glide中循环裁剪你的位图:
通过扩展BitmapTransformation创建一个自定义转换,然后重写transform方法,如下所示:
为了Glide 4.x.x
public class CircularTransformation extends BitmapTransformation {
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
RoundedBitmapDrawable circularBitmapDrawable =
RoundedBitmapDrawableFactory.create(null, toTransform);
circularBitmapDrawable.setCircular(true);
Bitmap bitmap = pool.get(outWidth, outHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
circularBitmapDrawable.setBounds(0, 0, outWidth, outHeight);
circularBitmapDrawable.draw(canvas);
return bitmap;
}
@Override
public void updateDiskCacheKey(MessageDigest messageDigest) {}
}
为了Glide 3.x.x
public class CircularTransformation extends BitmapTransformation {
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
RoundedBitmapDrawable circularBitmapDrawable =
RoundedBitmapDrawableFactory.create(null, toTransform);
circularBitmapDrawable.setCircular(true);
Bitmap bitmap = pool.get(outWidth, outHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
circularBitmapDrawable.setBounds(0, 0, outWidth, outHeight);
circularBitmapDrawable.draw(canvas);
return bitmap;
}
@Override
public String getId() {
// Return some id that uniquely identifies your transformation.
return "CircularTransformation";
}
}
然后在Glide builder中设置你需要它的地方:
Glide.with(yourActivity)
.load(yourUrl)
.asBitmap()
.transform(new CircularTransformation())
.into(yourView);
希望这对你有所帮助。
它很简单,我看过Glide图书馆,它非常好的图书馆和论文基础上的volley谷歌的图书馆
使用此库查看圆形图像
https://github.com/hdodenhof/CircleImageView
now
//一个简单的视图
@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" />
Glide.with(MainActivity.this)
.load(personPhoto)
.transition(withCrossFade(500))
.apply(RequestOptions.circleCropTransform())
.thumbnail(0.5f)
.into(imageView);