滑行:加载指定的图像区域

时间:2017-04-18 14:51:50

标签: java android android-glide image-loading

我正在使用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切割所需的图像部分?

1 个答案:

答案 0 :(得分:5)

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

相关问题