我正在尝试将图像从一个 Activity 中的列表转换为一个 Detail Activity。
在细节 Activity 中,图像比列表中的大,我正在使用 Picasso 从服务器检索图像。
问题是我第一次启动细节 Activity 时,图像传输良好但未调整大小也未居中。 当我返回时,图像会立即调整大小,如果我返回相同的细节 Activity ,它会按预期工作。
详细 Activity 调用方法:
public static void launch(Activity activity, View transitionView,
StoreProduct storeProduct) {
ActivityOptionsCompat options =
ActivityOptionsCompat.makeSceneTransitionAnimation(
activity, transitionView, activity.getString(R.string
.transition_product_image));
Intent intent = new Intent(activity, ProductDetailActivity.class);
intent.putExtra(PARAM_STORE_PRODUCT, storeProduct);
ActivityCompat.startActivity(activity, intent, options.toBundle());
}
细节 Activity 中的 picasso 图片加载:
Picasso.with(this).load(product.imageUrl).fit().centerInside()
谢谢你的帮助
最佳答案
尝试根据您的需要使用转换。
Transformation transformation = new Transformation() {
@Override
public Bitmap transform(Bitmap source) {
int targetWidth = context.getResources().getDisplayMetrics().widthPixels - lessWidth;
if (source.getWidth() > targetWidth) {
double aspectRatio = (double) source.getHeight()
/ (double) source.getWidth();
int targetHeight = (int) (targetWidth * aspectRatio);
Bitmap result = Bitmap.createScaledBitmap(source,
targetWidth, targetHeight, false);
if (result != source) {
source.recycle();
}
return result;
}
return source;
}
@Override
public String key() {
return "transformation" + " desiredWidth";
}
};
这个对象可以如下使用:
Picasso.with(context).load(strImageUrl).transform(transformation)
.into(imageView, new com.squareup.picasso.Callback() {
@Override
public void onSuccess() {
Log.d("Picasso", "ImageLoadSuccess");
}
@Override
public void onError() {
Log.d("PicassoHelper", "ImageLoadError");
if (imageView!=null&&defaultBitmap!=null) {
imageView.setImageBitmap(defaultBitmap);
}
}
});
希望这对您的问题有所帮助。
关于Android 与 Picasso 共享元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27720402/