diff options
author | Chris Craik <ccraik@google.com> | 2015-05-29 21:53:06 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2015-05-29 21:53:06 +0000 |
commit | 515274973fb4eff42770daa92c272234c0415cdd (patch) | |
tree | 3fb9e87efeb70358d373f72b3acc5ca98dcec105 | |
parent | 47acc4b4f4f7f15641044126a48028fbb0018350 (diff) | |
parent | 537754df0d7348949e2324a91cc9e4df7571d7bf (diff) | |
download | android_frameworks_ex-515274973fb4eff42770daa92c272234c0415cdd.tar.gz android_frameworks_ex-515274973fb4eff42770daa92c272234c0415cdd.tar.bz2 android_frameworks_ex-515274973fb4eff42770daa92c272234c0415cdd.zip |
Merge "Add setCircularMaskEnabled"
-rw-r--r-- | framesequence/src/android/support/rastermill/FrameSequenceDrawable.java | 43 |
1 files changed, 41 insertions, 2 deletions
diff --git a/framesequence/src/android/support/rastermill/FrameSequenceDrawable.java b/framesequence/src/android/support/rastermill/FrameSequenceDrawable.java index 5158bc0..c4ce0ce 100644 --- a/framesequence/src/android/support/rastermill/FrameSequenceDrawable.java +++ b/framesequence/src/android/support/rastermill/FrameSequenceDrawable.java @@ -17,11 +17,14 @@ package android.support.rastermill; import android.graphics.Bitmap; +import android.graphics.BitmapShader; import android.graphics.Canvas; import android.graphics.ColorFilter; import android.graphics.Paint; import android.graphics.PixelFormat; import android.graphics.Rect; +import android.graphics.RectF; +import android.graphics.Shader; import android.graphics.drawable.Animatable; import android.graphics.drawable.Drawable; import android.os.Handler; @@ -125,7 +128,10 @@ public class FrameSequenceDrawable extends Drawable implements Animatable, Runna private final FrameSequence.State mFrameSequenceState; private final Paint mPaint; - private final Rect mSrcRect; + private BitmapShader mFrontBitmapShader; + private BitmapShader mBackBitmapShader; + private final Rect mSrcRect; + private boolean mCircleMaskEnabled; //Protects the fields below private final Object mLock = new Object(); @@ -169,6 +175,7 @@ public class FrameSequenceDrawable extends Drawable implements Animatable, Runna } int lastFrame = nextFrame - 2; long invalidateTimeMs = mFrameSequenceState.getFrame(nextFrame, bitmap, lastFrame); + if (invalidateTimeMs < MIN_DELAY_MS) { invalidateTimeMs = DEFAULT_DELAY_MS; } @@ -237,6 +244,11 @@ public class FrameSequenceDrawable extends Drawable implements Animatable, Runna mPaint = new Paint(); mPaint.setFilterBitmap(true); + mFrontBitmapShader + = new BitmapShader(mFrontBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); + mBackBitmapShader + = new BitmapShader(mBackBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); + mLastSwap = 0; mNextFrameToDecode = -1; @@ -244,6 +256,18 @@ public class FrameSequenceDrawable extends Drawable implements Animatable, Runna initializeDecodingThread(); } + /** + * Pass true to mask the shape of the animated drawing content to a circle. + * + * <p> The masking circle will be the largest circle contained in the Drawable's bounds. + * Masking is done with BitmapShader, incurring minimal additional draw cost. + */ + public final void setCircleMaskEnabled(boolean circleMaskEnabled) { + mCircleMaskEnabled = circleMaskEnabled; + // Anti alias only necessary when using circular mask + mPaint.setAntiAlias(circleMaskEnabled); + } + private void checkDestroyedLocked() { if (mDestroyed) { throw new IllegalStateException("Cannot perform operation on recycled drawable"); @@ -318,6 +342,10 @@ public class FrameSequenceDrawable extends Drawable implements Animatable, Runna mBackBitmap = mFrontBitmap; mFrontBitmap = tmp; + BitmapShader tmpShader = mBackBitmapShader; + mBackBitmapShader = mFrontBitmapShader; + mFrontBitmapShader = tmpShader; + mLastSwap = SystemClock.uptimeMillis(); boolean continueLooping = true; @@ -337,7 +365,17 @@ public class FrameSequenceDrawable extends Drawable implements Animatable, Runna } } - canvas.drawBitmap(mFrontBitmap, mSrcRect, getBounds(), mPaint); + if (mCircleMaskEnabled) { + Rect bounds = getBounds(); + mPaint.setShader(mFrontBitmapShader); + float width = bounds.width(); + float height = bounds.height(); + float circleRadius = (Math.min(width, height)) / 2f; + canvas.drawCircle(width / 2f, height / 2f, circleRadius, mPaint); + } else { + mPaint.setShader(null); + canvas.drawBitmap(mFrontBitmap, mSrcRect, getBounds(), mPaint); + } } private void scheduleDecodeLocked() { @@ -391,6 +429,7 @@ public class FrameSequenceDrawable extends Drawable implements Animatable, Runna public void unscheduleSelf(Runnable what) { synchronized (mLock) { mNextFrameToDecode = -1; + mState = 0; } super.unscheduleSelf(what); } |