FocusHighlightHelper.java revision 7c004076d5289caa0af7b5fb04cf7a3374be56b1
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.graphics.drawable.TransitionDrawable; 17import android.support.v17.leanback.R; 18import android.view.View; 19import android.view.ViewGroup; 20import android.view.animation.AccelerateDecelerateInterpolator; 21import android.view.animation.Interpolator; 22import android.animation.TimeAnimator; 23import android.content.res.Resources; 24 25import static android.support.v17.leanback.widget.FocusHighlight.ZOOM_FACTOR_NONE; 26import static android.support.v17.leanback.widget.FocusHighlight.ZOOM_FACTOR_SMALL; 27import static android.support.v17.leanback.widget.FocusHighlight.ZOOM_FACTOR_MEDIUM; 28import static android.support.v17.leanback.widget.FocusHighlight.ZOOM_FACTOR_LARGE; 29 30 31/** 32 * Setup the behavior how to highlight when a item gains focus. 33 */ 34public class FocusHighlightHelper { 35 36 static class FocusAnimator implements TimeAnimator.TimeListener { 37 private final View mView; 38 private final int mDuration; 39 private final ShadowOverlayContainer mWrapper; 40 private final float mScaleDiff; 41 private float mFocusLevel = 0f; 42 private float mFocusLevelStart; 43 private float mFocusLevelDelta; 44 private final TimeAnimator mAnimator = new TimeAnimator(); 45 private final Interpolator mInterpolator = new AccelerateDecelerateInterpolator(); 46 47 void animateFocus(boolean select, boolean immediate) { 48 endAnimation(); 49 final float end = select ? 1 : 0; 50 if (immediate) { 51 setFocusLevel(end); 52 } else if (mFocusLevel != end) { 53 mFocusLevelStart = mFocusLevel; 54 mFocusLevelDelta = end - mFocusLevelStart; 55 mAnimator.start(); 56 } 57 } 58 59 FocusAnimator(View view, float scale, int duration) { 60 mView = view; 61 mDuration = duration; 62 mScaleDiff = scale - 1f; 63 if (view instanceof ShadowOverlayContainer) { 64 mWrapper = (ShadowOverlayContainer) view; 65 } else { 66 mWrapper = null; 67 } 68 mAnimator.setTimeListener(this); 69 } 70 71 void setFocusLevel(float level) { 72 mFocusLevel = level; 73 float scale = 1f + mScaleDiff * level; 74 mView.setScaleX(scale); 75 mView.setScaleY(scale); 76 if (mWrapper != null) { 77 mWrapper.setShadowFocusLevel(level); 78 } 79 } 80 81 float getFocusLevel() { 82 return mFocusLevel; 83 } 84 85 void endAnimation() { 86 mAnimator.end(); 87 } 88 89 @Override 90 public void onTimeUpdate(TimeAnimator animation, long totalTime, long deltaTime) { 91 float fraction; 92 if (totalTime >= mDuration) { 93 fraction = 1; 94 mAnimator.end(); 95 } else { 96 fraction = (float) (totalTime / (double) mDuration); 97 } 98 if (mInterpolator != null) { 99 fraction = mInterpolator.getInterpolation(fraction); 100 } 101 setFocusLevel(mFocusLevelStart + fraction * mFocusLevelDelta); 102 } 103 } 104 105 static class BrowseItemFocusHighlight implements FocusHighlight { 106 private static final int DURATION_MS = 150; 107 108 private static float[] sScaleFactor = new float[4]; 109 110 private int mScaleIndex; 111 112 BrowseItemFocusHighlight(int zoomIndex) { 113 mScaleIndex = (zoomIndex >= 0 && zoomIndex < sScaleFactor.length) ? 114 zoomIndex : ZOOM_FACTOR_MEDIUM; 115 } 116 117 private static void lazyInit(Resources resources) { 118 if (sScaleFactor[ZOOM_FACTOR_NONE] == 0f) { 119 sScaleFactor[ZOOM_FACTOR_NONE] = 1f; 120 sScaleFactor[ZOOM_FACTOR_SMALL] = 121 resources.getFraction(R.fraction.lb_focus_zoom_factor_small, 1, 1); 122 sScaleFactor[ZOOM_FACTOR_MEDIUM] = 123 resources.getFraction(R.fraction.lb_focus_zoom_factor_medium, 1, 1); 124 sScaleFactor[ZOOM_FACTOR_LARGE] = 125 resources.getFraction(R.fraction.lb_focus_zoom_factor_large, 1, 1); 126 } 127 } 128 129 private float getScale(View view) { 130 lazyInit(view.getResources()); 131 return sScaleFactor[mScaleIndex]; 132 } 133 134 private void viewFocused(View view, boolean hasFocus) { 135 view.setSelected(hasFocus); 136 FocusAnimator animator = (FocusAnimator) view.getTag(R.id.lb_focus_animator); 137 if (animator == null) { 138 animator = new FocusAnimator(view, getScale(view), DURATION_MS); 139 view.setTag(R.id.lb_focus_animator, animator); 140 } 141 animator.animateFocus(hasFocus, false); 142 } 143 144 @Override 145 public void onItemFocused(View view, boolean hasFocus) { 146 viewFocused(view, hasFocus); 147 } 148 } 149 150 /** 151 * Setup the focus highlight behavior of a focused item in browse list row. 152 * @param adapter adapter of the list row. 153 */ 154 public static void setupBrowseItemFocusHighlight(ItemBridgeAdapter adapter, int zoomIndex) { 155 adapter.setFocusHighlight(new BrowseItemFocusHighlight(zoomIndex)); 156 } 157 158 /** 159 * Setup the focus highlight behavior of a focused item in header list. 160 * @param gridView the header list. 161 */ 162 public static void setupHeaderItemFocusHighlight(VerticalGridView gridView) { 163 if (gridView.getAdapter() instanceof ItemBridgeAdapter) { 164 ((ItemBridgeAdapter) gridView.getAdapter()) 165 .setFocusHighlight(new HeaderItemFocusHighlight(gridView)); 166 } 167 } 168 169 static class HeaderItemFocusHighlight implements FocusHighlight { 170 private static boolean sInitialized; 171 private static float sSelectScale; 172 private static int sDuration; 173 private BaseGridView mGridView; 174 175 HeaderItemFocusHighlight(BaseGridView gridView) { 176 mGridView = gridView; 177 lazyInit(gridView.getContext().getResources()); 178 } 179 180 private static void lazyInit(Resources res) { 181 if (!sInitialized) { 182 sSelectScale = 183 Float.parseFloat(res.getString(R.dimen.lb_browse_header_select_scale)); 184 sDuration = 185 Integer.parseInt(res.getString(R.dimen.lb_browse_header_select_duration)); 186 sInitialized = true; 187 } 188 } 189 190 class HeaderFocusAnimator extends FocusAnimator { 191 192 ItemBridgeAdapter.ViewHolder mViewHolder; 193 HeaderFocusAnimator(View view, float scale, int duration) { 194 super(view, scale, duration); 195 mViewHolder = (ItemBridgeAdapter.ViewHolder) mGridView.getChildViewHolder(view); 196 } 197 198 @Override 199 void setFocusLevel(float level) { 200 Presenter presenter = mViewHolder.getPresenter(); 201 if (presenter instanceof RowHeaderPresenter) { 202 ((RowHeaderPresenter) presenter).setSelectLevel( 203 ((RowHeaderPresenter.ViewHolder) mViewHolder.getViewHolder()), level); 204 } 205 super.setFocusLevel(level); 206 } 207 208 } 209 210 private void viewFocused(View view, boolean hasFocus) { 211 view.setSelected(hasFocus); 212 FocusAnimator animator = (FocusAnimator) view.getTag(R.id.lb_focus_animator); 213 if (animator == null) { 214 animator = new HeaderFocusAnimator(view, sSelectScale, sDuration); 215 view.setTag(R.id.lb_focus_animator, animator); 216 } 217 animator.animateFocus(hasFocus, false); 218 } 219 220 @Override 221 public void onItemFocused(View view, boolean hasFocus) { 222 viewFocused(view, hasFocus); 223 } 224 } 225} 226