我正在使用
Glide image loading library,在调整位图大小时我遇到了问题.
使用以下代码时:
Glide.with(getActivity()) .load(images.get(i)) .asBitmap().centerCrop() .into(new SimpleTarget<Bitmap>(1200,1200) { @Override public void onResourceReady(Bitmap resource,GlideAnimation glideAnimation) { } });
每个位图都会调整大小到指定的尺寸.因此,如果图像是400×300,它会升级到1200 x 1200,这是我不想要的.如何使图像小于指定的尺寸,它不会调整大小?
我正在指定尺寸,因为我想要考虑到centerCrop来调整大于指定尺寸的每个图像的大小;然后如果图像小于指定的尺寸,我不希望它的大小调整.
解决方法
I want every image that’s bigger than the specified dimensions to be
resized taking into account centerCrop; and then if the image is
smaller than the specified dimensions,I don’t want it to be resized.
public class CustomCenterCrop extends CenterCrop { public CustomCenterCrop(BitmapPool bitmapPool) { super(bitmapPool); } public CustomCenterCrop(Context context) { super(context); } @Override protected Bitmap transform(BitmapPool pool,Bitmap toTransform,int outWidth,int outHeight) { if (toTransform.getHeight() > outHeight || toTransform.getWidth() > outWidth) { return super.transform(pool,toTransform,outWidth,outHeight); } else { return toTransform; } } }
然后像这样使用它:
Glide.with(getActivity()) .load(images.get(i)) .asBitmap() .transform(new CustomCenterCrop(getActivity())) .into(new SimpleTarget<Bitmap>(1200,GlideAnimation glideAnimation) { } });