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