DrawableCompat.java revision ac00a989afc2f1c559fc33174f481a0a0ef5b3d8
1/* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.support.v4.graphics.drawable; 18 19import android.graphics.drawable.Drawable; 20import android.os.Build; 21 22/** 23 * Helper for accessing features in {@link android.graphics.drawable.Drawable} 24 * introduced after API level 4 in a backwards compatible fashion. 25 */ 26public class DrawableCompat { 27 /** 28 * Interface for the full API. 29 */ 30 interface DrawableImpl { 31 void jumpToCurrentState(Drawable drawable); 32 void setAutoMirrored(Drawable drawable, boolean mirrored); 33 boolean isAutoMirrored(Drawable drawable); 34 void setHotspot(Drawable drawable, float x, float y); 35 void setHotspotBounds(Drawable drawable, int left, int top, int right, int bottom); 36 } 37 38 /** 39 * Interface implementation that doesn't use anything about v4 APIs. 40 */ 41 static class BaseDrawableImpl implements DrawableImpl { 42 @Override 43 public void jumpToCurrentState(Drawable drawable) { 44 } 45 46 @Override 47 public void setAutoMirrored(Drawable drawable, boolean mirrored) { 48 } 49 50 @Override 51 public boolean isAutoMirrored(Drawable drawable) { 52 return false; 53 } 54 55 @Override 56 public void setHotspot(Drawable drawable, float x, float y) { 57 } 58 59 @Override 60 public void setHotspotBounds(Drawable drawable, int left, int top, int right, int bottom) { 61 } 62 } 63 64 /** 65 * Interface implementation for devices with at least v11 APIs. 66 */ 67 static class HoneycombDrawableImpl extends BaseDrawableImpl { 68 @Override 69 public void jumpToCurrentState(Drawable drawable) { 70 DrawableCompatHoneycomb.jumpToCurrentState(drawable); 71 } 72 } 73 74 /** 75 * Interface implementation for devices with at least KitKat APIs. 76 */ 77 static class KitKatDrawableImpl extends HoneycombDrawableImpl { 78 @Override 79 public void setAutoMirrored(Drawable drawable, boolean mirrored) { 80 DrawableCompatKitKat.setAutoMirrored(drawable, mirrored); 81 } 82 83 @Override 84 public boolean isAutoMirrored(Drawable drawable) { 85 return DrawableCompatKitKat.isAutoMirrored(drawable); 86 } 87 } 88 89 /** 90 * Interface implementation for devices with at least L APIs. 91 */ 92 static class LDrawableImpl extends KitKatDrawableImpl { 93 @Override 94 public void setHotspot(Drawable drawable, float x, float y) { 95 DrawableCompatL.setHotspot(drawable, x, y); 96 } 97 98 @Override 99 public void setHotspotBounds(Drawable drawable, int left, int top, int right, int bottom) { 100 DrawableCompatL.setHotspotBounds(drawable, left, top, right, bottom); 101 } 102 } 103 104 /** 105 * Select the correct implementation to use for the current platform. 106 */ 107 static final DrawableImpl IMPL; 108 static { 109 final int version = android.os.Build.VERSION.SDK_INT; 110 if (version >= 21 || Build.VERSION.CODENAME.equals("L")) { 111 IMPL = new LDrawableImpl(); 112 } else if (version >= 19) { 113 IMPL = new KitKatDrawableImpl(); 114 } else if (version >= 11) { 115 IMPL = new HoneycombDrawableImpl(); 116 } else { 117 IMPL = new BaseDrawableImpl(); 118 } 119 } 120 121 /** 122 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}. 123 * <p> 124 * If running on a pre-{@link android.os.Build.VERSION_CODES#HONEYCOMB} 125 * device this method does nothing. 126 * 127 * @param drawable The Drawable against which to invoke the method. 128 */ 129 public static void jumpToCurrentState(Drawable drawable) { 130 IMPL.jumpToCurrentState(drawable); 131 } 132 133 /** 134 * Set whether this Drawable is automatically mirrored when its layout 135 * direction is RTL (right-to left). See 136 * {@link android.util.LayoutDirection}. 137 * <p> 138 * If running on a pre-{@link android.os.Build.VERSION_CODES#KITKAT} device 139 * this method does nothing. 140 * 141 * @param drawable The Drawable against which to invoke the method. 142 * @param mirrored Set to true if the Drawable should be mirrored, false if 143 * not. 144 */ 145 public static void setAutoMirrored(Drawable drawable, boolean mirrored) { 146 IMPL.setAutoMirrored(drawable, mirrored); 147 } 148 149 /** 150 * Tells if this Drawable will be automatically mirrored when its layout 151 * direction is RTL right-to-left. See {@link android.util.LayoutDirection}. 152 * <p> 153 * If running on a pre-{@link android.os.Build.VERSION_CODES#KITKAT} device 154 * this method returns false. 155 * 156 * @param drawable The Drawable against which to invoke the method. 157 * @return boolean Returns true if this Drawable will be automatically 158 * mirrored. 159 */ 160 public static boolean isAutoMirrored(Drawable drawable) { 161 return IMPL.isAutoMirrored(drawable); 162 } 163 164 /** 165 * Specifies the hotspot's location within the drawable. 166 * 167 * @param drawable The Drawable against which to invoke the method. 168 * @param x The X coordinate of the center of the hotspot 169 * @param y The Y coordinate of the center of the hotspot 170 */ 171 public static void setHotspot(Drawable drawable, float x, float y) { 172 IMPL.setHotspot(drawable, x, y); 173 } 174 175 /** 176 * Sets the bounds to which the hotspot is constrained, if they should be 177 * different from the drawable bounds. 178 * 179 * @param drawable The Drawable against which to invoke the method. 180 */ 181 public static void setHotspotBounds(Drawable drawable, int left, int top, 182 int right, int bottom) { 183 IMPL.setHotspotBounds(drawable, left, top, right, bottom); 184 } 185} 186