Resize image to full width and fixed height with Picasso

后端 未结 2 1060
我在风中等你
我在风中等你 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条回答
  • You are looking for:

    .fit().centerCrop()
    

    What these mean:

    • fit - wait until the ImageView has been measured and resize the image to exactly match its size.
    • centerCrop - scale the image honoring the aspect ratio until it fills the size. Crop either the top and bottom or left and right so it matches the size exactly.
    0 讨论(0)
  • 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);
                }
            });
    
    0 讨论(0)
提交回复
热议问题