JorgeCastilloPrz的AndroidFillableLoaders库 - SVGPath问题

时间:2016-10-31 04:24:12

标签: java android svg

[编辑] @antonio的解决方案有效。请参阅下面的screeshots以获取证明..

我正在尝试使用JorgeCastilloPrz的AndroidFillableLoaders库,这是我第一次使用SVG(或Path)。如果问题太天真,请耐心等待。

图书馆链接:https://github.com/JorgeCastilloPrz/AndroidFillableLoaders

代码:

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/rl_root_splash_activity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/splash_screen_color_1"
    android:paddingBottom="100dp"
    android:paddingEnd="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingStart="16dp">

<com.github.jorgecastillo.FillableLoader
        android:id="@+id/fillableLoader"
        android:layout_width="200dp"
        android:layout_height="100dp"
        app:fl_clippingTransform="waves"
        app:fl_fillColor="#1c9ade"
        app:fl_fillDuration="5000"
        app:fl_originalHeight="970"
        app:fl_originalWidth="970"
        app:fl_strokeColor="#1c9ade"
        app:fl_strokeDrawingDuration="2000"
        app:fl_strokeWidth="16dp" />

</RelativeLayout>

我使用库中示例应用程序的INDOMINOUS_REX字符串作为SVG路径。链接:https://github.com/JorgeCastilloPrz/AndroidFillableLoaders/blob/master/sampleapp/src/main/java/com/github/jorgecastillo/Paths.java

MainActivity.java

public class SplashActivity extends AppCompatActivity {

    RelativeLayout rlRoot;
    FillableLoader fillableLoader;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        rlRoot = (RelativeLayout) findViewById(R.id.rl_root_splash_activity);

        FillableLoaderBuilder loaderBuilder = new FillableLoaderBuilder();
        fillableLoader = loaderBuilder
                .parentView(rlRoot)
                .svgPath(Const.Paths.INDOMINUS_REX)
                .layoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT))
                .originalDimensions(970, 970)
                .strokeColor(Color.parseColor("#1c9ade"))
                .fillColor(Color.parseColor("#1c9ade"))
                .strokeDrawingDuration(2000)
                .clippingTransform(new WavesClippingTransform())
                .fillDuration(10000)
                .build();

    }

}

当我运行代码时,我得到以下异常:

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
                                                       at com.github.jorgecastillo.svg.SvgPathParser.parsePath(SvgPathParser.java:52)
                                                       at com.github.jorgecastillo.FillableLoader.buildPathData(FillableLoader.java:374)
                                                       at com.github.jorgecastillo.FillableLoader.onSizeChanged(FillableLoader.java:224)
                                                       at android.view.View.sizeChange(View.java:16748)
                                                       at android.view.View.setFrame(View.java:16710)
                                                       at android.view.View.layout(View.java:16627)
                                                       at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
                                                       at android.view.View.layout(View.java:16630)
                                                       at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                       at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                       at android.view.View.layout(View.java:16630)
                                                       at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                       at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
                                                       at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
                                                       at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
                                                       at android.view.View.layout(View.java:16630)
                                                       at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                       at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                       at android.view.View.layout(View.java:16630)
                                                       at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                       at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
                                                       at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
                                                       at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
                                                       at android.view.View.layout(View.java:16630)
                                                       at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                       at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                       at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                       at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678)
                                                       at android.view.View.layout(View.java:16630)
                                                       at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                       at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171)
                                                       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931)
                                                       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
                                                       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
                                                       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
                                                       at android.view.Choreographer.doCallbacks(Choreographer.java:670)
                                                       at android.view.Choreographer.doFrame(Choreographer.java:606)
                                                       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
                                                       at android.os.Handler.handleCallback(Handler.java:739)
                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                       at android.os.Looper.loop(Looper.java:148)
                                                       at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

有人可以帮我解决这个问题吗?

@antonio的解决方案有效,这是证据......

enter image description here

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:4)

NPE的发生是因为您在com.github.jorgecastillo.FillableLoader中定义了R.layout.activity_splash,但您没有以编程方式设置生成的路径。来自the documentation

  

按代码设置生成的路径(只要在xml布局中声明FillableLoader,就这样做):

fillableLoader.setSvgPath(String generatedSvgPath);

要解决此问题,您的onCreate可以是这样的:

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);

    rlRoot = (RelativeLayout) findViewById(R.id.rl_root_splash_activity);

    fillableLoader = (FillableLoader) findViewById(R.id.fillableLoader);
    fillableLoader.setSvgPath(Const.Paths.INDOMINUS_REX);
}

请注意,您已在布局中以编程方式定义了FillableLoader,但导致NPE的定义是布局中的定义,因为它缺少setSvgPath

其他选项是从您的布局中删除FillableLoader定义,因为您使用FillableLoaderBuilder以编程方式生成它:

<强> activity_splash.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/rl_root_splash_activity"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/splash_screen_color_1"
            android:paddingBottom="100dp"
            android:paddingEnd="16dp"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:paddingStart="16dp">
</RelativeLayout>

SplashActivity.onCreate方法

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    rlRoot = (RelativeLayout) findViewById(R.id.rl_root_splash_activity);

    FillableLoaderBuilder loaderBuilder = new FillableLoaderBuilder();
    fillableLoader = loaderBuilder
            .parentView(rlRoot)
            .svgPath(Const.Paths.INDOMINUS_REX)
            .layoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT))
            .originalDimensions(970, 970)
            .strokeColor(Color.parseColor("#1c9ade"))
            .fillColor(Color.parseColor("#1c9ade"))
            .strokeDrawingDuration(2000)
            .clippingTransform(new WavesClippingTransform())
            .fillDuration(10000)
            .build();
    fillableLoader.setSvgPath(Paths.INDOMINUS_REX);
}

请注意,在这两种情况下,您都需要调用fillableLoader.start();来查看动画。