【问题标题】:Glide: load specified region of imageGlide:加载图像的指定区域
【发布时间】:2017-04-18 14:51:50
【问题描述】:

我正在使用 Glide 和以下代码从服务器加载图像:

private void loadImage(Context context, String url, ImageView imageView, int x1, int y1, int x2, int y2) {
    Glide.with(context)
       .load(url)
       .into(imageView);
}

我只需要在我的 ImageView 中显示一张图片。这件作品的坐标放置在变量 x1、x2、y1 和 y2 中。如何使用 Glide 只剪切需要的部分?

【问题讨论】:

    标签: java android android-glide image-loading


    【解决方案1】:

    AFAIK,glide 中没有这样的 API。但是您可以手动执行此操作:

    private void loadImage(Context context, String url, ImageView imageView, int x1, int y1, int x2, int y2) {
        Glide.with(context)
           .load(url)
           .asBitmap()
           .into(new SimpleTarget<Bitmap>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
                Bitmap cropeedBitmap = Bitmap.createBitmap(resource, x1, y1, x2, y2);
                imageView.setImageBitmap(cropeedBitmap);
            }
        });
    }
    

    注意,主线程上正在执行一个比较繁重的Bitmap.createBitmap() 操作。如果这会影响整体性能,您应该考虑在后台线程中执行此操作。

    【讨论】:

      猜你喜欢
      • 2016-06-23
      • 2018-09-08
      • 2019-10-13
      • 2016-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多