在壁画中缩放图像后,SimpleDraweeView不会调整大小

时间:2016-01-19 10:32:54

标签: android facebook image fresco

我使用壁画在列表视图中显示图像。但是我有一个问题。

当我使用保持宽高比如CENTER_CROP的方法缩放我的图像,并且缩放的图像高度变得小于我的simpledraweeview的高度时,简单 - Drawee-View不会自动调整大小以匹配缩放图像的大小。

  • 我尝试使用android:adjustViewBounds = true 但它不起作用。
  • 我也尝试了下面的方法但是我想用来计算缩放高度的scaleY 返回 1 ,即使图像高度小于simpe-drawee - 观看高度

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

以上方法用于以下方法

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

结果:红色表示未填充图像的简单drawee视图的部分。这是Scale类型FIT_CENTER。当我使用CENTER_CROP时,图像会填满整个图像视图,图像的某些部分会被切掉,我不想要

fresco simpe drawee view not resizing

问:我如何获得缩放的图像大小并使用它来调整我的simpleDraweeView的大小,换句话说,我如何让我的simpleDraweeView调整它的大小?

我将不胜感激任何帮助。

2 个答案:

答案 0 :(得分:3)

我强烈建议您阅读Fresco文档。这里的所有问题都已在那里得到解答。

Drawee视图不支持ImageView属性(adjustViewBounds,getImageMatrix等)。请参阅documentation

再次阅读文档here中的内在维度。

如果您确实需要动态调整视图大小,并且具有所带来的所有缺点,您可以使用控制器侦听器执行此操作,如here所述。

如果您有任何疑问,希望有帮助并告诉我。

答案 1 :(得分:3)

为了使simpledraweeview调整大小,我实现了ControllerListener并根据需要更改了simpleDraweeView高度。

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