Android 笔记 :SimpleDraweeView的一些坑

Fresco中文说明:http://www.fresco-cn.org/

Fresco项目GitHub地址:https://github.com/facebook/fresco

1.DraweeView不支持wrap_content

所以动态适应需要拿到高度

解决方法:

 public static void setControllerListener(final SimpleDraweeView simpleDraweeView, String imagePath, final int imageWidth) {
    final ViewGroup.LayoutParams layoutParams = simpleDraweeView.getLayoutParams();
    ControllerListener controllerListener = new BaseControllerListener() {
        @Override
        public void onFinalImageSet(String id, @Nullable ImageInfo imageInfo, @Nullable Animatable anim) {
            if (imageInfo == null) {
                return;
            }
            int height = imageInfo.getHeight();
            int width = imageInfo.getWidth();
            layoutParams.width = imageWidth;
            layoutParams.height = (int) ((float) (imageWidth * height) / (float) width);
            simpleDraweeView.setLayoutParams(layoutParams);
        }

        @Override
        public void onIntermediateImageSet(String id, @Nullable ImageInfo imageInfo) {
            Log.d("TAG", "Intermediate image received");
        }

        @Override
        public void onFailure(String id, Throwable throwable) {
            throwable.printStackTrace();
        }
    };
    DraweeController controller = Fresco.newDraweeControllerBuilder().setControllerListener(controllerListener).setUri(Uri.parse(imagePath)).build();
    simpleDraweeView.setController(controller);
}

getScreenWidth()

 public int getScreenWidth() {
    WindowManager wm = (WindowManager) context
            .getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics outMetrics = new DisplayMetrics();
    wm.getDefaultDisplay().getMetrics(outMetrics);
    return outMetrics.widthPixels;
}

注意: 测试后发现使用的时候 如果 layout_width="match_parent"
要计算上 parent的layout_margin 把getScreenWidth()的宽度减去layout_margin。

2.圆角不显示

按部就班填完属性却发现SimpleDraweeView没有显示出来圆角。

解决办法:

请仔细看一下 命名空间:

 xmlns:fresco="http://schemas.android.com/tools"

改为

 xmlns:fresco="http://schemas.android.com/apk/res-auto"

3.代码动态添加本地资源一般没有问题,网络图片不显示,因为 没有确定宽高,

所以要 加上 宽高,

 SimpleDraweeView head = new SimpleDraweeView(mContext);
  LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(100,100);                 
  head.setLayoutParams(lp);

你可能感兴趣的:(Android 笔记 :SimpleDraweeView的一些坑)