8package org.skia.skottie;
19import android.widget.GridLayout;
21import java.io.FileNotFoundException;
22import java.util.ArrayList;
24import java.util.concurrent.CountDownLatch;
25import java.util.concurrent.TimeUnit;
26import java.util.concurrent.TimeoutException;
28import static java.lang.Math.ceil;
29import static java.lang.Math.sqrt;
33 private final static long TIME_OUT_MS = 10000;
35 private CountDownLatch mEnterAnimationFence =
new CountDownLatch(1);
37 private GridLayout mGrid;
38 private int mRowCount = 0;
39 private int mColumnCount = 0;
40 private int mCellWidth = 0;
41 private int mCellHeight = 0;
44 static private List<Uri> mAnimationFiles =
new ArrayList<Uri>();
46 private void populateGrid() {
49 mAnimations =
new ArrayList<SkottieView>();
57 R.raw.white_material_wave_loading
60 for (
int resId : rawAssets) {
63 mAnimations.add(view);
66 for (Uri uri : mAnimationFiles) {
70 mAnimations.add(view);
71 }
catch (FileNotFoundException
e) {
77 getWindowManager().getDefaultDisplay().getSize(
size);
78 int screenWidth =
size.x;
79 int screenHeight = (
int)(
size.y / 1.3f);
81 double unit =
sqrt(mAnimations.size() / 6.0f);
82 mRowCount = (
int)
ceil(3 * unit);
83 mColumnCount = (
int)
ceil(2 * unit);
84 mGrid.setColumnCount(mColumnCount);
85 mGrid.setRowCount(mRowCount);
86 mCellWidth = screenWidth / mColumnCount;
87 mCellHeight = screenHeight / mRowCount;
94 view.setOnClickListener(
new View.OnClickListener(){
95 public void onClick(View view){
96 inflateView((SkottieView)view);
108 static int mInflatedIndex = -1;
111 if (mInflatedIndex >= 0) {
113 SkottieView oldView = mAnimations.get(mInflatedIndex);
114 if (oldView !=
null) {
115 int row = mInflatedIndex / mColumnCount;
116 int column = mInflatedIndex % mColumnCount;
117 addView(oldView, row, column,
false);
122 if (anyView != oldView) {
124 anyView.setVisibility(View.VISIBLE);
132 if (anyView != view) {
134 anyView.setVisibility(View.INVISIBLE);
138 mInflatedIndex = mAnimations.indexOf(view);
140 GridLayout.Spec rowSpec = GridLayout.spec(0, mRowCount, GridLayout.CENTER);
141 GridLayout.Spec colSpec = GridLayout.spec(0, mColumnCount, GridLayout.CENTER);
142 GridLayout.LayoutParams
params =
new GridLayout.LayoutParams(rowSpec, colSpec);
143 params.width = ViewGroup.LayoutParams.MATCH_PARENT;
144 params.height = ViewGroup.LayoutParams.MATCH_PARENT;
146 mGrid.updateViewLayout(view,
params);
149 private void refreshGrid() {
150 mGrid.removeAllViews();
155 addView(view, row, column,
true);
157 if (column >= mColumnCount) {
164 private void addView(SkottieView view,
int row ,
int column,
boolean addView) {
165 GridLayout.Spec rowSpec = GridLayout.spec(row, 1, GridLayout.CENTER);
166 GridLayout.Spec colSpec = GridLayout.spec(column, 1, GridLayout.CENTER);
167 GridLayout.LayoutParams
params =
new GridLayout.LayoutParams(rowSpec, colSpec);
168 params.width = mCellWidth;
169 params.height = mCellHeight;
171 mGrid.addView(view,
params);
173 mGrid.updateViewLayout(view,
params);
177 private void startAnimation() {
178 for (SkottieView view : mAnimations) {
183 private void stopAnimation() {
184 for (SkottieView view : mAnimations) {
189 private void addLottie(Uri uri)
throws FileNotFoundException {
190 int animations = mAnimations.size();
191 if (animations < mRowCount * mColumnCount) {
192 SkottieView view =
new SkottieView(
this);
193 view.setSource(
this, uri);
194 int row = animations / mColumnCount;
195 int column = animations % mColumnCount;
196 mAnimations.add(view);
197 mAnimationFiles.add(uri);
198 view.setOnClickListener(
new View.OnClickListener(){
199 public void onClick(View view){
200 inflateView((SkottieView)view);
203 addView(view, row, column,
true);
207 mAnimationFiles.add(uri);
216 super.onEnterAnimationComplete();
217 mEnterAnimationFence.countDown();
221 if (!mEnterAnimationFence.await(TIME_OUT_MS, TimeUnit.MILLISECONDS)) {
222 throw new TimeoutException();
226 private void createLayout() {
227 setContentView(
R.layout.main_layout);
228 Button open = (Button)findViewById(
R.id.open_lottie);
229 open.setOnClickListener(
this);
231 Button play = (Button)findViewById(
R.id.play);
232 play.setOnClickListener(
this);
233 Button pause = (Button)findViewById(
R.id.pause);
234 pause.setOnClickListener(
this);
235 Button
reset = (Button)findViewById(
R.id.reset);
236 reset.setOnClickListener(
this);
238 mGrid = (GridLayout)findViewById(
R.id.grid_lotties);
239 mGrid.setBackgroundColor(
Color.LTGRAY);
245 protected void onCreate(Bundle savedInstanceState) {
246 super.onCreate(savedInstanceState);
256 static final int PICK_FILE_REQUEST = 2;
260 switch(view.getId()) {
261 case R.id.open_lottie:
262 Intent intent =
new Intent();
263 intent.setType(
"application/json");
264 Intent
i = Intent.createChooser(intent,
"View Default File Manager");
265 startActivityForResult(
i, PICK_FILE_REQUEST);
288 super.onActivityResult(requestCode, resultCode,
data);
289 if (resultCode == Activity.RESULT_OK) {
290 if (requestCode == PICK_FILE_REQUEST &&
data !=
null) {
292 Uri uri =
data.getData();
296 }
catch (FileNotFoundException
e) {
void onCreate(Bundle savedInstanceState)
void onEnterAnimationComplete()
void onActivityResult(int requestCode, int resultCode, Intent data)
void waitForEnterAnimationComplete()
static int mInflatedIndex
void setSource(InputStream inputStream)
const EmbeddedViewParams * params
SK_API sk_sp< SkShader > Color(SkColor)
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
SIN Vec< N, float > sqrt(const Vec< N, float > &x)
SIN Vec< N, float > ceil(const Vec< N, float > &x)
std::shared_ptr< const fml::Mapping > data