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

BAr*_*ell 2 java android image-loading android-glide

我使用以下代码使用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);
}
Run Code Online (Sandbox Code Playgroud)

我需要在ImageView中只显示一段图像.这件作品的坐标放在变量x1,x2,y1和y2中.如何使用Glide仅剪切所需的图像部分?

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

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