private GenericRequestBuilder buildDraftGlideRequest(Slide slide, MasterSecret masterSecret) { final DrawableTypeRequest<?> request; if (masterSecret == null) request = Glide.with(getContext()).load(slide.getThumbnailUri()); else request = Glide.with(getContext()).load(new DecryptableUri(masterSecret, slide.getThumbnailUri())); return request .transform(new RoundedCorners(getContext(), false, radius, backgroundColorHint)) .listener(new PduThumbnailSetListener(slide.getPart())); }
private PhotoView showGlidePicForUri(final int position, final PhotoView img, Uri uri) { Log.d("", "hc gallery show uri:" + (uri == null ? "null" : uri.toString())); BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inJustDecodeBounds = true; InputStream inputStream = null; try { inputStream = context.getContentResolver().openInputStream(uri); BitmapFactory.decodeStream(inputStream, null, opt); } catch (FileNotFoundException e) { e.printStackTrace(); } // 获取到这个图片的原始宽度和高度 int picWidth = opt.outWidth; int picHeight = opt.outHeight; int newWidth = 0; int newHeight = 0; Log.d("", "preview mms or smms image:wh=" + picWidth + "*" + picHeight); // 读取图片失败时直接返回 if (picWidth == 0 || picHeight == 0 || picWidth == -1 || picHeight == -1) { return img; } int screenH = ScreenUtils.getScreenH(); int screenW = ScreenUtils.getScreenW(); float pic_ratio = (float) picHeight / (float) picWidth; float screen_ratio = (float) screenH / (float) screenW; /* a、先获取图片、屏幕的比例。 b、对比两个比例,图片的比例大就是高可能超出了边界。图片的比例小就是宽可能超出边界。 c、对比比例大的那个边,看是否大于屏幕,没有就用图片自己的大小,如果有就进行各自的缩放。按比例缩放。 */ if (pic_ratio > screen_ratio) { // 高可能超出屏幕高 if (picHeight > screenH) { // 就缩放 newHeight = screenH; newWidth = (picWidth * newHeight) / picHeight; } else { // 没有超就按照原始大小 newHeight = picHeight; newWidth = picWidth; } } else { if (picWidth > screenW) { newWidth = screenW; newHeight = (picHeight * newWidth) / picWidth; } else { newHeight = picHeight; newWidth = picWidth; } } DrawableTypeRequest<Uri> requst = Glide.with(context).loadFromMediaStore(uri); requst .listener( new RequestListener<Uri, GlideDrawable>() { @Override public boolean onException( Exception e, Uri model, Target<GlideDrawable> target, boolean isFirstResource) { setEnableOfButton(position, false); galleryitem_pb.setVisibility(View.GONE); // img.setBackgroundResource(R.drawable.ic_image_failure); return false; } @Override public boolean onResourceReady( GlideDrawable resource, Uri model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) { setEnableOfButton(position, true); // img.setBackgroundResource(0); return false; } }) .dontAnimate() .fitCenter() .diskCacheStrategy(DiskCacheStrategy.NONE); // if (newHeight == picHeight && newWidth == picWidth) { // requst.override(newWidth, newHeight); // } requst.into(img); return img; }