diff options
Diffstat (limited to 'src/com/android/bitmap/drawable/TileDrawable.java')
-rw-r--r-- | src/com/android/bitmap/drawable/TileDrawable.java | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/src/com/android/bitmap/drawable/TileDrawable.java b/src/com/android/bitmap/drawable/TileDrawable.java new file mode 100644 index 0000000..40d3e16 --- /dev/null +++ b/src/com/android/bitmap/drawable/TileDrawable.java @@ -0,0 +1,164 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * 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.android.bitmap.drawable; + +import android.animation.ValueAnimator; +import android.animation.ValueAnimator.AnimatorUpdateListener; +import android.graphics.Canvas; +import android.graphics.ColorFilter; +import android.graphics.Paint; +import android.graphics.Rect; +import android.graphics.drawable.Drawable; + +/** + * A drawable that wraps another drawable and places it in the center of this space. This drawable + * allows a background color for the "tile", and has a fade-out transition when + * {@link #setVisible(boolean, boolean)} indicates that it is no longer visible. + */ +public class TileDrawable extends Drawable implements Drawable.Callback { + + private final Paint mPaint = new Paint(); + private final Drawable mInner; + private final int mInnerWidth; + private final int mInnerHeight; + + protected final ValueAnimator mFadeOutAnimator; + + public TileDrawable(Drawable inner, int innerWidth, int innerHeight, + int backgroundColor, int fadeOutDurationMs) { + mInner = inner.mutate(); + mInnerWidth = innerWidth; + mInnerHeight = innerHeight; + mPaint.setColor(backgroundColor); + mInner.setCallback(this); + + mFadeOutAnimator = ValueAnimator.ofInt(255, 0) + .setDuration(fadeOutDurationMs); + mFadeOutAnimator.addUpdateListener(new AnimatorUpdateListener() { + @Override + public void onAnimationUpdate(ValueAnimator animation) { + setAlpha((Integer) animation.getAnimatedValue()); + } + }); + + reset(); + } + + public void reset() { + setAlpha(0); + setVisible(false); + } + + @Override + protected void onBoundsChange(Rect bounds) { + super.onBoundsChange(bounds); + + if (bounds.isEmpty()) { + mInner.setBounds(0, 0, 0, 0); + } else { + final int l = bounds.left + (bounds.width() / 2) - (mInnerWidth / 2); + final int t = bounds.top + (bounds.height() / 2) - (mInnerHeight / 2); + mInner.setBounds(l, t, l + mInnerWidth, t + mInnerHeight); + } + } + + @Override + public void draw(Canvas canvas) { + if (!isVisible() && mPaint.getAlpha() == 0) { + return; + } + canvas.drawRect(getBounds(), mPaint); + mInner.draw(canvas); + } + + @Override + public void setAlpha(int alpha) { + final int old = mPaint.getAlpha(); + mPaint.setAlpha(alpha); + setInnerAlpha(alpha); + if (alpha != old) { + invalidateSelf(); + } + } + + @Override + public void setColorFilter(ColorFilter cf) { + mPaint.setColorFilter(cf); + mInner.setColorFilter(cf); + } + + @Override + public int getOpacity() { + return 0; + } + + protected int getCurrentAlpha() { + return mPaint.getAlpha(); + } + + public boolean setVisible(boolean visible) { + return setVisible(visible, true /* dontcare */); + } + + @Override + public boolean setVisible(boolean visible, boolean restart) { + mInner.setVisible(visible, restart); + final boolean changed = super.setVisible(visible, restart); + if (changed) { + if (isVisible()) { + // pop in (no-op) + // the transition will still be smooth if the previous state's layer fades out + mFadeOutAnimator.cancel(); + setAlpha(255); + } else { + // fade out + if (mPaint.getAlpha() == 255 && !getBounds().isEmpty()) { + mFadeOutAnimator.start(); + } + } + } + return changed; + } + + @Override + protected boolean onLevelChange(int level) { + return mInner.setLevel(level); + } + + /** + * Changes the alpha on just the inner wrapped drawable. + */ + public void setInnerAlpha(int alpha) { + mInner.setAlpha(alpha); + } + + @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); + } + +} |