4

I'm using fresco to display images in a list view. however i have an issue.

when i scale my image using methods that maintain aspect ratio like CENTER_CROP, and the scaled image height becomes smaller than my simpledraweeview's height, simple-Drawee-View isn't automatically resized to match size of scaled image.

  • I have tried to use android:adjustViewBounds = true But it doesn't work.
  • i have also tried the method below but scaleY which i wanted to use to calculate scaled height returns 1 even when image height is smaller than simpe-drawee-view's height

    float[] f = new float[9]
    draweeView.getImageMatrix().getValues(f);
    float scaleY = f[Matrix.MSCALE_Y];
    Log.d("App","scale y is "+ scaleY);
    

The above codlet is used in the method below

private void loadImage(Uri fileUri,final SimpleDraweeView draweeView,int myImageHeight){

    draweeView.getLayoutParams().width = displayWidth;

    int selectedSize = (myImageHeight > displayWidth)? displayWidth : myImageHeight;

    draweeView.getLayoutParams().height= selectedSize;


    GenericDraweeHierarchyBuilder builder = new GenericDraweeHierarchyBuilder(context.getResources());

    GenericDraweeHierarchy hierarchy = builder
            .setProgressBarImage(new CircleProgressBarDrawable())
            .setActualImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
            .build();


    ImageRequest requestBuilder = ImageRequestBuilder.newBuilderWithSource(fileUri)
            .setProgressiveRenderingEnabled(true)
            .build();

    //ImagePipeline imagePipeline = Fresco.getImagePipeline();

    DraweeController controller = Fresco.newDraweeControllerBuilder()
            .setOldController(draweeView.getController())
            .setImageRequest(requestBuilder)
            .build();
    draweeView.setHierarchy(hierarchy);
    draweeView.setController(controller);

     float[] f = new float[9]
     draweeView.getImageMatrix().getValues(f);
     float scaleY = f[Matrix.MSCALE_Y];
     Log.d("App","scale y is "+ scaleY);
}

RESULTS: red color symbolizes parts of simple drawee view not filled with the image. this is Scale type FIT_CENTER. when i use CENTER_CROP, the image fills the entire imageview and some parts of the image are chopped off and i don't want that

fresco simpe drawee view not resizing

Q: How can i get scaled image size and use it to resize my simpleDraweeView or in other words, how can i make my simpleDraweeView adjust it size ?

i will appreciate any help.

Edijae Crusar
  • 3,473
  • 3
  • 37
  • 74

2 Answers2

7

In order to make simpledraweeview resize, this is what i implemented a ControllerListener and changed simpleDraweeView height as desired.

private void loadImage(Uri fileUri,final SimpleDraweeView simpeDraweeView) {

    GenericDraweeHierarchyBuilder builder = new GenericDraweeHierarchyBuilder(context.getResources());

    GenericDraweeHierarchy hierarchy = builder
            .setProgressBarImage(new CircleProgressBarDrawable())
            .setActualImageScaleType(ScalingUtils.ScaleType.CENTER_CROP)
            .build();


    ImageRequest requestBuilder = ImageRequestBuilder.newBuilderWithSource(fileUri)
            .setProgressiveRenderingEnabled(true)
            .build();


    ControllerListener<ImageInfo> contollerListener = new BaseContollerListener<ImageInfo>() {

    public void onFinalImageSet(String id, ImageInfo imageinfo,Animatable animatable) {

        if(imageinfo != null) {
           updateViewSize(imageinfo)
         }
    }

    DraweeContoller contoller = Fresco.newDraweeContollerBuilder()
                 .setContollerListener(contollerListener)
                 .setImageRequest(requestBuilder)
                 .build();

    simpleDraweeView.setHierarchy(hierarchy);
    simpleDraweeView.setController(contoller);

}

private void updateViewSize(ImageInfo imageinfo){
 //this is my own implementation of changing simple-drawee-view height
 // you canhave yours using imageinfo.getHeight() or imageinfo.getWidth();
 simpleDraweeView.getLayoutParams().height = imageinfo.getHeight();

   // don't forget to call this method. thanks to @plamenko for reminding me.

   simpleDraweeView.requestLayout()
}
itsmysterybox
  • 2,748
  • 3
  • 21
  • 26
Edijae Crusar
  • 3,473
  • 3
  • 37
  • 74
  • I believe you should also call `simpleDraveeView.requestLayout()`.Right now this happens by accident because an invalidate event will follow due to hierarchy chaning its drawable, but it's better to be explicit. – plamenko Jan 26 '16 at 10:37
  • @plamenko it's now even better. thanks for helping me. everything has worked as i wanted. however can i ask you something. is it possible to get the decoded bitmap stored in memory cache or disk cache? if so which place is better to get the bitmap? is it memory or disk cache?. i haven't found any info about this issue in fresco documentation. you see when you use Universal Image Loader, you can be able to achieve this. fresco seems to provide high quality images and i would like to know how i can achieve this. i need the bitmap to show it in another small imageview – Edijae Crusar Jan 29 '16 at 06:14
  • You just use another SimpleDraweeView. If the image is already in cache, it will be fetched from there. – tyronen Feb 02 '16 at 19:53
  • @tyronen so if the image path let's say is `sdcard/pictures/tyronen.jpg` i just pass that file Uri to simpledrawee view. it will take care of checking whether it is in the memory or not. i don't have to manually check it myself. isn't it? – Edijae Crusar Feb 03 '16 at 05:38
  • Correct. All the hassle of checking caches is done by the library. – tyronen Feb 08 '16 at 12:01
3

I strongly recommend that you read the Fresco documentation. All of your questions here are already answered there.

Drawee view does not support ImageView attributes (adjustViewBounds, getImageMatrix, etc.). See the documentation.

Read about intrinsic dimensions, again, in the documentation here.

If you really need to dynamically resize your view, with all of the disadvantages that come with that, you can do that by using a controller listener as explained here.

Hope that helps and let me know should you have any more questions.

Community
  • 1
  • 1
plamenko
  • 1,068
  • 1
  • 8
  • 10
  • this is what i did and it worked. i highly appreciate your help. [the answer below](http://stackoverflow.com/a/34989392/3671509) – Edijae Crusar Jan 25 '16 at 09:47