Java tutorial
/* * ================================================================================================= * Copyright (C) 2014 Martin Albedinsky * ================================================================================================= * Licensed under the Apache License, Version 2.0 or later (further "License" only). * ------------------------------------------------------------------------------------------------- * You may use this file only in compliance with the License. More details and copy of this License * you may obtain at * * http://www.apache.org/licenses/LICENSE-2.0 * * You can redistribute, modify or publish any part of the code written within this file but as it * is described in the License, the software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES or CONDITIONS OF ANY KIND. * * See the License for the specific language governing permissions and limitations under the License. * ================================================================================================= */ package com.albedinsky.android.support.ui.graphics.drawable; import android.annotation.SuppressLint; import android.content.res.ColorStateList; import android.graphics.Canvas; import android.graphics.ColorFilter; import android.graphics.PorterDuff; import android.graphics.Rect; import android.graphics.Region; import android.graphics.drawable.Drawable; import android.graphics.drawable.InsetDrawable; import android.os.Build; import android.support.annotation.NonNull; import android.support.v4.graphics.drawable.DrawableCompat; /** * A {@link Drawable} implementation which can be used to wrap another instance of Drawable. * * @author Martin Albedinsky */ public class DrawableWrapper extends Drawable implements Drawable.Callback { /** * Interface =================================================================================== */ /** * Constants =================================================================================== */ /** * Log TAG. */ // private static final String TAG = "DrawableWrapper"; /** * Flag indicating whether the output trough log-cat is enabled or not. */ // private static final boolean LOG_ENABLED = true; /** * Flag indicating whether the debug output trough log-cat is enabled or not. */ // private static final boolean DEBUG_ENABLED = true; /** * Static members ============================================================================== */ /** * Members ===================================================================================== */ /** * Instance of wrapped drawable. */ final Drawable mDrawable; /** * Flat indicating whether this drawable is already mutated or not. */ private boolean mMutated; /** * Constructors ================================================================================ */ /** * Creates a new instance of DrawableWrapper which wraps the given <var>drawable</var>. * * @param drawable The drawable to wrap. */ public DrawableWrapper(@NonNull Drawable drawable) { this.mDrawable = drawable; mDrawable.setCallback(this); } /** * Methods ===================================================================================== */ /** * Public -------------------------------------------------------------------------------------- */ /** * Re-attaches this drawable wrapper as {@link Callback} to the wrapped drawable. This should be * called in case when the callback of the wrapped drawable has been since initialization removed * by framework, this means that this parent actually has been removed as callback. */ public void attachCallback() { mDrawable.setCallback(this); invalidateSelf(); } /** */ @Override public Drawable mutate() { if (!mMutated && super.mutate() == this) { mDrawable.mutate(); this.mMutated = true; } return this; } /** */ @Override public void draw(Canvas canvas) { mDrawable.draw(canvas); } /** */ @Override public void invalidateDrawable(Drawable who) { invalidateSelf(); } /** */ @Override public void scheduleDrawable(Drawable who, Runnable what, long when) { scheduleSelf(what, when); } /** */ @Override public void unscheduleDrawable(Drawable who, Runnable what) { unscheduleSelf(what); } /** * Getters + Setters --------------------------------------------------------------------------- */ /** * Returns the wrapped drawable by this tint drawable instance. * * @return Wrapped drawable. */ @NonNull public Drawable getDrawable() { return mDrawable; } /** */ @Override public void setChangingConfigurations(int configs) { mDrawable.setChangingConfigurations(configs); } /** */ @Override public int getChangingConfigurations() { return mDrawable.getChangingConfigurations(); } /** */ @Override public void setDither(boolean dither) { mDrawable.setDither(dither); } /** */ @Override public void setFilterBitmap(boolean filter) { mDrawable.setFilterBitmap(filter); } /** */ @Override public void setAlpha(int alpha) { mDrawable.setAlpha(alpha); } /** */ @Override public void setColorFilter(ColorFilter cf) { mDrawable.setColorFilter(cf); } /** */ @Override public boolean isStateful() { return mDrawable.isStateful(); } /** */ @Override public void jumpToCurrentState() { DrawableCompat.jumpToCurrentState(mDrawable); } /** */ @Override @SuppressLint("NewApi") public Drawable getCurrent() { if (mDrawable instanceof InsetDrawable && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { return ((InsetDrawable) mDrawable).getDrawable().getCurrent(); } return mDrawable.getCurrent(); } /** */ @Override public boolean setVisible(boolean visible, boolean restart) { return super.setVisible(visible, restart) || mDrawable.setVisible(visible, restart); } /** */ @Override public int getOpacity() { return mDrawable.getOpacity(); } /** */ @Override public Region getTransparentRegion() { return mDrawable.getTransparentRegion(); } /** */ @Override public int getIntrinsicWidth() { return mDrawable.getIntrinsicWidth(); } /** */ @Override public int getIntrinsicHeight() { return mDrawable.getIntrinsicHeight(); } /** */ @Override public int getMinimumWidth() { return mDrawable.getMinimumWidth(); } /** */ @Override public int getMinimumHeight() { return mDrawable.getMinimumHeight(); } /** */ @Override public boolean getPadding(Rect padding) { return mDrawable.getPadding(padding); } /** */ @Override public void setAutoMirrored(boolean mirrored) { DrawableCompat.setAutoMirrored(mDrawable, mirrored); } /** */ @Override public boolean isAutoMirrored() { return DrawableCompat.isAutoMirrored(mDrawable); } /** */ @Override public void setTint(int tint) { DrawableCompat.setTint(mDrawable, tint); } /** */ @Override public void setTintList(ColorStateList tint) { DrawableCompat.setTintList(mDrawable, tint); } /** */ @Override public void setTintMode(PorterDuff.Mode tintMode) { DrawableCompat.setTintMode(mDrawable, tintMode); } /** */ @Override public void setHotspot(float x, float y) { DrawableCompat.setHotspot(mDrawable, x, y); } /** */ @Override public void setHotspotBounds(int left, int top, int right, int bottom) { DrawableCompat.setHotspotBounds(mDrawable, left, top, right, bottom); } /** * Protected ----------------------------------------------------------------------------------- */ /** */ @Override protected boolean onStateChange(int[] stateSet) { return mDrawable.setState(stateSet); } /** */ @Override protected void onBoundsChange(Rect bounds) { mDrawable.setBounds(bounds); } /** */ @Override protected boolean onLevelChange(int level) { return mDrawable.setLevel(level); } /** * Private ------------------------------------------------------------------------------------- */ /** * Inner classes =============================================================================== */ }