Fresco如何獲取圖片的真是寬高比例,實現瀑布流
阿新 • • 發佈:2019-02-01
public class UitlsToos { /** * 通過imageWidth 的寬度,自動適應高度 * * @param simpleDraweeView view * * @param imagePath Uri * * @param imageWidth width */ public static void setControllerListener(final SimpleDraweeView simpleDraweeView, String imagePath, final int imageWidth) { finalViewGroup.LayoutParams layoutParams = simpleDraweeView.getLayoutParams(); ControllerListener controllerListener = new BaseControllerListener<ImageInfo>() { @Override public void onFinalImageSet(String id, @Nullable ImageInfo imageInfo, @Nullable Animatable anim) { if(imageInfo == null) { return; } int height = imageInfo.getHeight(); int width = imageInfo.getWidth(); layoutParams.width = imageWidth; layoutParams.height = (int) ((float) (imageWidth * height) / (float) width); simpleDraweeView.setLayoutParams(layoutParams); } @Override public void onIntermediateImageSet(String id, @Nullable ImageInfo imageInfo) { Log.d("TAG", "Intermediate image received"); } @Override public void onFailure(String id, Throwable throwable) { throwable.printStackTrace(); } }; DraweeController controller = Fresco.newDraweeControllerBuilder().setControllerListener(controllerListener).setUri(Uri.parse(imagePath)).build(); simpleDraweeView.setController(controller); } }