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

问题描述 投票:2回答:1

我使用以下代码使用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个回答
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()操作。如果这影响整体性能,您应该考虑在后台线程中执行此操作。

© www.soinside.com 2019 - 2024. All rights reserved.