如何用Glide库舍入图像?

mr.*_*fox 174 android android-glide

所以,有人知道如何使用Glide显示带圆角的图像吗?我正在使用Glide加载图像,但我不知道如何将舍入的参数传递给此库.

我需要显示图像如下例子:

在此输入图像描述

jim*_*251 443

滑翔V4:

    Glide.with(context)
        .load(url)
        .apply(RequestOptions.circleCropTransform())
        .into(imageView);
Run Code Online (Sandbox Code Playgroud)

Glide V3:

您可以使用RoundedBitmapDrawableGlide的圆形图像.无需自定义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);
        }
    });
Run Code Online (Sandbox Code Playgroud)

  • 设置背景与可绘制的形状椭圆形.并为imageview提供填充以创建边框. (7认同)
  • 但是,这不会创建边框. (5认同)
  • 如果您遇到一些问题,请检查Roman Samoylenko的下一个解决方案. (2认同)

Har*_*han 65

看看这篇文章,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();
    }
} 
Run Code Online (Sandbox Code Playgroud)

  • 有没有办法将转换应用于占位符? (4认同)
  • Stan只对转换ID的要求是它们在所有转换中都是唯一的,因此这里的用法是正确的.缓存键将包括源ID和转换ID,因此转换ID是mixin而不是替换.请参阅https://github.com/bumptech/glide/wiki/Caching-and-Cache-Invalidation#cache-keys (2认同)

Rom*_*nko 35

最简单的方法(需要Glide 4.xx)

Glide.with(context).load(uri).apply(RequestOptions().circleCrop()).into(imageView)
Run Code Online (Sandbox Code Playgroud)

  • @RafaelLima 它是用 Kotlin 编写的。 (3认同)
  • 请注意,“.apply()”位于“.load()”之后。 (2认同)
  • 你需要写 Glide.with(context).load(uri).apply(circleCrop()).into(imageView) 看看 apply 函数。 (2认同)

小智 21

试试这种方式

Glide.with(this)
    .load(R.drawable.thumbnail)
    .bitmapTransform(new CropCircleTransformation(this))
    .into(mProfile);
Run Code Online (Sandbox Code Playgroud)

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"/>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)


Mil*_*ank 9

它非常简单我已经看到Glide库非常好的图书馆和论文基于凌空谷歌的图书馆

使用此库来获得圆形图像视图

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);
}
Run Code Online (Sandbox Code Playgroud)

//列表:

@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;
}
Run Code Online (Sandbox Code Playgroud)

并以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" />
Run Code Online (Sandbox Code Playgroud)

  • 就像之前的回答评论中提到的那样,这种方式也不行.至少对'de.hdodenhof:circleimageview:1.2.2'+'com.github.bumptech.glide:glide:3.5.2'.检查并仔细检查.滑动3.4.+和circleimageview 1.2.1也是同样的问题 (2认同)

Gre*_*nis 9

其他解决方案对我不起作用.我发现他们都有很大的缺点:

  • 使用滑动变换的解决方案不适用于占位符
  • 使用圆形图像视图的解决方案不适用于动画(即交叉渐变)
  • 使用剪辑其子节点的父节点的通用方法(即此处接受的答案)的解决方案滑行时效果不佳

非常有趣的是,在摸索了这个之后,我找到了关于圆角和圆圈Fresco库页面,其中列出了基本相同的限制并以声明结束:

在Android上进行四舍五入是没有真正好的解决方案,必须在上述权衡之间做出选择

令人难以置信的是,目前我们仍然没有真正的解决方案.我有一个基于我上面的链接的替代解决方案.这种方法的缺点是假设你的背景是纯色(角落不是真的透明).你会像这样使用它:

<RoundedCornerLayout ...>
    <ImageView ...>
</RoundedCornerLayout>
Run Code Online (Sandbox Code Playgroud)

这里的要点是完整的代码:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)


Pra*_*sai 7

现在在 Glide V4 中你可以直接使用 CircleCrop()

Glide.with(fragment)
  .load(url)
  .circleCrop()
  .into(imageView);
Run Code Online (Sandbox Code Playgroud)

内置类型

  • 中心裁剪
  • 健身中心
  • 圆形裁剪


Ami*_*emi 6

根据这个答案,两种语言中最简单的方法是:

科特林:

Glide.with(context).load(uri).apply(RequestOptions().circleCrop()).into(imageView)
Run Code Online (Sandbox Code Playgroud)

爪哇:

Glide.with(context).load(uri).apply(new RequestOptions().circleCrop()).into(imageView)
Run Code Online (Sandbox Code Playgroud)

这适用于 Glide 4.XX


小智 5

使用此转换,它将正常工作.

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();
}} 
Run Code Online (Sandbox Code Playgroud)


Bas*_*asi 5

对于Glide 4.xx

Glide
  .with(context)
  .load(uri)
  .apply(
      RequestOptions()
        .circleCrop())
  .into(imageView)
Run Code Online (Sandbox Code Playgroud)

doc它说

圆形图片:CircleImageView / CircularImageView / RoundedImageView已知有问题与TransitionDrawable(.crossFade()与.thumbnail()或.placeholder())和GIF动画,使用 BitmapTransformation(.circleCrop()将在V4使用)或.dontAnimate() 来解决这个问题


小智 5

Roman Samoylenko 的回答是正确的,只是功能发生了变化。正确答案是

Glide.with(context)
                .load(yourImage)
                .apply(RequestOptions.circleCropTransform())
                .into(imageView);
Run Code Online (Sandbox Code Playgroud)