老师,领导要求我在ui不分开切图的情况下,想办法加载巨长图,使页面看起来不要太模糊。
做法如下:
adapter中,
@Override
protected void bindData(@NonNull BaseViewHolder holder, String data) {
ImageView imageView = holder.getViewById(R.id.img_detail);
// imageView.setImageBitmap(data);
Glide.with(context).downloadOnly().load(data).into(new CustomTarget<File>() {
@Override
public void onResourceReady(@NonNull File resource, @Nullable Transition<? super File> transition) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P){
imageView.setImageBitmap(decodeSampledBitmapFromResource(resource.getPath(),300,300));
}else{
imageView.setImageBitmap(decodeSampledBitmapFromResource(resource.getPath(),500,500));
}
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});
}
public static Bitmap decodeSampledBitmapFromResource(String pathName, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(pathName, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(pathName, options);
}
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) >= reqHeight
&& (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
onResourceReady中的处理逻辑不优雅,老师您觉得怎么处理优雅,或者说怎么处理才能更好的解决问题?
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星