FocusHighlightHelper.java revision 8b068ddbbf22a246eab49ec25a2f7c3abfbdca51
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    private static class BrowseItemFocusHighlight implements FocusHighlight {
26        private static final int DURATION_MS = 150;
27
28        private int[] mZoomSizeResourceId = new int[] { R.dimen.lb_browse_item_zoom_height,
29                R.dimen.lb_browse_item_zoom_width };
30        private float[] mZoomSizeCache;
31
32        public BrowseItemFocusHighlight() {
33            mZoomSizeCache = new float[mZoomSizeResourceId.length];
34        }
35
36        private float getScale(View view) {
37            final int index = (view.getHeight() > view.getWidth() ? 0 : 1);
38            if (mZoomSizeCache[index] == 0f) {
39                mZoomSizeCache[index] = view.getResources()
40                        .getDimension(mZoomSizeResourceId[index]);
41            }
42            if (index == 0) {
43                final float height = view.getHeight();
44                return (height + mZoomSizeCache[index]) / height;
45            } else {
46                final float width = view.getWidth();
47                return (width + mZoomSizeCache[index]) / width;
48            }
49        }
50
51        private void viewFocused(View view, boolean hasFocus) {
52            if (hasFocus) {
53                final float scale = getScale(view);
54                view.animate().scaleX(scale).scaleY(scale).setDuration(DURATION_MS);
55            } else {
56                view.animate().scaleX(1f).scaleY(1f).setDuration(DURATION_MS);
57            }
58        }
59
60        @Override
61        public void onItemFocused(View view, Object item, boolean hasFocus) {
62            viewFocused(view, hasFocus);
63        }
64    }
65
66    private static BrowseItemFocusHighlight sBrowseItemFocusHighlight =
67            new BrowseItemFocusHighlight();
68
69    private static HeaderItemFocusHighlight sHeaderItemFocusHighlight =
70            new HeaderItemFocusHighlight();
71
72    /**
73     * Setup the focus highlight behavior of a focused item in browse list row.
74     * @param adapter  adapter of the list row.
75     */
76    public static void setupBrowseItemFocusHighlight(ItemBridgeAdapter adapter) {
77        adapter.setFocusHighlight(sBrowseItemFocusHighlight);
78    }
79
80    /**
81     * Setup the focus highlight behavior of a focused item in header list.
82     * @param adapter  adapter of the header list.
83     */
84    public static void setupHeaderItemFocusHighlight(ItemBridgeAdapter adapter) {
85        adapter.setFocusHighlight(sHeaderItemFocusHighlight);
86    }
87
88    static class HeaderItemFocusHighlight implements FocusHighlight {
89        private boolean mInitialized;
90        private float mSelectScale;
91        private float mUnselectAlpha;
92        private int mDuration;
93
94        private void initializeDimensions(Resources res) {
95            if (!mInitialized) {
96                mSelectScale =
97                        Float.parseFloat(res.getString(R.dimen.lb_browse_header_select_scale));
98                mUnselectAlpha =
99                        Float.parseFloat(res.getString(R.dimen.lb_browse_header_unselect_alpha));
100                mDuration =
101                        Integer.parseInt(res.getString(R.dimen.lb_browse_header_select_duration));
102                mInitialized = true;
103            }
104        }
105
106        private void viewFocused(View view, boolean hasFocus) {
107            initializeDimensions(view.getResources());
108            if (hasFocus) {
109                view.animate().scaleX(mSelectScale).scaleY(mSelectScale)
110                        .alpha(1f)
111                        .setDuration(mDuration);
112            } else {
113                view.animate().scaleX(1f).scaleY(1f)
114                        .alpha(mUnselectAlpha)
115                        .setDuration(mDuration);
116            }
117        }
118
119        @Override
120        public void onItemFocused(View view, Object item, boolean hasFocus) {
121            viewFocused(view, hasFocus);
122        }
123    }
124}
125