Picasso to view

How to use picasso to crop an image as an image?

By default it seems that it zooms out, so all this shows fits, it seems to stretch it. The centercrop itself breaks. fit centercrop seems the same as just

thanks

+7
android picasso
source share
3 answers

Try using centerCrop()

 Picasso.with(mContext) .load(url) .centerCrop() .resize(yourImageView.getMeasuredWidth(),yourImageView.getMeasuredHeight()) .error(R.drawable.error) .placeholder(R.drawable.blank_img) .into(yourImageView); 

You need to add an addOnPreDrawListener listener, otherwise you will get 0 for the width and height if the image is not drawn. Click here to learn how to use addOnPreDrawListener .

+6
source share

You must call the resize before calling the centerCrop or centerInside () methods, otherwise Picasso will complain about the width / height of the target equal to 0.

 public class MainActivity extends Activity { ImageView imageView; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = (ImageView) findViewById(R.id.imageView); } @Override public void onWindowFocusChanged(boolean hasFocus) { if (hasFocus) { Picasso.with(this) .load("http://i.imgur.com/removed.png") .resize(imageView.getMeasuredWidth(), imageView.getMeasuredHeight()) .centerCrop() // or centerInside() .into(imageView); } super.onWindowFocusChanged(hasFocus); } } 

And here is the figurative image defined in the layout:

  <ImageView android:layout_width="160dp" android:layout_height="160dp" android:id="@+id/imageView" /> 
+3
source share

CenterCrop() is a cropping method that scales the image so that it fills in the requested borders of the ImageView , and then crop the excess. ImageView will fill out completely, but the entire image may not be displayed.

 Picasso .with(context) .load(UsageExampleListViewAdapter.eatFoodyImages[0]) .resize(600, 200) // resizes the image to these dimensions (in pixel) .centerCrop() .into(imageViewResizeCenterCrop); 

CenterInside() is a cropping method that scales the image so that both dimensions are equal to or less than the requested ImageView borders. The image will be displayed in full, but may not fill the entire ImageView .

 Picasso .with(context) .load(UsageExampleListViewAdapter.eatFoodyImages[0]) .resize(600, 200) .centerInside() .into(imageViewResizeCenterInside); 

The options discussed should take into account your needs for image resizing and scaling functionality. There is one final Picasso helper functionality that can be very useful: fit() .

 Picasso .with(context) .load(UsageExampleListViewAdapter.eatFoodyImages[0]) .fit() // call .centerInside() or .centerCrop() to avoid a stretched image .into(imageViewFit); 
+1
source share

All Articles