2016-07-23 78 views
1

我在包含圖像的片段中有一個回收站視圖。我實現了OnImageCLickListener,點擊圖像後,打開一個全屏對話框片段並顯示圖像。現在我想實現在回收視圖中的圖像和對話框片段中圖像的全屏對話框之間的共享元素轉換,我還希望在Lollipop前支持共享元素。如何實現前棒棒糖的片段共享元素?

我該怎麼辦?

回答

1

首先,你必須獲得點擊的ImageView的確切位置,把它傳遞給片段: 爲了讓你可以使用這個位置:

int[] location = {0,0}; 
mImageView.getLocationOnScreen(location); 

,並把它傳遞給你的片段,您可以使用此:

Bundle bundle = new Bundle(); 
bundle.putInt("locationX",location[0]); 
bundle.putInt("locationY",location[1]); 

並以此進入您的片段:

locationX = getArguments().getInt("locationX"); 
locationY = getArguments().getInt("locationY"); 

注意:爲了獲得這個位置,不要使用像view.getTop(),view.getRight()等方法。 現在你需要一個簡單的動畫,像這樣在你的片段OnCreateView:

float factor = ((float) (Utils.getWidth(getActivity()))/((float) (pictureWidth))); 

    viewPager.getLayoutParams().height = pictureHeight; 
    viewPager.getLayoutParams().width = pictureWidth; 
    viewPager.setTranslationY(locationY); 
    viewPager.setTranslationX(locationX); 

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) { 
     viewPager.animate() 
       .translationY(Utils.getHeight(getActivity())/2 - pictureHeight/2) 
       .scaleX(factor) 
       .scaleY(factor) 
       .translationX(Utils.getWidth(getActivity())/2 - pictureWidth/2);} 

它還支持預棒棒糖了。