com.github.shareme.gwsmaterialuikit.library.material.drawable.PaddingDrawable.java Source code

Java tutorial

Introduction

Here is the source code for com.github.shareme.gwsmaterialuikit.library.material.drawable.PaddingDrawable.java

Source

/*
 * Copyright 2015 Rey Pham.
 * Modifications Copyright(C) 2016 Fred Grott
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.github.shareme.gwsmaterialuikit.library.material.drawable;

import android.content.res.ColorStateList;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.Region;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v4.graphics.drawable.DrawableCompat;

/**
 * Created by Rey on 8/19/2015.
 */
public class PaddingDrawable extends Drawable implements Drawable.Callback {

    private Drawable mDrawable;

    private int mPaddingLeft;
    private int mPaddingTop;
    private int mPaddingRight;
    private int mPaddingBottom;

    public PaddingDrawable(Drawable drawable) {
        setWrappedDrawable(drawable);
    }

    public void setPadding(int paddingLeft, int paddingTop, int paddingRight, int paddingBottom) {
        mPaddingLeft = paddingLeft;
        mPaddingTop = paddingTop;
        mPaddingRight = paddingRight;
        mPaddingBottom = paddingBottom;
    }

    public int getPaddingLeft() {
        return mPaddingLeft;
    }

    public int getPaddingTop() {
        return mPaddingTop;
    }

    public int getPaddingRight() {
        return mPaddingRight;
    }

    public int getPaddingBottom() {
        return mPaddingBottom;
    }

    @Override
    public void draw(@NonNull Canvas canvas) {
        if (mDrawable != null)
            mDrawable.draw(canvas);
    }

    @Override
    protected void onBoundsChange(Rect bounds) {
        if (mDrawable != null)
            mDrawable.setBounds(bounds.left + mPaddingLeft, bounds.top + mPaddingTop, bounds.right - mPaddingRight,
                    bounds.bottom - mPaddingBottom);
    }

    @Override
    public void setChangingConfigurations(int configs) {
        if (mDrawable != null)
            mDrawable.setChangingConfigurations(configs);
    }

    @Override
    public int getChangingConfigurations() {
        return mDrawable != null ? mDrawable.getChangingConfigurations() : 0;
    }

    @Override
    public void setDither(boolean dither) {
        if (mDrawable != null)
            mDrawable.setDither(dither);
    }

    @Override
    public void setFilterBitmap(boolean filter) {
        if (mDrawable != null)
            mDrawable.setFilterBitmap(filter);
    }

    @Override
    public void setAlpha(int alpha) {
        if (mDrawable != null)
            mDrawable.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
        if (mDrawable != null)
            mDrawable.setColorFilter(cf);
    }

    @Override
    public boolean isStateful() {
        return mDrawable != null && mDrawable.isStateful();
    }

    @Override
    public boolean setState(@NonNull final int[] stateSet) {
        return mDrawable != null && mDrawable.setState(stateSet);
    }

    @NonNull
    @Override
    public int[] getState() {
        return mDrawable != null ? mDrawable.getState() : null;
    }

    public void jumpToCurrentState() {
        if (mDrawable != null)
            DrawableCompat.jumpToCurrentState(mDrawable);
    }

    @NonNull
    @Override
    public Drawable getCurrent() {
        return mDrawable != null ? mDrawable.getCurrent() : null;
    }

    @Override
    public boolean setVisible(boolean visible, boolean restart) {
        return super.setVisible(visible, restart) || (mDrawable != null && mDrawable.setVisible(visible, restart));
    }

    @Override
    public int getOpacity() {
        return mDrawable != null ? mDrawable.getOpacity() : PixelFormat.UNKNOWN;
    }

    @Override
    public Region getTransparentRegion() {
        return mDrawable != null ? mDrawable.getTransparentRegion() : null;
    }

    @Override
    public int getIntrinsicWidth() {
        return (mDrawable != null ? mDrawable.getIntrinsicWidth() : 0) + mPaddingLeft + mPaddingRight;
    }

    @Override
    public int getIntrinsicHeight() {
        return (mDrawable != null ? mDrawable.getIntrinsicHeight() : 0) + mPaddingTop + mPaddingBottom;
    }

    @Override
    public int getMinimumWidth() {
        return (mDrawable != null ? mDrawable.getMinimumWidth() : 0) + mPaddingLeft + mPaddingRight;
    }

    @Override
    public int getMinimumHeight() {
        return (mDrawable != null ? mDrawable.getMinimumHeight() : 0) + mPaddingTop + mPaddingBottom;
    }

    @Override
    public boolean getPadding(@NonNull Rect padding) {
        boolean hasPadding = mDrawable != null && mDrawable.getPadding(padding);
        if (hasPadding) {
            padding.left += mPaddingLeft;
            padding.top += mPaddingTop;
            padding.right += mPaddingRight;
            padding.bottom += mPaddingBottom;
        } else {
            padding.set(mPaddingLeft, mPaddingTop, mPaddingRight, mPaddingBottom);
            hasPadding = mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0 || mPaddingBottom != 0;
        }

        return hasPadding;
    }

    /**
     * {@inheritDoc}
     */
    public void invalidateDrawable(@NonNull Drawable who) {
        invalidateSelf();
    }

    /**
     * {@inheritDoc}
     */
    public void scheduleDrawable(@NonNull Drawable who, @NonNull Runnable what, long when) {
        scheduleSelf(what, when);
    }

    /**
     * {@inheritDoc}
     */
    public void unscheduleDrawable(@NonNull Drawable who, @NonNull Runnable what) {
        unscheduleSelf(what);
    }

    @Override
    protected boolean onLevelChange(int level) {
        return mDrawable != null && mDrawable.setLevel(level);
    }

    @Override
    public void setAutoMirrored(boolean mirrored) {
        if (mDrawable != null)
            DrawableCompat.setAutoMirrored(mDrawable, mirrored);
    }

    @Override
    public boolean isAutoMirrored() {
        return mDrawable != null && DrawableCompat.isAutoMirrored(mDrawable);
    }

    @Override
    public void setTint(int tint) {
        if (mDrawable != null)
            DrawableCompat.setTint(mDrawable, tint);
    }

    @Override
    public void setTintList(ColorStateList tint) {
        if (mDrawable != null)
            DrawableCompat.setTintList(mDrawable, tint);
    }

    @Override
    public void setTintMode(@NonNull PorterDuff.Mode tintMode) {
        if (mDrawable != null)
            DrawableCompat.setTintMode(mDrawable, tintMode);
    }

    @Override
    public void setHotspot(float x, float y) {
        if (mDrawable != null)
            DrawableCompat.setHotspot(mDrawable, x, y);
    }

    @Override
    public void setHotspotBounds(int left, int top, int right, int bottom) {
        if (mDrawable != null)
            DrawableCompat.setHotspotBounds(mDrawable, left, top, right, bottom);
    }

    public Drawable getWrappedDrawable() {
        return mDrawable;
    }

    public void setWrappedDrawable(Drawable drawable) {
        if (mDrawable != null) {
            mDrawable.setCallback(null);
        }

        mDrawable = drawable;

        if (drawable != null) {
            drawable.setCallback(this);
        }

        onBoundsChange(getBounds());
        invalidateSelf();
    }
}