Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkottieView.java
Go to the documentation of this file.
1/*
2 * Copyright 2018 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8package org.skia.skottie;
9
10import android.animation.Animator;
11import android.content.Context;
12import android.content.res.TypedArray;
13import android.graphics.Color;
14import android.net.Uri;
15import android.util.AttributeSet;
16import android.util.Log;
17import android.view.SurfaceView;
18import android.view.TextureView;
19
20import android.view.View;
21import android.view.ViewGroup;
22import android.widget.FrameLayout;
23import java.io.FileNotFoundException;
24import java.io.InputStream;
25
26import org.skia.skottielib.R;
27
28public class SkottieView extends FrameLayout {
29
30 private SkottieAnimation mAnimation;
31 private View mBackingView;
32 private int mBackgroundColor;
33 // Repeat follows Animator API, infinite is represented by -1 (see Animator.DURATION_INFINITE)
34 private int mRepeatCount;
35 private static final int BACKING_VIEW_TEXTURE = 0;
36 private static final int BACKING_VIEW_SURFACE = 1;
37 private final String LOG_TAG = "SkottieView";
38
39 /*
40 * Build function for SkottieViews backed with a texture view
41 * Is the same as calling for a default SkottieView
42 */
44 return new SkottieView(context);
45 }
46
47 /*
48 * Build function for SkottieViews backed with a surface view
49 * Backs the animation surface with a SurfaceView instead, requires background color
50 */
51 public SkottieView buildAsSurface(Context context, int backgroundColor) {
52 SkottieView s = new SkottieView(context);
53 s.mBackingView = new SurfaceView(context);
54 s.mBackgroundColor = backgroundColor;
55 return s;
56 }
57
58 // Basic SkottieView, backing view defaults to TextureView
59 public SkottieView(Context context) {
60 super(context);
61 mBackingView = new TextureView(context);
62 initBackingView();
63 }
64
65 public SkottieView(Context context, AttributeSet attrs) {
66 this(context, attrs, 0, 0);
67 }
68
69 public SkottieView(Context context, AttributeSet attrs, int defStyleAttr) {
70 this(context, attrs, defStyleAttr, 0);
71 }
72
73 // SkottieView constructor when initialized in XML layout
74 public SkottieView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
75 super(context, attrs);
76 TypedArray a = context.getTheme()
77 .obtainStyledAttributes(attrs, R.styleable.SkottieView, defStyleAttr, defStyleRes);
78 try {
79 // set mRepeatCount
80 mRepeatCount = a.getInteger(R.styleable.SkottieView_android_repeatCount, 0);
81 // set backing view and background color
82 switch (a.getInteger(R.styleable.SkottieView_backing_view, -1)) {
83 case BACKING_VIEW_TEXTURE:
84 mBackingView = new TextureView(context);
85 ((TextureView) mBackingView).setOpaque(false);
86 mBackgroundColor = a.getColor(R.styleable.SkottieView_background_color, 0);
87 break;
88 case BACKING_VIEW_SURFACE:
89 if (!a.hasValue(R.styleable.SkottieView_background_color)) {
90 throw new RuntimeException("background_color attribute "
91 + "needed for SurfaceView");
92 }
93 mBackingView = new SurfaceView(context);
94 mBackgroundColor = a.getColor(R.styleable.SkottieView_background_color, -1);
95 if (Color.alpha(mBackgroundColor) != 255) {
96 throw new RuntimeException("background_color cannot be transparent");
97 }
98 break;
99 default:
100 throw new RuntimeException("backing_view attribute needed to "
101 + "specify between texture_view or surface_view");
102 }
103 // set source
104 int src = a.getResourceId(R.styleable.SkottieView_src, -1);
105 if (src != -1) {
106 setSource(src);
107 }
108 } finally {
109 a.recycle();
110 }
111 initBackingView();
112 }
113
114 private void initBackingView() {
115 mBackingView.setLayoutParams(new ViewGroup.LayoutParams(
116 ViewGroup.LayoutParams.MATCH_PARENT,
117 ViewGroup.LayoutParams.MATCH_PARENT));
118 addView(mBackingView);
119 }
120
121 public void setSource(InputStream inputStream) {
122 mAnimation = setSourceHelper(inputStream);
123 }
124
125 public void setSource(int resId) {
126 InputStream inputStream = mBackingView.getResources().openRawResource(resId);
127 mAnimation = setSourceHelper(inputStream);
128 }
129
130 public void setSource(Context context, Uri uri) throws FileNotFoundException {
131 InputStream inputStream = context.getContentResolver().openInputStream(uri);
132 mAnimation = setSourceHelper(inputStream);
133 }
134
135 private SkottieAnimation setSourceHelper(InputStream inputStream) {
136 SkottieAnimation.Config config = null;
137 SkottieAnimation animation;
138 // if there is already an animation, save config and finalize it so as to not confuse GL
139 if (mAnimation != null) {
140 config = mAnimation.getBackingViewConfig();
141 try {
142 mAnimation.finalize();
143 } catch (Throwable t) {
144 Log.e(LOG_TAG, "existing animation couldn't finalize before setting new src");
145 }
146 }
147 if (mBackingView instanceof TextureView) {
148 animation = SkottieRunner.getInstance()
149 .createAnimation(((TextureView) mBackingView), inputStream, mBackgroundColor, mRepeatCount);
150 } else {
151 animation = SkottieRunner.getInstance()
152 .createAnimation(((SurfaceView) mBackingView), inputStream, mBackgroundColor, mRepeatCount);
153 }
154 // restore config settings from previous animation if needed
155 if (config != null) {
156 animation.setBackingViewConfig(config);
157 animation.setProgress(0f);
158 }
159 return animation;
160 }
161
163 return mAnimation;
164 }
165
166 public void removeListener(Animator.AnimatorListener listener) {
167 mAnimation.removeListener(listener);
168 }
169
170 public void addListener(Animator.AnimatorListener listener) {
171 mAnimation.addListener(listener);
172 }
173
174 // progress: a float from 0 to 1 representing the percent into the animation
175 public void seek(float progress) {
176 if(mAnimation != null) {
177 mAnimation.setProgress(progress);
178 }
179 }
180
181 public void play() {
182 if(mAnimation != null) {
183 mAnimation.resume();
184 }
185 }
186
187 public void pause() {
188 if(mAnimation != null) {
189 mAnimation.pause();
190 }
191 }
192
193 public void start() {
194 if(mAnimation != null) {
195 mAnimation.start();
196 }
197 }
198
199 public void stop() {
200 if(mAnimation != null) {
201 mAnimation.end();
202 }
203 }
204
205 public float getProgress() {
206 if(mAnimation != null) {
207 return mAnimation.getProgress();
208 }
209 return -1;
210 }
211
212 public void setRepeatCount(int repeatCount) {
213 mRepeatCount = repeatCount;
214 }
215
216 public void setBackgroundColor(int colorRGB) {
217 mBackgroundColor = colorRGB;
218 }
219
220 static public void setMaxCacheSize(int maxCacheSize) {
222 }
223}
emscripten::val TypedArray
Definition WasmCommon.h:31
void setMaxCacheSize(int maxCacheSize)
static synchronized SkottieRunner getInstance()
void seek(float progress)
void setRepeatCount(int repeatCount)
SkottieView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
SkottieView(Context context, AttributeSet attrs)
void setBackgroundColor(int colorRGB)
SkottieView buildAsTexture(Context context)
void removeListener(Animator.AnimatorListener listener)
static void setMaxCacheSize(int maxCacheSize)
void setSource(Context context, Uri uri)
void setSource(InputStream inputStream)
void addListener(Animator.AnimatorListener listener)
SkottieView(Context context, AttributeSet attrs, int defStyleAttr)
SkottieAnimation getSkottieAnimation()
SkottieView buildAsSurface(Context context, int backgroundColor)
struct MyStruct s
struct MyStruct a[10]
#define R(r)