2014-12-31 59 views
5

我試圖將圖像從一個活動列表中過渡到一個活動細節。Android與Picasso共享元素

在詳細活動中,圖像比列表中的大,我使用畢加索從服務器檢索圖像。

問題是,我第一次啓動細節活動,圖像過境,但沒有調整大小,也沒有居中。 當我回去時,圖像會立即調整大小,如果我回到相同的細節活動,它會按預期工作。

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()); 
} 

的詳細活動畢加索圖像加載:

Picasso.with(this).load(product.imageUrl).fit().centerInside() 

感謝您的幫助

回答

0

嘗試使用基於您的需要改造

的詳細活動調用方法。

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); 
        }      
       } 
      }); 

希望這會在你的問題有所幫助。

+0

感謝您的建議。我實現了它,但我仍然得到完全相同的結果。儘管由transform方法返回的位圖大小正確,但圖像未被調整大小。 – Leguman

+0

您可以按照您的要求更改** transform **方法中的代碼 –