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.Outline; 17import android.support.annotation.RequiresApi; 18import android.util.SparseArray; 19import android.view.View; 20import android.view.ViewOutlineProvider; 21 22@RequiresApi(21) 23class RoundedRectHelperApi21 { 24 25 private static SparseArray<ViewOutlineProvider> sRoundedRectProvider; 26 private static final int MAX_CACHED_PROVIDER = 32; 27 28 static final class RoundedRectOutlineProvider extends ViewOutlineProvider { 29 30 private int mRadius; 31 32 RoundedRectOutlineProvider(int radius) { 33 mRadius = radius; 34 } 35 36 @Override 37 public void getOutline(View view, Outline outline) { 38 outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), mRadius); 39 outline.setAlpha(1f); 40 } 41 }; 42 43 public static void setClipToRoundedOutline(View view, boolean clip, int roundedCornerRadius) { 44 if (clip) { 45 if (sRoundedRectProvider == null) { 46 sRoundedRectProvider = new SparseArray<ViewOutlineProvider>(); 47 } 48 ViewOutlineProvider provider = sRoundedRectProvider.get(roundedCornerRadius); 49 if (provider == null) { 50 provider = new RoundedRectOutlineProvider(roundedCornerRadius); 51 if (sRoundedRectProvider.size() < MAX_CACHED_PROVIDER) { 52 sRoundedRectProvider.put(roundedCornerRadius, provider); 53 } 54 } 55 view.setOutlineProvider(provider); 56 } else { 57 view.setOutlineProvider(ViewOutlineProvider.BACKGROUND); 58 } 59 view.setClipToOutline(clip); 60 } 61} 62