SimpleDraweeView not resizing after scaling image

2019-01-28 08:27发布

问题:

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

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.

回答1:

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.



回答2:

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()
}