DetailsOverviewSharedElementHelper.java revision 72a2146f4c3e6dbb84a5f9f92e7ab42d142dab04
1/* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14package android.support.v17.leanback.widget; 15 16import android.support.v4.app.ActivityCompat; 17import android.support.v4.app.SharedElementListener; 18import android.support.v4.view.ViewCompat; 19import android.support.v17.leanback.widget.DetailsOverviewRowPresenter.ViewHolder; 20 21import android.app.Activity; 22import android.text.TextUtils; 23import android.view.View; 24import android.view.View.MeasureSpec; 25 26import java.util.List; 27 28final class DetailsOverviewSharedElementHelper extends SharedElementListener { 29 30 private ViewHolder mViewHolder; 31 private Activity mActivityToRunTransition; 32 private String mSharedElementName; 33 private int mRightPanelWidth; 34 private int mRightPanelHeight; 35 36 @Override 37 public void setSharedElementStart(List<String> sharedElementNames, 38 List<View> sharedElements, List<View> sharedElementSnapshots) { 39 if (sharedElements.size() < 1) { 40 return; 41 } 42 View overviewView = sharedElements.get(0); 43 if (mViewHolder == null || mViewHolder.mOverviewView != overviewView) { 44 return; 45 } 46 View imageView = mViewHolder.mImageView; 47 final int width = overviewView.getWidth(); 48 final int height = overviewView.getHeight(); 49 imageView.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), 50 MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)); 51 imageView.layout(0, 0, width, height); 52 final View rightPanel = mViewHolder.mRightPanel; 53 if (mRightPanelWidth != 0 && mRightPanelHeight != 0) { 54 rightPanel.measure(MeasureSpec.makeMeasureSpec(mRightPanelWidth, MeasureSpec.EXACTLY), 55 MeasureSpec.makeMeasureSpec(mRightPanelHeight, MeasureSpec.EXACTLY)); 56 rightPanel.layout(width, rightPanel.getTop(), width + mRightPanelWidth, 57 rightPanel.getTop() + mRightPanelHeight); 58 } else { 59 rightPanel.offsetLeftAndRight(width - rightPanel.getLeft()); 60 } 61 mViewHolder.mActionsRow.setVisibility(View.INVISIBLE); 62 mViewHolder.mDetailsDescriptionFrame.setVisibility(View.INVISIBLE); 63 } 64 65 @Override 66 public void setSharedElementEnd(List<String> sharedElementNames, 67 List<View> sharedElements, List<View> sharedElementSnapshots) { 68 if (sharedElements.size() < 1) { 69 return; 70 } 71 View overviewView = sharedElements.get(0); 72 if (mViewHolder == null || mViewHolder.mOverviewView != overviewView) { 73 return; 74 } 75 mViewHolder.mActionsRow.setVisibility(View.VISIBLE); 76 mViewHolder.mDetailsDescriptionFrame.setVisibility(View.VISIBLE); 77 } 78 79 void setSharedElementEnterTransition(Activity activity, String sharedElementName) { 80 if (activity == null && !TextUtils.isEmpty(sharedElementName) || 81 activity != null && TextUtils.isEmpty(sharedElementName)) { 82 throw new IllegalArgumentException(); 83 } 84 if (activity == mActivityToRunTransition && 85 TextUtils.equals(sharedElementName, mSharedElementName)) { 86 return; 87 } 88 if (mActivityToRunTransition != null) { 89 ActivityCompat.setEnterSharedElementListener(mActivityToRunTransition, null); 90 } 91 mActivityToRunTransition = activity; 92 mSharedElementName = sharedElementName; 93 if (mActivityToRunTransition != null) { 94 ActivityCompat.setEnterSharedElementListener(mActivityToRunTransition, this); 95 ActivityCompat.postponeEnterTransition(mActivityToRunTransition); 96 } 97 } 98 99 void onBindToDrawable(ViewHolder vh) { 100 // After we got a image drawable, we can determine size of right panel. 101 // We want right panel to have fixed size so that the right panel don't change size 102 // when the overview is layout as a small bounds in transition. 103 mViewHolder = vh; 104 mViewHolder.mRightPanel.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { 105 @Override 106 public void onLayoutChange(View v, int left, int top, int right, int bottom, 107 int oldLeft, int oldTop, int oldRight, int oldBottom) { 108 mViewHolder.mRightPanel.removeOnLayoutChangeListener(this); 109 mRightPanelWidth = mViewHolder.mRightPanel.getWidth(); 110 mRightPanelHeight = mViewHolder.mRightPanel.getHeight(); 111 } 112 }); 113 if (mActivityToRunTransition != null) { 114 mViewHolder.mRightPanel.postOnAnimation(new Runnable() { 115 @Override 116 public void run() { 117 if (mActivityToRunTransition == null) { 118 return; 119 } 120 ViewCompat.setTransitionName(mViewHolder.mOverviewView, mSharedElementName); 121 ActivityCompat.startPostponedEnterTransition(mActivityToRunTransition); 122 mActivityToRunTransition = null; 123 mSharedElementName = null; 124 } 125 }); 126 } 127 } 128} 129