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
164f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stoutimport android.graphics.Outline;
178f886fe8c7e23fe6ccb8734167c960c2ed3429c3Alan Viveretteimport android.support.annotation.RequiresApi;
188f886fe8c7e23fe6ccb8734167c960c2ed3429c3Alan Viveretteimport android.util.SparseArray;
194f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stoutimport android.view.View;
208f886fe8c7e23fe6ccb8734167c960c2ed3429c3Alan Viveretteimport android.view.ViewOutlineProvider;
214f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout
228f886fe8c7e23fe6ccb8734167c960c2ed3429c3Alan Viverette@RequiresApi(21)
234f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stoutclass RoundedRectHelperApi21 {
244f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout
2585833087b2288e0f002de6b4ebcbc0564839a217Dake Gu    private static SparseArray<ViewOutlineProvider> sRoundedRectProvider;
2685833087b2288e0f002de6b4ebcbc0564839a217Dake Gu    private static final int MAX_CACHED_PROVIDER = 32;
2785833087b2288e0f002de6b4ebcbc0564839a217Dake Gu
2885833087b2288e0f002de6b4ebcbc0564839a217Dake Gu    static final class RoundedRectOutlineProvider extends ViewOutlineProvider {
2985833087b2288e0f002de6b4ebcbc0564839a217Dake Gu
3085833087b2288e0f002de6b4ebcbc0564839a217Dake Gu        private int mRadius;
3185833087b2288e0f002de6b4ebcbc0564839a217Dake Gu
3285833087b2288e0f002de6b4ebcbc0564839a217Dake Gu        RoundedRectOutlineProvider(int radius) {
3385833087b2288e0f002de6b4ebcbc0564839a217Dake Gu            mRadius = radius;
3485833087b2288e0f002de6b4ebcbc0564839a217Dake Gu        }
354f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout
364f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout        @Override
374f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout        public void getOutline(View view, Outline outline) {
3885833087b2288e0f002de6b4ebcbc0564839a217Dake Gu            outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), mRadius);
394f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout            outline.setAlpha(1f);
404f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout        }
414f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout    };
424f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout
4385833087b2288e0f002de6b4ebcbc0564839a217Dake Gu    public static void setClipToRoundedOutline(View view, boolean clip, int roundedCornerRadius) {
4485833087b2288e0f002de6b4ebcbc0564839a217Dake Gu        if (clip) {
4585833087b2288e0f002de6b4ebcbc0564839a217Dake Gu            if (sRoundedRectProvider == null) {
4685833087b2288e0f002de6b4ebcbc0564839a217Dake Gu                sRoundedRectProvider = new SparseArray<ViewOutlineProvider>();
4785833087b2288e0f002de6b4ebcbc0564839a217Dake Gu            }
4885833087b2288e0f002de6b4ebcbc0564839a217Dake Gu            ViewOutlineProvider provider = sRoundedRectProvider.get(roundedCornerRadius);
4985833087b2288e0f002de6b4ebcbc0564839a217Dake Gu            if (provider == null) {
5085833087b2288e0f002de6b4ebcbc0564839a217Dake Gu                provider = new RoundedRectOutlineProvider(roundedCornerRadius);
5185833087b2288e0f002de6b4ebcbc0564839a217Dake Gu                if (sRoundedRectProvider.size() < MAX_CACHED_PROVIDER) {
5285833087b2288e0f002de6b4ebcbc0564839a217Dake Gu                    sRoundedRectProvider.put(roundedCornerRadius, provider);
5385833087b2288e0f002de6b4ebcbc0564839a217Dake Gu                }
5485833087b2288e0f002de6b4ebcbc0564839a217Dake Gu            }
5585833087b2288e0f002de6b4ebcbc0564839a217Dake Gu            view.setOutlineProvider(provider);
5685833087b2288e0f002de6b4ebcbc0564839a217Dake Gu        } else {
5785833087b2288e0f002de6b4ebcbc0564839a217Dake Gu            view.setOutlineProvider(ViewOutlineProvider.BACKGROUND);
5885833087b2288e0f002de6b4ebcbc0564839a217Dake Gu        }
59be6b6282397861a044efd596bf5db501fa0b3a66Craig Stout        view.setClipToOutline(clip);
60be6b6282397861a044efd596bf5db501fa0b3a66Craig Stout    }
614f34a05cdf73b68c3b2eb8678f740ab15225126aCraig Stout}
62