2016-12-26 8 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のようなURLからイメージをロードするとき、私は画像のwidthheightを取得し、それに基づいて、私は、アスペクト比を計算し、このよう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); 

は今、私は正しいとfilenameまたはUriを使用してSimpleDraweeViewにイメージをロードできるようにする必要がありますアスペクト比、どうしたらいいですか?

答えて

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をご覧ください –

関連する問題