问题
Hello Picasso+Android Users,
I have used following version of Picasso and its working with following Transformation snippet:
compile 'com.squareup.picasso:picasso:2.3.2'
and created following Transformation for Image Transformation
:
public class ImageTransformation implements Transformation {
int maxWidth;
int maxHeight;
public ImageTransformation(ImageView imageView) {
this.maxWidth = imageView.getWidth();
this.maxHeight = imageView.getHeight();
}
@Override
public Bitmap transform(Bitmap source) {
int targetWidth, targetHeight;
double aspectRatio;
if (source.getWidth() > source.getHeight()) {
targetWidth = maxWidth;
aspectRatio = (double) source.getHeight() / (double) source.getWidth();
targetHeight = (int) (targetWidth * aspectRatio);
} else {
targetHeight = maxHeight;
aspectRatio = (double) source.getWidth() / (double) source.getHeight();
targetWidth = (int) (targetHeight * aspectRatio);
}
Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);
if (result != source) {
source.recycle();
}
return result;
}
@Override
public String key() {
return maxWidth + "x" + maxHeight;
}
}
and Used like:
Picasso.with(mContext).load(photo.getLink())
.error(R.drawable.ic_place_holder_circle)
.placeholder(R.drawable.ic_place_holder_circle)
.transform(new ImageTransformation(holder.albumPhotoDetailSubMainImage))
.into(holder.albumPhotoDetailSubMainImage);
Now i want to use .priority()
which is available in new following version:
compile 'com.squareup.picasso:picasso:2.5.2'
But it is giving me error of Transformation like:
java.lang.IllegalArgumentException: width and height must be > 0
at android.graphics.Bitmap.createBitmap(Bitmap.java:815)
at android.graphics.Bitmap.createBitmap(Bitmap.java:794)
at android.graphics.Bitmap.createBitmap(Bitmap.java:725)
at com.squareup.picasso.BitmapHunter.transformResult(BitmapHunter.java:558)
at com.squareup.picasso.BitmapHunter.hunt(BitmapHunter.java:232)
at com.squareup.picasso.BitmapHunter.run(BitmapHunter.java:159)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
at com.squareup.picasso.Utils$PicassoThread.run(Utils.java:411)
I am getting this error in new version of picasso only.
Is there any solution to solve?
Your help would be appreciated.
回答1:
I think you should use the following instead:
public ImageTransformation(ImageView imageView) {
this.maxWidth = imageView.getLayoutParams().width; //imageView.getWidth();
this.maxHeight = imageView.getLayoutParams().height; //imageView.getHeight();
// check
if (this.maxWidth <= 0) {
this.maxWidth = ...
}
if (this.maxHeight <= 0) {
this.maxHeight = ...
}
}
About the exception, actually, ver 2.3.2 also got it, however, it has different way to process the exception. You can see 2 following screenshots:
Ver 2.3.2:
Ver 2.5.2:
来源:https://stackoverflow.com/questions/34504840/picasso-new-version-2-5-2-not-working-with-transformation