FocusHighlightHelper.java revision 62d36d44e03a3dd0632b156615b04563c62e83c1
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.content.res.Resources; 20 21/** 22 * Setup the behavior how to highlight when a item gains focus. 23 */ 24public class FocusHighlightHelper { 25 26 static class BrowseItemFocusHighlight implements FocusHighlight { 27 private static final int DURATION_MS = 150; 28 29 public static int ZOOM_FACTOR_NONE = 0; 30 public static int ZOOM_FACTOR_SMALL = 1; 31 public static int ZOOM_FACTOR_MEDIUM = 2; 32 public static int ZOOM_FACTOR_LARGE = 3; 33 34 private static float[] sScaleFactor = new float[4]; 35 36 private int mScaleIndex; 37 38 BrowseItemFocusHighlight(int zoomIndex) { 39 mScaleIndex = (zoomIndex >= 0 && zoomIndex < sScaleFactor.length) ? 40 zoomIndex : ZOOM_FACTOR_MEDIUM; 41 } 42 43 private void lazyInit(Resources resources) { 44 if (sScaleFactor[ZOOM_FACTOR_NONE] == 0f) { 45 sScaleFactor[ZOOM_FACTOR_NONE] = 1f; 46 sScaleFactor[ZOOM_FACTOR_SMALL] = 47 resources.getFraction(R.fraction.lb_focus_zoom_factor_small, 1, 1); 48 sScaleFactor[ZOOM_FACTOR_MEDIUM] = 49 resources.getFraction(R.fraction.lb_focus_zoom_factor_medium, 1, 1); 50 sScaleFactor[ZOOM_FACTOR_LARGE] = 51 resources.getFraction(R.fraction.lb_focus_zoom_factor_large, 1, 1); 52 } 53 } 54 55 private float getScale(View view) { 56 lazyInit(view.getResources()); 57 return sScaleFactor[mScaleIndex]; 58 } 59 60 private void viewFocused(View view, boolean hasFocus) { 61 if (hasFocus) { 62 final float scale = getScale(view); 63 view.animate().scaleX(scale).scaleY(scale).setDuration(DURATION_MS); 64 } else { 65 view.animate().scaleX(1f).scaleY(1f).setDuration(DURATION_MS); 66 } 67 } 68 69 @Override 70 public void onItemFocused(View view, boolean hasFocus) { 71 viewFocused(view, hasFocus); 72 } 73 } 74 75 private static HeaderItemFocusHighlight sHeaderItemFocusHighlight = 76 new HeaderItemFocusHighlight(); 77 78 private static ActionItemFocusHighlight sActionItemFocusHighlight = 79 new ActionItemFocusHighlight(); 80 81 /** 82 * Setup the focus highlight behavior of a focused item in browse list row. 83 * @param adapter adapter of the list row. 84 */ 85 public static void setupBrowseItemFocusHighlight(ItemBridgeAdapter adapter, int zoomIndex) { 86 adapter.setFocusHighlight(new BrowseItemFocusHighlight(zoomIndex)); 87 } 88 89 /** 90 * Setup the focus highlight behavior of a focused item in header list. 91 * @param adapter adapter of the header list. 92 */ 93 public static void setupHeaderItemFocusHighlight(ItemBridgeAdapter adapter) { 94 adapter.setFocusHighlight(sHeaderItemFocusHighlight); 95 } 96 97 /** 98 * Setup the focus highlight behavior of a focused item in an action list. 99 * @param adapter adapter of the action list. 100 */ 101 public static void setupActionItemFocusHighlight(ItemBridgeAdapter adapter) { 102 adapter.setFocusHighlight(sActionItemFocusHighlight); 103 } 104 105 private static class HeaderItemFocusHighlight implements FocusHighlight { 106 private boolean mInitialized; 107 private float mSelectScale; 108 private float mUnselectAlpha; 109 private int mDuration; 110 111 private void initializeDimensions(Resources res) { 112 if (!mInitialized) { 113 mSelectScale = 114 Float.parseFloat(res.getString(R.dimen.lb_browse_header_select_scale)); 115 mUnselectAlpha = 116 Float.parseFloat(res.getString(R.dimen.lb_browse_header_unselect_alpha)); 117 mDuration = 118 Integer.parseInt(res.getString(R.dimen.lb_browse_header_select_duration)); 119 mInitialized = true; 120 } 121 } 122 123 private void viewFocused(View view, boolean hasFocus) { 124 initializeDimensions(view.getResources()); 125 if (hasFocus) { 126 view.animate().scaleX(mSelectScale).scaleY(mSelectScale) 127 .alpha(1f) 128 .setDuration(mDuration); 129 } else { 130 view.animate().scaleX(1f).scaleY(1f) 131 .alpha(mUnselectAlpha) 132 .setDuration(mDuration); 133 } 134 } 135 136 @Override 137 public void onItemFocused(View view, boolean hasFocus) { 138 viewFocused(view, hasFocus); 139 } 140 } 141 142 private static class ActionItemFocusHighlight implements FocusHighlight { 143 private boolean mInitialized; 144 private int mDuration; 145 146 private void initializeDimensions(Resources res) { 147 if (!mInitialized) { 148 mDuration = Integer.parseInt(res.getString(R.dimen.lb_details_overview_action_select_duration)); 149 } 150 } 151 152 @Override 153 public void onItemFocused(View view, boolean hasFocus) { 154 initializeDimensions(view.getResources()); 155 TransitionDrawable td = (TransitionDrawable) view.getBackground(); 156 if (hasFocus) { 157 td.startTransition(mDuration); 158 } else { 159 td.reverseTransition(mDuration); 160 } 161 } 162 } 163} 164