首页 > 解决方案 > 如何在android中使用动画矢量

问题描述

我从ShapeShifter.com创建了一个可绘制的动画矢量,我正在使用ImageView它来测试它。唯一的问题是它不起作用。不知道是什么错误。

avd_amin:

<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:name="vector"
        android:width="24dp"
        android:height="24dp"
        android:viewportWidth="100"
        android:viewportHeight="100">
        <path
            android:name="search"
            android:pathData="M 37.5 8.333 C 21.442 8.333 8.333 21.442 8.333 37.5 C 8.333 53.558 21.442 66.667 37.5 66.667 C 44.783 66.667 51.438 63.95 56.558 59.504 L 58.333 61.279 L 58.333 66.667 L 83.333 91.667 L 91.667 83.333 L 66.667 58.333 L 61.279 58.333 L 59.504 56.558 C 63.95 51.438 66.667 44.783 66.667 37.5 C 66.667 21.442 53.558 8.333 37.5 8.333 Z M 37.5 16.667 C 49.054 16.667 58.333 25.946 58.333 37.5 C 58.333 49.054 49.054 58.333 37.5 58.333 C 25.946 58.333 16.667 49.054 16.667 37.5 C 16.667 25.946 25.946 16.667 37.5 16.667 Z"
            android:fillColor="#000"
            android:strokeWidth="1"/>
        <clip-path
            android:name="path"
            android:pathData="M 37.5 8.333 C 21.442 8.333 8.333 21.442 8.333 37.5 C 8.333 53.558 21.442 66.667 37.5 66.667 C 44.783 66.667 51.438 63.95 56.558 59.504 L 58.333 61.279 L 58.333 66.667 L 83.333 91.667 L 91.667 83.333 L 66.667 58.333 L 61.279 58.333 L 59.504 56.558 C 63.95 51.438 66.667 44.783 66.667 37.5 C 66.667 21.442 53.558 8.333 37.5 8.333 Z M 37.5 16.667 C 49.054 16.667 58.333 25.946 58.333 37.5 C 58.333 49.054 49.054 58.333 37.5 58.333 C 25.946 58.333 16.667 49.054 16.667 37.5 C 16.667 25.946 25.946 16.667 37.5 16.667 Z"/>
        <path
            android:name="multiply"
            android:pathData="M 24 24.5 L 20.6 28 L 31.5 39 L 42.5 50 L 31.5 61 L 20.5 72 L 24.3 75.7 L 28 79.5 L 38.9 68.6 L 49.8 57.7 L 61.1 68.4 L 72.4 79 L 75.9 75.5 L 79.5 72 L 68.5 61 L 57.5 50 L 68.5 39 L 79.5 28 L 75.9 24.5 L 72.4 20.9 L 61.1 31.6 L 49.9 42.3 L 39.2 31.7 C 33.3 25.8 28.2 21 27.9 21 C 27.6 21 25.9 22.6 24 24.5 Z"
            android:fillColor="#000000"
            android:strokeWidth="1"/>
    </vector>
</aapt:attr>
<target android:name="search">
    <aapt:attr name="android:animation">
        <objectAnimator
            android:propertyName="pathData"
            android:duration="100"
            android:valueFrom="M 22.801 12.327 C 18.48 14.863 14.863 18.48 12.327 22.801 C 9.79 27.122 8.333 32.147 8.333 37.5 C 8.333 39.794 8.601 42.028 9.106 44.172 C 9.612 46.316 10.355 48.37 11.307 50.305 C 12.258 52.24 13.418 54.055 14.757 55.721 C 16.095 57.388 17.612 58.905 19.279 60.243 C 20.945 61.582 22.76 62.742 24.695 63.693 C 26.63 64.645 28.684 65.388 30.828 65.894 C 31.9 66.146 32.995 66.34 34.108 66.47 C 35.221 66.6 36.353 66.667 37.5 66.667 C 44.783 66.667 51.438 63.95 56.558 59.504 L 58.333 61.279 L 58.333 66.667 L 83.333 91.667 L 87.5 87.5 L 91.667 83.333 L 66.667 58.333 L 61.279 58.333 L 59.504 56.558 C 63.95 51.438 66.667 44.783 66.667 37.5 C 66.667 34.824 66.303 32.229 65.621 29.764 C 64.94 27.298 63.942 24.962 62.673 22.801 C 61.405 20.641 59.867 18.656 58.105 16.895 C 56.344 15.133 54.359 13.595 52.199 12.327 C 50.038 11.058 47.702 10.06 45.236 9.379 C 42.771 8.697 40.176 8.333 37.5 8.333 L 37.5 8.333 C 32.147 8.333 27.122 9.79 22.801 12.327 M 37.5 16.667 C 38.842 16.667 40.154 16.792 41.423 17.031 C 42.748 17.281 44.028 17.655 45.25 18.142 C 46.471 18.628 47.635 19.226 48.729 19.923 C 49.804 20.609 50.812 21.391 51.739 22.257 C 52.682 23.138 53.543 24.106 54.308 25.149 C 55.029 26.131 55.666 27.179 56.208 28.283 C 56.918 29.729 57.465 31.271 57.826 32.885 C 58.158 34.368 58.333 35.913 58.333 37.5 C 58.333 38.842 58.208 40.154 57.969 41.423 C 57.725 42.715 57.363 43.964 56.895 45.158 C 56.418 46.372 55.831 47.53 55.146 48.619 C 54.455 49.718 53.665 50.746 52.787 51.692 C 51.923 52.622 50.975 53.473 49.955 54.232 C 48.805 55.087 47.562 55.826 46.244 56.432 C 44.986 57.011 43.659 57.468 42.278 57.789 C 40.745 58.145 39.146 58.333 37.5 58.333 C 35.994 58.333 34.528 58.176 33.115 57.876 C 31.483 57.53 29.923 56.995 28.46 56.293 C 26.918 55.554 25.482 54.632 24.181 53.553 C 22.963 52.543 21.863 51.396 20.904 50.136 C 20.272 49.304 19.7 48.423 19.197 47.498 C 18.761 46.698 18.376 45.865 18.046 45.005 C 17.637 43.939 17.314 42.831 17.083 41.688 C 16.81 40.336 16.667 38.936 16.667 37.5 C 16.667 35.983 16.827 34.505 17.131 33.082 C 17.353 32.042 17.652 31.032 18.022 30.057 C 18.572 28.61 19.277 27.242 20.117 25.972 C 20.821 24.91 21.62 23.916 22.502 23.004 C 23.57 21.898 24.761 20.911 26.053 20.064 C 27.273 19.264 28.583 18.59 29.965 18.058 C 31.07 17.632 32.222 17.297 33.411 17.063 C 34.732 16.803 36.099 16.667 37.5 16.667 L 37.5 16.667"
            android:valueTo="M 27.9 21 C 27.6 21 25.9 22.6 24 24.5 C 24 24.5 24 24.5 24 24.5 C 22.867 25.667 21.733 26.833 20.6 28 C 24.233 31.667 27.867 35.333 31.5 39 C 35.167 42.667 38.833 46.333 42.5 50 C 38.833 53.667 35.167 57.333 31.5 61 C 27.833 64.667 24.167 68.333 20.5 72 C 21.767 73.233 23.033 74.467 24.3 75.7 C 25.533 76.967 26.767 78.233 28 79.5 C 31.633 75.867 35.267 72.233 38.9 68.6 C 42.533 64.967 46.167 61.333 49.8 57.7 L 55.45 63.05 L 61.1 68.4 L 72.4 79 L 75.9 75.5 L 79.5 72 L 68.5 61 L 63 55.5 L 57.5 50 C 61.167 46.333 64.833 42.667 68.5 39 C 72.167 35.333 75.833 31.667 79.5 28 C 78.3 26.833 77.1 25.667 75.9 24.5 C 74.733 23.3 73.567 22.1 72.4 20.9 C 68.633 24.467 64.867 28.033 61.1 31.6 C 57.367 35.167 53.633 38.733 49.9 42.3 C 48.117 40.533 46.333 38.767 44.55 37 L 39.2 31.7 C 33.3 25.8 28.2 21 27.9 21 M 37.501 37.493 C 37.501 37.493 37.501 37.493 37.501 37.493 C 37.501 37.493 37.501 37.493 37.501 37.493 C 37.501 37.493 37.501 37.493 37.501 37.493 C 37.501 37.493 37.501 37.493 37.501 37.493 C 37.501 37.493 37.501 37.493 37.501 37.493 C 37.501 37.493 37.501 37.493 37.501 37.493 C 37.501 37.493 37.501 37.493 37.501 37.493 C 37.501 37.493 37.501 37.493 37.501 37.493 C 37.501 37.493 37.501 37.493 37.501 37.493 C 37.501 37.493 37.501 37.493 37.501 37.493 C 37.501 37.493 37.501 37.493 37.501 37.493 C 37.501 37.493 37.501 37.493 37.501 37.493 C 37.501 37.493 37.501 37.493 37.501 37.493 C 37.501 37.493 37.501 37.493 37.501 37.493 C 37.501 37.493 37.501 37.493 37.501 37.493 C 37.501 37.493 37.501 37.493 37.501 37.493 C 37.501 37.493 37.501 37.493 37.501 37.493 C 37.501 37.493 37.501 37.493 37.501 37.493 C 37.501 37.493 37.501 37.493 37.501 37.493 C 37.501 37.493 37.501 37.493 37.501 37.493 C 37.501 37.493 37.501 37.493 37.501 37.493 C 37.501 37.493 37.501 37.493 37.501 37.493 C 37.501 37.493 37.501 37.493 37.501 37.493 C 37.501 37.493 37.501 37.493 37.501 37.493 C 37.501 37.493 37.501 37.493 37.501 37.493 C 37.501 37.493 37.501 37.493 37.501 37.493 C 37.501 37.493 37.501 37.493 37.501 37.493 C 37.501 37.493 37.501 37.493 37.501 37.493 C 37.501 37.493 37.501 37.493 37.501 37.493 C 37.501 37.493 37.501 37.493 37.501 37.493 C 37.501 37.493 37.501 37.493 37.501 37.493 C 37.501 37.493 37.501 37.493 37.501 37.493 L 37.501 37.493"
            android:valueType="pathType"
            android:interpolator="@android:interpolator/fast_out_slow_in"/>
    </aapt:attr>
</target>

活动主.xml:

<ImageView
    android:id="@+id/searchview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="24dp"
    android:src="@drawable/avd_anim"
    android:layout_marginEnd="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/smartMaterialSpinner1" />

MainActivity.java

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    searchview = (ImageView)findViewById(R.id.searchview);
    searchview.setBackgroundResource(R.drawable.avd_anim);
    avd =(AnimationDrawable)searchview.getBackground();
    searchview.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            avd.start();
        }
    });

错误信息:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}: java.lang.ClassCastException: android.graphics.drawable.AnimatedVectorDrawable cannot be cast to android.graphics.drawable.AnimationDrawable

标签: android

解决方案


我已经进一步更新了代码。 参考

    searchtoclose = (AnimatedVectorDrawable)getDrawable(R.drawable.avd_anim);
    closetosearch = (AnimatedVectorDrawable)getDrawable(R.drawable.close_to_search);



searchview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                AnimatedVectorDrawable drawable = isSearch ? searchtoclose : closetosearch;
                drawable.registerAnimationCallback(new Animatable2.AnimationCallback() {
                    @Override
                    public void onAnimationStart(Drawable drawable) {
                        super.onAnimationStart(drawable);
                    }

                    @Override
                    public void onAnimationEnd(Drawable drawable) {
                        super.onAnimationEnd(drawable);
                    }
                });
                ImageView searchview = ((ImageView) view);
                searchview.setImageDrawable(drawable);
                drawable.start();
                isSearch = !isSearch
            }
        });

推荐阅读