RoundedRectHelper.java revision a76db0cf97b5f05ef0a5e1f6d999933f738f4a3e
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.Color; 17import android.graphics.drawable.ColorDrawable; 18import android.os.Build; 19import android.view.View; 20 21/** 22 * Helper for setting rounded rectangle backgrounds on a view. 23 */ 24final class RoundedRectHelper { 25 26 private final static RoundedRectHelper sInstance = new RoundedRectHelper(); 27 private Impl mImpl; 28 29 /** 30 * Returns an instance of the helper. 31 */ 32 public static RoundedRectHelper getInstance() { 33 return sInstance; 34 } 35 36 /** 37 * Sets or removes a rounded rectangle outline on the given view. 38 */ 39 public void setClipToRoundedOutline(View view, boolean clip) { 40 mImpl.setClipToRoundedOutline(view, clip); 41 } 42 43 /** 44 * Sets a rounded rectangle background on the given view, and clips the view to the 45 * background. If clipping isn't supported on the android runtime, a simple rectangle 46 * background is set instead. 47 * 48 * @param view The view to be modified 49 * @param color The color of the background 50 */ 51 public void setRoundedRectBackground(View view, int color) { 52 mImpl.setRoundedRectBackground(view, color); 53 } 54 55 /** 56 * Clears the background of the view to transparent. 57 * 58 * @param view The view to be modified 59 */ 60 public void clearBackground(View view) { 61 mImpl.clearBackground(view); 62 } 63 64 static interface Impl { 65 public void setClipToRoundedOutline(View view, boolean clip); 66 public void setRoundedRectBackground(View view, int color); 67 public void clearBackground(View view); 68 } 69 70 /** 71 * Implementation used prior to L. 72 */ 73 private static final class StubImpl implements Impl { 74 @Override 75 public void setClipToRoundedOutline(View view, boolean clip) { 76 // Not supported 77 } 78 79 @Override 80 public void setRoundedRectBackground(View view, int color) { 81 // We could set a rounded rect background, but we don't 82 // because we can't do setClipToOutline. 83 // So just set a regular rectangle. 84 view.setBackgroundColor(color); 85 } 86 87 @Override 88 public void clearBackground(View view) { 89 view.setBackground(null); 90 } 91 } 92 93 /** 94 * Implementation used on api 21 (and above). 95 */ 96 private static final class Api21Impl implements Impl { 97 @Override 98 public void setClipToRoundedOutline(View view, boolean clip) { 99 RoundedRectHelperApi21.setClipToRoundedOutline(view, clip); 100 } 101 102 @Override 103 public void setRoundedRectBackground(View view, int color) { 104 RoundedRectHelperApi21.setRoundedRectBackground(view, color); 105 } 106 107 @Override 108 public void clearBackground(View view) { 109 RoundedRectHelperApi21.clearBackground(view); 110 } 111 } 112 113 private RoundedRectHelper() { 114 if (Build.VERSION.SDK_INT >= 21) { 115 mImpl = new Api21Impl(); 116 } else { 117 mImpl = new StubImpl(); 118 } 119 } 120} 121