|
8 | 8 | package org.skia.skottie; |
9 | 9 |
|
10 | 10 | import android.content.Context; |
| 11 | +import android.content.res.TypedArray; |
| 12 | +import android.graphics.Color; |
11 | 13 | import android.net.Uri; |
12 | 14 | import android.util.AttributeSet; |
13 | 15 | import android.view.SurfaceView; |
|
18 | 20 | import android.widget.FrameLayout; |
19 | 21 | import java.io.FileNotFoundException; |
20 | 22 | import java.io.InputStream; |
| 23 | +import org.skia.skottielib.R; |
21 | 24 |
|
22 | 25 | public class SkottieView extends FrameLayout { |
23 | 26 |
|
24 | 27 | private SkottieAnimation mAnimation; |
25 | | - private View backingView; |
| 28 | + private View mbackingView; |
26 | 29 |
|
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 |
28 | 35 | public SkottieView(Context context, AttributeSet attrs) { |
29 | 36 | 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(); |
30 | 64 | } |
31 | | - |
| 65 | + |
| 66 | + // SkottieView builder constructor |
32 | 67 | private SkottieView(Context context, SkottieViewBuilder builder) { |
33 | | - super(context); |
| 68 | + super(context, builder.attrs, builder.defStyleAttr); |
34 | 69 | // create the backing view |
35 | 70 | if (builder.advancedFeatures) { |
36 | 71 | // backing view must be SurfaceView |
37 | | - backingView = new SurfaceView(context, builder.attrs, builder.defStyleAttr); |
| 72 | + mbackingView = new SurfaceView(context); |
38 | 73 | } 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); |
41 | 76 | } |
42 | | - backingView.setLayoutParams(new ViewGroup.LayoutParams( |
| 77 | + initBackingView(); |
| 78 | + } |
| 79 | + |
| 80 | + private void initBackingView() { |
| 81 | + mbackingView.setLayoutParams(new ViewGroup.LayoutParams( |
43 | 82 | ViewGroup.LayoutParams.MATCH_PARENT, |
44 | 83 | ViewGroup.LayoutParams.MATCH_PARENT)); |
45 | | - addView(backingView); |
| 84 | + addView(mbackingView); |
46 | 85 | } |
47 | 86 |
|
48 | 87 | //TODO handle SurfaceView |
49 | 88 | public void setSource(InputStream inputStream) { |
50 | | - mAnimation = SkottieRunner.getInstance().createAnimation(((TextureView)backingView), inputStream); |
| 89 | + mAnimation = SkottieRunner.getInstance() |
| 90 | + .createAnimation(((TextureView) mbackingView), inputStream); |
51 | 91 | } |
52 | 92 |
|
53 | 93 | 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); |
56 | 97 | } |
57 | 98 |
|
58 | 99 | public void setSource(Context context, Uri uri) throws FileNotFoundException { |
59 | 100 | InputStream inputStream = context.getContentResolver().openInputStream(uri); |
60 | | - mAnimation = SkottieRunner.getInstance().createAnimation(((TextureView)backingView), inputStream); |
| 101 | + mAnimation = SkottieRunner.getInstance() |
| 102 | + .createAnimation(((TextureView) mbackingView), inputStream); |
61 | 103 | } |
62 | 104 |
|
63 | 105 | public SkottieAnimation getSkottieAnimation() { |
|
0 commit comments