滑行圆角转换问题

tsi*_*iro 25 android

我使用以下代码使用滑动将带圆角的图像加载到imageview中:

Glide.with(this)
                .load(url)
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        return false;
                    }
                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        return false;
                    }
                })
                .transition(withCrossFade())
                .apply(new RequestOptions().transform(new RoundedCorners(50)).error(R.drawable.default_person).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE))
                .into(mBinding.profileImgv);
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述 由于某种原因,图像被像素化.有人能告诉我问题出在哪里吗?

小智 54

我有同样的问题,在我的情况下的问题是我试图加载的图像具有较小的像素(320x480),然后是像素的ImageView大小.我的解决方案如下:

我在xml文件中的ImageView:

    <ImageView
    android:id="@+id/image_program_thumb"
    android:layout_width="match_parent"
    android:layout_height="186dp" />
Run Code Online (Sandbox Code Playgroud)

ProgramViewHolder.java类

@BindView(R.id.image_program_thumb) ImageView mProgramThumbnail;
.....
void bindData(final Program item) {
    RequestOptions requestOptions = new RequestOptions();
    requestOptions = requestOptions.transforms(new CenterCrop(), new RoundedCorners(16));
    Glide.with(itemView.getContext())
            .load(item.getImage())
            .apply(requestOptions)
            .into(mProgramThumbnail);
....
Run Code Online (Sandbox Code Playgroud)

}

PS我使用4.2.0版本的Glide

  • 请提醒一下`transforms()中的参数顺序很重要!`CenterCrop()`必须在`RoundedCorners()`之前完成. (5认同)

Sam*_*der 36

对于 Glide v4.9 转换 (java):

Glide.with(this)
            .load(R.drawable.sample)
            .transform(new CenterCrop(),new RoundedCorners(25))
            .into(image);
Run Code Online (Sandbox Code Playgroud)


Sav*_*vad 29

在Glide V4中

试试这样吧

 Glide.with(this.context)
                .load(url)
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(14)))
                .into(ImageView);
Run Code Online (Sandbox Code Playgroud)

  • 这就是答案,但请确保在您的 ImageView 中,您不能设置 android:scaleType="centerCrop"。否则就行不通。 (9认同)
  • 只有`.apply(RequestOptions.bitmapTransform(new RoundedCorners(14)))`行足以应用角落.无需添加`.apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.ALL))`.谢谢你的回答. (4认同)

zed*_*abs 14

对于 Glide v4.9,以下转换工作正常:

Glide.with(holder.imageView.context)
        .load(myDataset[position])
        .transform(CenterInside(),RoundedCorners(24))
        .into(holder.imageView)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


alg*_*rid 9

这对我有用(生成的 API、Glide 4.9、Kotlin):

GlideApp.with(imageView)
    .load(pictureUri)
    .transform(CenterCrop(), RoundedCorners(8))
    .into(imageView)
Run Code Online (Sandbox Code Playgroud)

在这里找到:https : //github.com/wasabeef/glide-transformations/issues/94


Sir*_*lot 6

正如你在我的回答中看到的,它也可以使用Glide 的 Generated API来实现。它需要一些初始工作,但随后会为您提供 Glide 的所有功能以及做任何事情的灵活性,因为您编写了实际代码,所以我认为从长远来看这是一个很好的解决方案。另外,用法非常简单和整洁。

首先,设置Glide 4+ 版本:

implementation 'com.github.bumptech.glide:glide:4.6.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.6.1'
Run Code Online (Sandbox Code Playgroud)

然后创建 Glide 的 app 模块类来触发注解处理:

@GlideModule
public final class MyAppGlideModule extends AppGlideModule {}
Run Code Online (Sandbox Code Playgroud)

然后创建实际完成工作的 Glide 扩展。您可以自定义它以执行任何您想要的操作:

@GlideExtension
public class MyGlideExtension {

private MyGlideExtension() {}

    @NonNull
    @GlideOption
    public static RequestOptions roundedCorners(RequestOptions options, @NonNull Context context, int cornerRadius) {
        int px = Math.round(cornerRadius * (context.getResources().getDisplayMetrics().xdpi / DisplayMetrics.DENSITY_DEFAULT));
        return options.transforms(new RoundedCorners(px));
    }
}
Run Code Online (Sandbox Code Playgroud)

添加这些文件后,构建您的项目。

然后在您的代码中使用它,如下所示:

GlideApp.with(this)
    .load(imageUrl)
    .roundedCorners(getApplicationContext(), 5)
    .into(imageView);
Run Code Online (Sandbox Code Playgroud)


Nil*_*hod 0

Glide.with(context).load(url).centerCrop().into(new BitmapImageViewTarget(imageView) {
        @Override
        protected void setResource(Bitmap resource) {
            RoundedBitmapDrawable circularBitmapDrawable =
                    RoundedBitmapDrawableFactory.create(context.getResources(), resource);
            circularBitmapDrawable.setCircular(true);
            imageView.setImageDrawable(circularBitmapDrawable);
        }
    });
Run Code Online (Sandbox Code Playgroud)