14f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout/*
24f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout * Copyright (C) 2014 The Android Open Source Project
34f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout *
44f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
54f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout * in compliance with the License. You may obtain a copy of the License at
64f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout *
74f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout * http://www.apache.org/licenses/LICENSE-2.0
84f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout *
94f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout * Unless required by applicable law or agreed to in writing, software distributed under the License
104f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
114f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout * or implied. See the License for the specific language governing permissions and limitations under
124f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout * the License.
134f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout */
144f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stoutpackage android.support.v17.leanback.widget;
154f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout
1685833087b2288e0f002de6b4ebcbc0564839a217Dake Guimport android.util.SparseArray;
174f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stoutimport android.graphics.Outline;
184f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stoutimport android.view.ViewOutlineProvider;
194f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stoutimport android.view.View;
204f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout
214f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stoutclass RoundedRectHelperApi21 {
224f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout
2385833087b2288e0f002de6b4ebcbc0564839a217Dake Gu    private static SparseArray<ViewOutlineProvider> sRoundedRectProvider;
2485833087b2288e0f002de6b4ebcbc0564839a217Dake Gu    private static final int MAX_CACHED_PROVIDER = 32;
2585833087b2288e0f002de6b4ebcbc0564839a217Dake Gu
2685833087b2288e0f002de6b4ebcbc0564839a217Dake Gu    static final class RoundedRectOutlineProvider extends ViewOutlineProvider {
2785833087b2288e0f002de6b4ebcbc0564839a217Dake Gu
2885833087b2288e0f002de6b4ebcbc0564839a217Dake Gu        private int mRadius;
2985833087b2288e0f002de6b4ebcbc0564839a217Dake Gu
3085833087b2288e0f002de6b4ebcbc0564839a217Dake Gu        RoundedRectOutlineProvider(int radius) {
3185833087b2288e0f002de6b4ebcbc0564839a217Dake Gu            mRadius = radius;
3285833087b2288e0f002de6b4ebcbc0564839a217Dake Gu        }
334f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout
344f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout        @Override
354f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout        public void getOutline(View view, Outline outline) {
3685833087b2288e0f002de6b4ebcbc0564839a217Dake Gu            outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), mRadius);
374f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout            outline.setAlpha(1f);
384f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout        }
394f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout    };
404f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout
4185833087b2288e0f002de6b4ebcbc0564839a217Dake Gu    public static void setClipToRoundedOutline(View view, boolean clip, int roundedCornerRadius) {
4285833087b2288e0f002de6b4ebcbc0564839a217Dake Gu        if (clip) {
4385833087b2288e0f002de6b4ebcbc0564839a217Dake Gu            if (sRoundedRectProvider == null) {
4485833087b2288e0f002de6b4ebcbc0564839a217Dake Gu                sRoundedRectProvider = new SparseArray<ViewOutlineProvider>();
4585833087b2288e0f002de6b4ebcbc0564839a217Dake Gu            }
4685833087b2288e0f002de6b4ebcbc0564839a217Dake Gu            ViewOutlineProvider provider = sRoundedRectProvider.get(roundedCornerRadius);
4785833087b2288e0f002de6b4ebcbc0564839a217Dake Gu            if (provider == null) {
4885833087b2288e0f002de6b4ebcbc0564839a217Dake Gu                provider = new RoundedRectOutlineProvider(roundedCornerRadius);
4985833087b2288e0f002de6b4ebcbc0564839a217Dake Gu                if (sRoundedRectProvider.size() < MAX_CACHED_PROVIDER) {
5085833087b2288e0f002de6b4ebcbc0564839a217Dake Gu                    sRoundedRectProvider.put(roundedCornerRadius, provider);
5185833087b2288e0f002de6b4ebcbc0564839a217Dake Gu                }
5285833087b2288e0f002de6b4ebcbc0564839a217Dake Gu            }
5385833087b2288e0f002de6b4ebcbc0564839a217Dake Gu            view.setOutlineProvider(provider);
5485833087b2288e0f002de6b4ebcbc0564839a217Dake Gu        } else {
5585833087b2288e0f002de6b4ebcbc0564839a217Dake Gu            view.setOutlineProvider(ViewOutlineProvider.BACKGROUND);
5685833087b2288e0f002de6b4ebcbc0564839a217Dake Gu        }
57be6b6282397861a044efd596bf5db501fa0b3a66Craig Stout        view.setClipToOutline(clip);
58be6b6282397861a044efd596bf5db501fa0b3a66Craig Stout    }
594f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout}
60