Skip to content
This repository has been archived by the owner on Feb 6, 2023. It is now read-only.

Commit

Permalink
fix: support reuse bitmap paint and canvas.
Browse files Browse the repository at this point in the history
  • Loading branch information
errnull committed Jun 18, 2019
1 parent b8a85db commit 3df95bb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
Binary file added app/src/main/assets/Goddess.svga
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
public class AnimationFromAssetsActivity extends Activity {

SVGAImageView animationView = null;
int currentIndex = 0;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
Expand All @@ -28,7 +29,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
animationView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
loadAnimation();
animationView.stepToFrame(currentIndex++, false);
}
});
loadAnimation();
Expand All @@ -41,7 +42,7 @@ private void loadAnimation() {
@Override
public void onComplete(@NotNull SVGAVideoEntity videoItem) {
animationView.setVideoItem(videoItem);
animationView.startAnimation();
animationView.stepToFrame(0, true);
}
@Override
public void onError() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,9 @@ internal class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVG
isMatteing = false

matteSprites.get(svgaDrawerSprite.matteKey)?.let {
val matteBitmap = Bitmap.createBitmap(canvas.width, canvas.height, Bitmap.Config.ARGB_8888)
val matteCanvas = Canvas(matteBitmap)
val paint = Paint()
paint.setXfermode(PorterDuffXfermode(PorterDuff.Mode.DST_IN))
drawSprite(it, this.sharedValues.shareMatteCanvas(canvas.width, canvas.height), frameIndex)

drawSprite(it, matteCanvas, frameIndex)
canvas.drawBitmap(matteBitmap, 0f, 0f, paint)
canvas.drawBitmap(this.sharedValues.sharedMatteBitmap(), 0f, 0f, this.sharedValues.shareMattePaint())
canvas.restore()
}
}
Expand All @@ -72,13 +68,9 @@ internal class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVG
// if current sprite is the last one and isMatteing
if (isMatteing && index == sprites.count() - 1) {
matteSprites.get(svgaDrawerSprite.matteKey)?.let {
val matteBitmap = Bitmap.createBitmap(canvas.width, canvas.height, Bitmap.Config.ARGB_8888)
val matteCanvas = Canvas(matteBitmap)
val paint = Paint()
paint.setXfermode(PorterDuffXfermode(PorterDuff.Mode.DST_IN))
drawSprite(it, this.sharedValues.shareMatteCanvas(canvas.width, canvas.height), frameIndex)

drawSprite(it, matteCanvas, frameIndex)
canvas.drawBitmap(matteBitmap, 0f, 0f, paint)
canvas.drawBitmap(this.sharedValues.sharedMatteBitmap(), 0f, 0f, this.sharedValues.shareMattePaint())
canvas.restore()
}
}
Expand Down Expand Up @@ -344,6 +336,10 @@ internal class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVG
private val sharedMatrix = Matrix()
private val sharedMatrix2 = Matrix()

private val shareMattePaint = Paint()
private var shareMatteCanvas: Canvas? = null
private var sharedMatteBitmap: Bitmap? = null

fun sharedPaint(): Paint {
sharedPaint.reset()
return sharedPaint
Expand All @@ -369,6 +365,24 @@ internal class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVG
return sharedMatrix2
}

fun shareMattePaint(): Paint {
shareMattePaint.setXfermode(PorterDuffXfermode(PorterDuff.Mode.DST_IN))
return shareMattePaint
}

fun sharedMatteBitmap(): Bitmap {
return sharedMatteBitmap as Bitmap
}

fun shareMatteCanvas(width: Int, height: Int): Canvas {
if (shareMatteCanvas == null) {
sharedMatteBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ALPHA_8)
shareMatteCanvas = Canvas(sharedMatteBitmap)
}
val matteCanvas = shareMatteCanvas as Canvas
matteCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
return matteCanvas
}
}

class PathCache {
Expand Down

0 comments on commit 3df95bb

Please sign in to comment.