Glide:加载图像的指定区域

Glide: load specified region of image

我正在使用以下代码使用 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只剪切需要的部分图片?

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()操作。如果这会影响整体性能,您应该考虑在后台线程中执行此操作。