[androidkit] Add Skottie sample

Convert existing SkottieActivity to use a Skottie Sample, and also add
one to the cube demo.

Change-Id: I86193889d293ddd653b0cac7200e830fa873ea69
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/413736
Reviewed-by: Jorge Betancourt <jmbetancourt@google.com>
This commit is contained in:
Florin Malita 2021-05-28 12:50:07 -04:00 committed by Florin Malita
parent 8c8abd8f9f
commit ce6fe3fe80
3 changed files with 65 additions and 33 deletions

View File

@ -29,6 +29,7 @@ import org.skia.androidkit.util.SurfaceRenderer;
import org.skia.androidkitdemo1.samples.ImageShaderSample;
import org.skia.androidkitdemo1.samples.Sample;
import org.skia.androidkitdemo1.samples.SkottieSample;
import static java.lang.Math.tan;
@ -161,7 +162,7 @@ class CubeRenderer extends SurfaceRenderer implements GestureDetector.OnGestureL
final float rot = (float) Math.PI;
faces = new Face[] {
new Face(0, -rot/2, new ImageShaderSample(res, R.raw.brickwork_texture)),
new Face(0, 0 , new SolidColorSample(1, 0, 0, 1)),
new Face(0, 0 , new SkottieSample(res, R.raw.im_thirsty)),
new Face(0, rot , new SolidColorSample(0, 1, 0, 1)),
new Face(rot/2, 0 , new SolidColorSample(0, 0, 1, 1)),
new Face(-rot/2, 0, new SolidColorSample(1, 1, 0, 1)),

View File

@ -8,6 +8,7 @@
package org.skia.androidkitdemo1;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
@ -15,39 +16,27 @@ import android.view.SurfaceView;
import java.io.InputStream;
import org.skia.androidkit.*;
import org.skia.androidkit.util.*;
import org.skia.androidkitdemo1.samples.SkottieSample;
class SkottieAnimationRenderer extends SurfaceRenderer {
private SkottieAnimation mAnimation;
private Matrix mAnimationMatrix;
private SkottieSample mSample;
private float mSurfaceWidth,
mSurfaceHeight;
SkottieAnimationRenderer(SkottieAnimation animation) {
mAnimation = animation;
SkottieAnimationRenderer(Resources res, int resID) {
mSample = new SkottieSample(res, resID);
}
@Override
protected void onSurfaceInitialized(Surface surface) {
// Scale to fit/center.
float sx = surface.getWidth() / mAnimation.getWidth();
float sy = surface.getHeight() / mAnimation.getHeight();
float s = Math.min(sx, sy);
mAnimationMatrix = new Matrix()
.translate((surface.getWidth() - s * mAnimation.getWidth()) / 2,
(surface.getHeight() - s * mAnimation.getHeight()) / 2)
.scale(s, s);
mSurfaceWidth = surface.getWidth();
mSurfaceHeight = surface.getHeight();
}
@Override
protected void onRenderFrame(Canvas canvas, long ms) {
double t = (double)ms / 1000 % mAnimation.getDuration();
mAnimation.seekTime(t);
canvas.save();
canvas.concat(mAnimationMatrix);
canvas.drawColor(1, 1, 1, 1);
mAnimation.render(canvas);
canvas.restore();
mSample.render(canvas, ms, 0, 0, mSurfaceWidth, mSurfaceHeight);
}
}
@ -62,16 +51,6 @@ public class SkottieAnimationActivity extends Activity {
setContentView(R.layout.activity_animation);
SurfaceView sv = findViewById(R.id.surfaceView);
try {
InputStream is = getResources().openRawResource(R.raw.im_thirsty);
byte[] data = new byte[is.available()];
is.read(data);
SkottieAnimation animation = new SkottieAnimation(new String(data));
sv.getHolder().addCallback(new SkottieAnimationRenderer(animation));
} catch (Exception e) {
Log.e("AndroidKit", "Could not load animation resource: " + R.raw.im_thirsty);
}
sv.getHolder().addCallback(new SkottieAnimationRenderer(getResources(), R.raw.im_thirsty));
}
}

View File

@ -0,0 +1,52 @@
/*
* Copyright 2021 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
package org.skia.androidkitdemo1.samples;
import android.content.res.Resources;
import java.io.InputStream;
import org.skia.androidkit.Canvas;
import org.skia.androidkit.Matrix;
import org.skia.androidkit.SkottieAnimation;
import org.skia.androidkitdemo1.samples.Sample;
public class SkottieSample implements Sample {
private SkottieAnimation mAnimation;
public SkottieSample(Resources res, int resId) {
String json = "";
try {
InputStream is = res.openRawResource(resId);
byte[] data = new byte[is.available()];
is.read(data);
json = new String(data);
} catch (Exception e) {}
mAnimation = new SkottieAnimation(json);
}
public void render(Canvas canvas, long ms, float left, float top, float right, float bottom) {
double t = (double)ms / 1000 % mAnimation.getDuration();
mAnimation.seekTime(t);
float w = right - left,
h = bottom - top,
s = Math.min(w / mAnimation.getWidth(),
h / mAnimation.getHeight());
canvas.save();
canvas.concat(new Matrix().translate(left + (w - s*mAnimation.getWidth() )/2,
top + (h - s*mAnimation.getHeight())/2)
.scale(s, s));
mAnimation.render(canvas);
canvas.restore();
}
}