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