Android 水平无限期进度条,基于animated-vector

版权声明:本文为博主原创文章,未经博主允许不得转载。欢迎访问http://github.com/shenhuanet 转载请注明出处: https://blog.csdn.net/klxh2009/article/details/80407336

Android 水平无限期进度条,基于 animated-vector

简单易用,修改方便
仅适用于 Android Lollipop 5.0 API level 21 及以上

效果:
水平无限期进度条

资源文件结构:
文件引用

资源文件内容:

  • res/animator/anim_apih_rect1_grp.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:aapt="http://schemas.android.com/aapt">
    <objectAnimator
        android:duration="1500"
        android:pathData="M -522.59998,0 c 48.89972,0 166.02656,0 301.21729,0 c 197.58128,0 420.9827,0 420.9827,0"
        android:propertyXName="translateX"
        android:repeatCount="infinite">
        <aapt:attr name="android:interpolator">
            <pathInterpolator android:pathData="M 0,0 L 0.2 0 C 0.3958333333336,0 0.474845090492,0.206797621729 0.5916666666664,0.417082932942 C 0.7151610251224,0.639379624869 0.81625,0.974556908664 1,1" />
        </aapt:attr>
    </objectAnimator>
    <objectAnimator
        android:duration="1500"
        android:pathData="M 0 0.1 L 1 0.826849212646 L 2 0.1"
        android:propertyYName="scaleX"
        android:repeatCount="infinite">
        <aapt:attr name="android:interpolator">
            <pathInterpolator android:pathData="M 0 0 L 0.3665 0 C 0.47252618112021,0.062409910275 0.61541608570164,0.5 0.68325,0.5 C 0.75475061236836,0.5 0.75725829093844,0.814510098964 1,1" />
        </aapt:attr>
    </objectAnimator>
</set>
  • res/animator/anim_apih_rect2_grp.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:aapt="http://schemas.android.com/aapt">
    <objectAnimator
        android:duration="1500"
        android:pathData="M -197.60001,0 c 14.28182,0 85.07782,0 135.54689,0 c 54.26191,0 90.42461,0 168.24331,0 c 144.72154,0 316.40982,0 316.40982,0"
        android:propertyXName="translateX"
        android:repeatCount="infinite">
        <aapt:attr name="android:interpolator">
            <pathInterpolator android:pathData="M 0,0 C 0.0375,0 0.128764607715,0.0895380946618 0.25,0.218553507947 C 0.322410320025,0.295610602487 0.436666666667,0.417591408114 0.483333333333,0.489826169306 C 0.69,0.80972296795 0.793333333333,0.950016125212 1,1 " />
        </aapt:attr>
    </objectAnimator>
    <objectAnimator
        android:duration="1500"
        android:pathData="M 0,0.1 L 1,0.571379510698 L 2,0.909950256348 L 3,0.1"
        android:propertyYName="scaleX"
        android:repeatCount="infinite">
        <aapt:attr name="android:interpolator">
            <pathInterpolator android:pathData="M 0,0 C 0.06834272400867,0.01992566661414 0.19220331656133,0.15855429260523 0.33333333333333,0.34926160892842 C 0.38410433133433,0.41477913453861 0.54945792615267,0.68136029463551 0.66666666666667,0.68279962777002 C 0.752586273196,0.68179620963216 0.737253971954,0.878896194318 1,1" />
        </aapt:attr>
    </objectAnimator>
</set>
  • res/drawable/avd_progress_indeterminate_horizontal.xml
<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:aapt="http://schemas.android.com/aapt">

    <aapt:attr name="android:drawable">
        <vector
            android:width="360dp"
            android:height="10dp"
            android:viewportHeight="10"
            android:viewportWidth="360">
            <group
                android:name="progress_group"
                android:translateX="180"
                android:translateY="5">
                <path
                    android:name="background_track"
                    android:fillAlpha="0.4"
                    android:fillColor="#7F7F7F"
                    android:pathData="M -180,-1 l 360,0 l 0,2 l -360,0 Z" />
                <group
                    android:name="rect2_grp"
                    android:scaleX="0.1"
                    android:translateX="-197.60001">
                    <path
                        android:name="rect2"
                        android:fillColor="#FF4081"
                        android:pathData="M -144,-1 l 288,0 l 0,2 l -288,0 Z" />
                </group>
                <group
                    android:name="rect1_grp"
                    android:scaleX="0.1"
                    android:translateX="-522.59998">
                    <path
                        android:name="rect1"
                        android:fillColor="#FF4081"
                        android:pathData="M -144,-1 l 288,0 l 0,2 l -288,0 Z" />
                </group>
            </group>
        </vector>
    </aapt:attr>

    <target
        android:name="rect1_grp"
        android:animation="@animator/anim_apih_rect1_grp" />

    <target
        android:name="rect2_grp"
        android:animation="@animator/anim_apih_rect2_grp" />

</animated-vector>

使用:

1、布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.AppCompatImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/avd_progress_indeterminate_horizontal" />

</android.support.constraint.ConstraintLayout>

2、Java 代码

private void start() {
    AppCompatImageView imageView = findViewById(R.id.imageView);
    ((AnimatedVectorDrawableCompat) imageView.getDrawable()).start();
}

参考资料:https://github.com/alexjlockwood/adp-delightful-details

猜你喜欢

转载自blog.csdn.net/klxh2009/article/details/80407336