Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
617 views
in Technique[技术] by (71.8m points)

android - SimpleDraweeView not resizing after scaling image in fresco

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...