DetailsOverviewSharedElementHelper.java revision b0181a77ce38f75a7d218745a330c5d80daacfb6
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 rightPanel.setVisibility(View.INVISIBLE); 62 } 63 64 @Override 65 public void setSharedElementEnd(List<String> sharedElementNames, 66 List<View> sharedElements, List<View> sharedElementSnapshots) { 67 if (sharedElements.size() < 1) { 68 return; 69 } 70 View overviewView = sharedElements.get(0); 71 if (mViewHolder == null || mViewHolder.mOverviewView != overviewView) { 72 return; 73 } 74 final View rightPanel = mViewHolder.mRightPanel; 75 rightPanel.setVisibility(View.VISIBLE); 76 } 77 78 void setSharedElementEnterTransition(Activity activity, String sharedElementName) { 79 if (activity == null && !TextUtils.isEmpty(sharedElementName) || 80 activity != null && TextUtils.isEmpty(sharedElementName)) { 81 throw new IllegalArgumentException(); 82 } 83 if (activity == mActivityToRunTransition && 84 TextUtils.equals(sharedElementName, mSharedElementName)) { 85 return; 86 } 87 if (mActivityToRunTransition != null) { 88 ActivityCompat.setEnterSharedElementListener(mActivityToRunTransition, null); 89 } 90 mActivityToRunTransition = activity; 91 mSharedElementName = sharedElementName; 92 if (mActivityToRunTransition != null) { 93 ActivityCompat.setEnterSharedElementListener(mActivityToRunTransition, this); 94 ActivityCompat.postponeEnterTransition(mActivityToRunTransition); 95 } 96 } 97 98 void onBindToDrawable(ViewHolder vh) { 99 // After we got a image drawable, we can determine size of right panel. 100 // We want right panel to have fixed size so that the right panel don't change size 101 // when the overview is layout as a small bounds in transition. 102 mViewHolder = vh; 103 mViewHolder.mRightPanel.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { 104 @Override 105 public void onLayoutChange(View v, int left, int top, int right, int bottom, 106 int oldLeft, int oldTop, int oldRight, int oldBottom) { 107 mViewHolder.mRightPanel.removeOnLayoutChangeListener(this); 108 mRightPanelWidth = mViewHolder.mRightPanel.getWidth(); 109 mRightPanelHeight = mViewHolder.mRightPanel.getHeight(); 110 } 111 }); 112 if (mActivityToRunTransition != null) { 113 mViewHolder.mRightPanel.postOnAnimation(new Runnable() { 114 @Override 115 public void run() { 116 if (mActivityToRunTransition == null) { 117 return; 118 } 119 ViewCompat.setTransitionName(mViewHolder.mOverviewView, mSharedElementName); 120 ActivityCompat.startPostponedEnterTransition(mActivityToRunTransition); 121 mActivityToRunTransition = null; 122 mSharedElementName = null; 123 } 124 }); 125 } 126 } 127} 128