private static boolean setContainerConstantStateV9(DrawableContainer drawable, ConstantState constantState) { if (!sSetConstantStateMethodFetched) { try { sSetConstantStateMethod = DrawableContainer.class.getDeclaredMethod("setConstantState", new Class[]{DrawableContainerState.class}); sSetConstantStateMethod.setAccessible(true); } catch (NoSuchMethodException e) { Log.e(LOG_TAG, "Could not fetch setConstantState(). Oh well."); } sSetConstantStateMethodFetched = true; } if (sSetConstantStateMethod != null) { try { sSetConstantStateMethod.invoke(drawable, new Object[]{constantState}); return true; } catch (Exception e2) { Log.e(LOG_TAG, "Could not invoke setConstantState(). Oh well."); } } return false; }
/** * Returns a {@link Map} that holds a mapping from the existing state-lists * in the background {@link Drawable}. * * @param background A {@link Drawable} background ( * {@link StateListDrawable}). * @return A {@link Map}. An empty map in case the background * <code>null</code>, or is not a {@link StateListDrawable}. */ public static Map<int[], Drawable> getExistingStates(Drawable background) { LinkedHashMap<int[], Drawable> map = new LinkedHashMap<int[], Drawable>(); if (background instanceof StateListDrawable) { // Grab the existing states. Note that the API hides some of the // public functionality with the @hide tag, so we have to access // those through reflection... StateListDrawable stateList = (StateListDrawable) background; DrawableContainerState containerState = (DrawableContainerState) stateList .getConstantState(); Drawable[] children = containerState.getChildren(); try { // This method is public but hidden ("pending API council") Method method = stateList.getClass().getMethod("getStateSet", int.class); for (int i = 0; i < containerState.getChildCount(); i++) { Object state = method.invoke(stateList, i); if (state instanceof int[]) { map.put((int[]) state, children[i]); } } } catch (Exception e) { PXLog.e(TAG, e, "Error getting the state set"); } } return map; }
/** * Check if two Drawables are equal. A regular check for a Drawable equals * just checks for the instance reference, while this check is doing a * deeper equals when dealing with {@link DrawableContainer} instances. In * these cases, the method will run equals on each of the child drawables in * the container (order is importance as well). * * @param d1 * @param d2 * @return <code>true</code> if the drawables are equal, <code>false</code> * otherwise. */ public static boolean isEquals(Drawable d1, Drawable d2) { if (d1 == d2) { return true; } if (d1 == null || d2 == null) { return false; } if (d1 instanceof DrawableContainer && d2 instanceof DrawableContainer) { // Try to match the content of those containers DrawableContainerState containerState1 = (DrawableContainerState) ((DrawableContainer) d1) .getConstantState(); DrawableContainerState containerState2 = (DrawableContainerState) ((DrawableContainer) d2) .getConstantState(); return Arrays.equals(containerState1.getChildren(), containerState2.getChildren()); } return d1.equals(d2); }
private void setColors(View view, int[] colors) { Drawable drawable = view.getBackground(); if (drawable instanceof StateListDrawable) { StateListDrawable stateListDrawable = (StateListDrawable) drawable; DrawableContainerState drawableContainerState = (DrawableContainerState) stateListDrawable.getConstantState(); if (drawableContainerState != null) { Drawable[] drawables = drawableContainerState.getChildren(); for (int i = 0; i < colors.length; i++) if (drawables[i] instanceof GradientDrawable) ((GradientDrawable) drawables[i]).setColor(colors[i]); } } else Log.w(TAG, "Drawable of view must be StateListDrawable in WoWoStateListColorAnimation"); }
public static boolean m2634b(Drawable drawable) { if (drawable instanceof LayerDrawable) { return VERSION.SDK_INT >= 16; } else if (drawable instanceof InsetDrawable) { return VERSION.SDK_INT >= 14; } else { if (drawable instanceof StateListDrawable) { return VERSION.SDK_INT >= 8; } else { if (drawable instanceof GradientDrawable) { return VERSION.SDK_INT >= 14; } else { if (!(drawable instanceof DrawableContainer)) { return drawable instanceof C0063q ? m2634b(((C0063q) drawable).m469a()) : drawable instanceof C0244a ? m2634b(((C0244a) drawable).m1984a()) : drawable instanceof ScaleDrawable ? m2634b(((ScaleDrawable) drawable).getDrawable()) : true; } else { ConstantState constantState = drawable.getConstantState(); if (!(constantState instanceof DrawableContainerState)) { return true; } for (Drawable b : ((DrawableContainerState) constantState).getChildren()) { if (!m2634b(b)) { return false; } } return true; } } } } }
private static boolean shouldMutateBackground(Drawable paramDrawable) { if (Build.VERSION.SDK_INT >= 16) {} for (;;) { return true; if ((paramDrawable instanceof LayerDrawable)) { if (Build.VERSION.SDK_INT < 16) { return false; } } else if ((paramDrawable instanceof InsetDrawable)) { if (Build.VERSION.SDK_INT < 14) { return false; } } else if ((paramDrawable instanceof DrawableContainer)) { Drawable.ConstantState localConstantState = paramDrawable.getConstantState(); if ((localConstantState instanceof DrawableContainer.DrawableContainerState)) { Drawable[] arrayOfDrawable = ((DrawableContainer.DrawableContainerState)localConstantState).getChildren(); int i = arrayOfDrawable.length; for (int j = 0; j < i; j++) { if (!shouldMutateBackground(arrayOfDrawable[j])) { return false; } } } } } }
private Bitmap getBitmapFromStateListDrawable(){ DrawableContainerState state = (DrawableContainerState) getDrawable().getConstantState(); for(Drawable drawable : state.getChildren() ){ if( drawable instanceof BitmapDrawable ){ return ((BitmapDrawable) drawable).getBitmap(); } } return null; }
static boolean canSafelyMutateDrawable(@NonNull Drawable drawable) { if (drawable instanceof LayerDrawable) { if (VERSION.SDK_INT >= 16) { return true; } return false; } else if (drawable instanceof InsetDrawable) { if (VERSION.SDK_INT < 14) { return false; } return true; } else if (drawable instanceof StateListDrawable) { if (VERSION.SDK_INT < 8) { return false; } return true; } else if (drawable instanceof GradientDrawable) { if (VERSION.SDK_INT < 14) { return false; } return true; } else if (drawable instanceof DrawableContainer) { ConstantState state = drawable.getConstantState(); if (!(state instanceof DrawableContainerState)) { return true; } for (Drawable child : ((DrawableContainerState) state).getChildren()) { if (!canSafelyMutateDrawable(child)) { return false; } } return true; } else if (drawable instanceof DrawableWrapper) { return canSafelyMutateDrawable(((DrawableWrapper) drawable).getWrappedDrawable()); } else { if (drawable instanceof android.support.v7.graphics.drawable.DrawableWrapper) { return canSafelyMutateDrawable(((android.support.v7.graphics.drawable.DrawableWrapper) drawable).getWrappedDrawable()); } return true; } }