2016-12-26 23 views
1

我必须从URL或文件名/ Uri中将图像加载到SimpleDraweeView使用Fresco从文件中以正确的高宽比加载图像

这是我的xml布局:

<com.facebook.drawee.view.SimpleDraweeView 
        android:id="@+id/image" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        fresco:actualImageScaleType="centerCrop" 
        fresco:placeholderImage="@color/wait_color" 
        fresco:placeholderImageScaleType="centerCrop" 
        fresco:roundTopLeft="true" 
        fresco:roundTopRight="true" 
        fresco:roundBottomLeft="false" 
        fresco:roundBottomRight="false" 
        fresco:roundedCornerRadius="3dp" /> 

当我加载从网址诸如Facebook或Instagram的图像时,得到width和图像的height并且基于这一点,我计算纵横比和其设置为SimpleDraweeView这样的:

imageUrl = intent.getExtras().getString(KEY_IMAGE_URL); 

       int width = intent.getExtras().getInt(KEY_WIDTH); 
       int height = intent.getExtras().getInt(KEY_HEIGHT); 

       float aspectRatio = (float) width/height; 

       image.setAspectRatio(aspectRatio); 

       Uri uri = Uri.parse(imageUrl); 
       ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri) 
         .setProgressiveRenderingEnabled(true) 
         .setAutoRotateEnabled(true) 
         .build(); 
       DraweeController controller = Fresco.newDraweeControllerBuilder() 
         .setImageRequest(request) 
         .setOldController(image.getController()) 
         .build(); 
       image.setController(controller); 

现在,我需要能够加载图像到SimpleDraweeView使用filenameUri与正确长宽比,我该怎么做?

回答

0

一般来说,我们不鼓励人们使用这种方法,因为Drawee可能会显示超过1个事物并且没有真正的固有尺寸。 Read more about the reasons why wrap_content is not supported here。这个页面在最后一段还有一个链接,如何一个controller listener can be used for this。 但是,由于Fresco文档中列出的所有问题,考虑采用不同的方法可能会更好。例如,固定大小和适当的比例类型可以更好地工作。

0

试试这个:
设置adjustViewBounds = true

-1

解决它。刚刚获得图像的宽度和高度并计算纵横比。

filename = intent.getExtras().getString(KEY_FILE); 
      if (!TextUtils.isEmpty(filename)) { 
       BitmapFactory.Options options = new BitmapFactory.Options(); 
       options.inJustDecodeBounds = true; 
       BitmapFactory.decodeFile(filename, options); 
       int height = options.outHeight; 
       int width = options.outWidth; 

       f = new File(filename); 
       Uri uri = Uri.fromFile(f); 

       float aspectRatio = (float) width/height; 

       image.setAspectRatio(aspectRatio); 
       ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri) 
         .setProgressiveRenderingEnabled(true) 
         .setAutoRotateEnabled(true) 
         .build(); 
       DraweeController controller = Fresco.newDraweeControllerBuilder() 
         .setImageRequest(request) 
         .setOldController(image.getController()) 
         .build(); 
       image.setController(controller); 
      } 
+0

这比使用像我在我的解决方案中建议的'ControllerListener'更低效。看看https://stackoverflow.com/questions/33955510/facebook-fresco-using-wrap-conent/34075281#34075281 –