有人知道如何用Glide显示圆角图像吗? 我正在用Glide加载图像,但我不知道如何将圆角参数传递给这个库。
我需要显示图像像下面的例子:
有人知道如何用Glide显示圆角图像吗? 我正在用Glide加载图像,但我不知道如何将圆角参数传递给这个库。
我需要显示图像像下面的例子:
当前回答
对我来说;.apply(RequestOptions.circleCropTransform())(4.11)未工作。因为我在用ImageButton。当我用(可点击的)ImageView更改时,它工作了,看起来像我想要的样子。
其他回答
你可以简单地调用roundedcornerstrtransform构造函数,它有一个拐角类型的枚举输入。是这样的:
Glide.with(context)
.load(bizList.get(position).getCover())
.bitmapTransform(new RoundedCornersTransformation(context,20,0, RoundedCornersTransformation.CornerType.TOP))
.into(holder.bizCellCoverImg);
但首先你必须添加滑翔转换到你的项目。
滑翔V4:
Glide.with(context)
.load(url)
.circleCrop()
.into(imageView);
滑翔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);
}
});
您也可以使用这个实现,不过值得注意的是,这个实现是基于匕首柄的
提供程序实现
@Module
@Named("circleCrop")
@InstallIn(SingletonComponent::class)
object AppModule {
@Singleton
@Provides
fun provideGlideInstance(
@ApplicationContext context: Context
) = Glide.with(context).setDefaultRequestOptions(
RequestOptions()
.placeholder(R.drawable.logo)
.error(R.drawable.logo)
.apply(RequestOptions().circleCropTransform())
.diskCacheStrategy(DiskCacheStrategy.DATA)
)
}
依赖注入
@Inject
@Named("circleCrop")
lateinit var glide: RequestManager
加载图片
glide.load(hotel.image).into(imgItemSearch)
我发现了一个简单的解决方案,为imageview添加边界,其中的颜色想要设置或添加渐变图像。
步骤:
取一帧布局并添加两张图像。您可以根据您的要求设置大小。对于imgPlaceHolder,您需要一个想要设置的白色图像或颜色。
<ImageView
android:id="@+id/imgPlaceHolder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:src="@drawable/white_bg"/>
<ImageView
android:id="@+id/imgPic"
android:layout_width="190dp"
android:layout_height="190dp"
android:layout_gravity="center"
android:src="@drawable/image01"/>
</FrameLayout>
After placing this code on xml file , put below line in java file. Glide.with(this).load(R.drawable.image01).asBitmap().centerCrop().into(new BitmapImageViewTarget(imgPic) { @Override protected void setResource(Bitmap resource) { RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), resource); circularBitmapDrawable.setCircular(true); imageView.setImageDrawable(circularBitmapDrawable); } }); Glide.with(this).load(R.drawable.white_bg).asBitmap().centerCrop().into(new BitmapImageViewTarget(imgPlaceHolder) { @Override protected void setResource(Bitmap resource) { RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), resource); circularBitmapDrawable.setCircular(true); imgTemp2.setImageDrawable(circularBitmapDrawable); } });
这将使imageview的边界简单,没有任何额外的填充和边缘。
注意:白色图像是强制性的边界,否则它将不起作用。
快乐编码:)
Glide.with(MainActivity.this)
.load(personPhoto)
.transition(withCrossFade(500))
.apply(RequestOptions.circleCropTransform())
.thumbnail(0.5f)
.into(imageView);