Resize image to full width and fixed height with Picasso

后端 未结 2 1074
我在风中等你
我在风中等你 2020-11-28 01:51

I have a vertical LinearLayout where one of the items is an ImageView loaded using Picasso. I need to rise the image\'s width to the full device width, and to d

2条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-28 02:34

    In some case the fit() is useless. Before you must wait for the width and height measurement to end. So you can use globallayoutlistener. for example;

    imageView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
                public void onGlobalLayout() {
                    Picasso.with(getActivity())
                            .load(imageUrl)
                            .placeholder(R.drawable.placeholder)
                            .error(R.drawable.error)
                            .resize(screenWidth, imageHeight)
                            .fit
                            .centerInside()
                            .into(imageView);
                    imageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                }
            });
    

提交回复
热议问题