Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit a70445f

Browse files
jbeta51Skia Commit-Bot
authored andcommitted
reland: configure attributes for SkottieView to be initialized from XML layout
Original CL: https://skia-review.googlesource.com/c/skia/+/301716 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/301716 Reviewed-by: Stan Iliev <stani@google.com> Commit-Queue: Jorge Betancourt <jmbetancourt@google.com> Change-Id: I30b782fb6c3edfb6552ed733bd1948bda5f6b0b4 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/308717
1 parent 1fbb331 commit a70445f

File tree

3 files changed

+67
-15
lines changed

3 files changed

+67
-15
lines changed

platform_tools/android/apps/skottie/skottielib/src/main/java/org/skia/skottie/SkottieView.java

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
package org.skia.skottie;
99

1010
import android.content.Context;
11+
import android.content.res.TypedArray;
12+
import android.graphics.Color;
1113
import android.net.Uri;
1214
import android.util.AttributeSet;
1315
import android.view.SurfaceView;
@@ -18,46 +20,86 @@
1820
import android.widget.FrameLayout;
1921
import java.io.FileNotFoundException;
2022
import java.io.InputStream;
23+
import org.skia.skottielib.R;
2124

2225
public class SkottieView extends FrameLayout {
2326

2427
private SkottieAnimation mAnimation;
25-
private View backingView;
28+
private View mbackingView;
2629

27-
//TODO add and access custom attributes for SkottieView
30+
private static final int BACKING_VIEW_TEXTURE = 0;
31+
private static final int BACKING_VIEW_SURFACE = 1;
32+
33+
34+
// SkottieView constructor when initialized in XML layout
2835
public SkottieView(Context context, AttributeSet attrs) {
2936
super(context, attrs);
37+
TypedArray a = context.getTheme()
38+
.obtainStyledAttributes(attrs, R.styleable.SkottieView, 0, 0);
39+
try {
40+
switch (a.getInteger(R.styleable.SkottieView_backing_view, -1)) {
41+
case BACKING_VIEW_TEXTURE:
42+
mbackingView = new TextureView(context);
43+
((TextureView) mbackingView).setOpaque(false);
44+
break;
45+
case BACKING_VIEW_SURFACE:
46+
mbackingView = new SurfaceView(context);
47+
int bg = a.getColor(R.styleable.SkottieView_background_color, -1);
48+
if (bg == -1) {
49+
throw new RuntimeException("background_color attribute "
50+
+ "needed for SurfaceView");
51+
}
52+
if (Color.alpha(bg) != 255) {
53+
throw new RuntimeException("background_color cannot be transparent");
54+
}
55+
break;
56+
default:
57+
throw new RuntimeException("backing_view attribute needed to "
58+
+ "specify between texture_view or surface_view");
59+
}
60+
} finally {
61+
a.recycle();
62+
}
63+
initBackingView();
3064
}
31-
65+
66+
// SkottieView builder constructor
3267
private SkottieView(Context context, SkottieViewBuilder builder) {
33-
super(context);
68+
super(context, builder.attrs, builder.defStyleAttr);
3469
// create the backing view
3570
if (builder.advancedFeatures) {
3671
// backing view must be SurfaceView
37-
backingView = new SurfaceView(context, builder.attrs, builder.defStyleAttr);
72+
mbackingView = new SurfaceView(context);
3873
} else {
39-
backingView = new TextureView(context, builder.attrs, builder.defStyleAttr);
40-
((TextureView)backingView).setOpaque(false);
74+
mbackingView = new TextureView(context);
75+
((TextureView) mbackingView).setOpaque(false);
4176
}
42-
backingView.setLayoutParams(new ViewGroup.LayoutParams(
77+
initBackingView();
78+
}
79+
80+
private void initBackingView() {
81+
mbackingView.setLayoutParams(new ViewGroup.LayoutParams(
4382
ViewGroup.LayoutParams.MATCH_PARENT,
4483
ViewGroup.LayoutParams.MATCH_PARENT));
45-
addView(backingView);
84+
addView(mbackingView);
4685
}
4786

4887
//TODO handle SurfaceView
4988
public void setSource(InputStream inputStream) {
50-
mAnimation = SkottieRunner.getInstance().createAnimation(((TextureView)backingView), inputStream);
89+
mAnimation = SkottieRunner.getInstance()
90+
.createAnimation(((TextureView) mbackingView), inputStream);
5191
}
5292

5393
public void setSource(int resId) {
54-
InputStream inputStream = backingView.getResources().openRawResource(resId);
55-
mAnimation = SkottieRunner.getInstance().createAnimation(((TextureView)backingView), inputStream);
94+
InputStream inputStream = mbackingView.getResources().openRawResource(resId);
95+
mAnimation = SkottieRunner.getInstance()
96+
.createAnimation(((TextureView) mbackingView), inputStream);
5697
}
5798

5899
public void setSource(Context context, Uri uri) throws FileNotFoundException {
59100
InputStream inputStream = context.getContentResolver().openInputStream(uri);
60-
mAnimation = SkottieRunner.getInstance().createAnimation(((TextureView)backingView), inputStream);
101+
mAnimation = SkottieRunner.getInstance()
102+
.createAnimation(((TextureView) mbackingView), inputStream);
61103
}
62104

63105
public SkottieAnimation getSkottieAnimation() {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<declare-styleable name="SkottieView">
4+
<attr name="backing_view" format="enum">
5+
<enum name="texture_view" value="0"/>
6+
<enum name="surface_view" value="1"/>
7+
</attr>
8+
<attr name="background_color" format="color"/>
9+
</declare-styleable>
10+
</resources>

platform_tools/android/apps/skottie/src/main/res/layout/main_layout.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
android:layout_height="match_parent"
1414
android:layout_width="match_parent"
1515
android:layout_gravity="center"
16-
android:columnCount="1"
17-
>
16+
android:columnCount="1">
1817
</GridLayout>
1918

19+
2020
</LinearLayout>

0 commit comments

Comments
 (0)