From d6ae643bd34fd311b7e0b29637e90911797de019 Mon Sep 17 00:00:00 2001 From: Renyu Liu Date: Thu, 15 Jun 2017 16:24:52 -0700 Subject: [PATCH] Support bitmap with opacity animation on Android (#135) --- .../com/facebook/keyframes/KeyframesDrawable.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/android/keyframes/src/main/java/com/facebook/keyframes/KeyframesDrawable.java b/android/keyframes/src/main/java/com/facebook/keyframes/KeyframesDrawable.java index 19bfd20..0eeef2a 100644 --- a/android/keyframes/src/main/java/com/facebook/keyframes/KeyframesDrawable.java +++ b/android/keyframes/src/main/java/com/facebook/keyframes/KeyframesDrawable.java @@ -189,18 +189,23 @@ public void draw(Canvas canvas) { continue; } + // We need paint for drawing (opacity animated) bitmaps and paths + mDrawingPaint.setShader(null); + mDrawingPaint.setAlpha(featureState.getAlpha()); + + // Drawing bitmap final Bitmap backedImage = featureState.getBackedImageBitmap(); final Matrix uniqueFeatureMatrix = featureState.getUniqueFeatureMatrix(); if (backedImage != null && uniqueFeatureMatrix != null) { // This block is for the experimental bitmap supporting canvas.save(); canvas.concat(mScaleMatrix); - canvas.drawBitmap(backedImage, uniqueFeatureMatrix, null); - + canvas.drawBitmap(backedImage, uniqueFeatureMatrix, mDrawingPaint); canvas.restore(); continue; } + // Drawing path pathToDraw = featureState.getCurrentPathForDrawing(); if (pathToDraw == null || pathToDraw.isEmpty()) { continue; @@ -209,7 +214,6 @@ public void draw(Canvas canvas) { canvas.save(); applyScaleAndClipCanvas(canvas, featureState.getCurrentMaskPath(), Region.Op.INTERSECT); } - mDrawingPaint.setShader(null); mDrawingPaint.setStrokeCap(featureState.getStrokeLineCap()); if (featureState.getFillColor() != Color.TRANSPARENT) { mDrawingPaint.setStyle(Paint.Style.FILL); @@ -224,7 +228,6 @@ public void draw(Canvas canvas) { } if (featureState.getStrokeColor() != Color.TRANSPARENT && featureState.getStrokeWidth() > 0) { mDrawingPaint.setColor(featureState.getStrokeColor()); - mDrawingPaint.setAlpha(featureState.getAlpha()); mDrawingPaint.setStyle(Paint.Style.STROKE); mDrawingPaint.setStrokeWidth( featureState.getStrokeWidth() * mScale * mScaleFromCenter * mScaleFromEnd); @@ -484,6 +487,7 @@ public void setupFeatureStateForProgress(float frameProgress) { if (layerTransformMatrix != null && !layerTransformMatrix.isIdentity()) { mFeatureMatrix.postConcat(layerTransformMatrix); } + mFeature.setOpacity(mOpacity, frameProgress); KeyFramedPath path = mFeature.getPath(); if (hasCustomDrawable() || path == null) { return; // skip all the path stuff @@ -494,8 +498,8 @@ public void setupFeatureStateForProgress(float frameProgress) { mFeature.setStrokeWidth(mStrokeWidth, frameProgress); mFeature.setStrokeColor(mStrokeColor, frameProgress); + mFeature.setFillColor(mFillColor, frameProgress); mStrokeWidth.adjustScale(extractScaleFromMatrix(mFeatureMatrix)); - mFeature.setOpacity(mOpacity, frameProgress); if (mFeature.getEffect() != null) { prepareShadersForFeature(mFeature); }