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<ImageInfo>() {
        @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);
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Fresco简单的使用—SimpleDraweeView 百学须先立志—学前须知: 在我们平时加载图片(不管是下载...
    天天大保建阅读 3,475评论 0 8
  • 关于网络加载已经写完了,今天来给大家分享一下关于图像加载的知识,在开发中除了请求数据怎么显示之外,剩下的 最...
    deyson阅读 922评论 0 3
  • 世间一切,皆有定数。 所有的艰难,都得走完。 唐僧经历了九九八十一难才取到了真经。 哪有谁的人生会那么轻易简单~
    珠小萌阅读 167评论 0 1
  • 2018处处充满阳光活力 宋燕茹
    寒冰_4932阅读 115评论 0 0
  • 自古文人墨客视“风”为法宝,为我们留下了千古名句:“羌笛何须怨杨柳,春风不度玉门关。”“不知细叶谁裁出,二月春风似...
    雪花飘飘xhpp阅读 536评论 0 6