AnimatedVectorDrawable不在开始状态

时间:2018-10-31 10:50:08

标签: android android-animation android-vectordrawable

我有一个带有自定义ImageView的活动和一个打开相同活动的新实例的按钮。单击ImageView启动AnimatedVectorDrawable的动画。

问题是,如果我在第一个活动中播放动画,它将以结束状态出现在下一个活动中。如果我不会在第一次活动中播放动画或使用常规ImageView,则不会发生这种情况。

API 19中的AnimatedVectorDrawableCompat也不会发生此问题。

此行为的原因是什么?

活动:

class TestActivity: AppCompatActivity() {

    companion object {
        fun start(context: Context){
            context.startActivity(Intent(context, TestActivity::class.java))
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test)

        myImageView.setOnClickListener {
            (myImageView.drawable as? Animatable)?.start()
        }

        button.setOnClickListener {
            TestActivity.start(this)
        }
    }
}

自定义ImageView:

class MyImageView (context: Context, attrs: AttributeSet?) : ImageView(context, attrs) {

    init {
        val anim = ContextCompat.getDrawable(context, R.drawable.anim)
        setImageDrawable(anim)
    }
}

动画资源:

<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="24dp"
            android:height="24dp"
            android:viewportWidth="24.0"
            android:viewportHeight="24.0">
            <path
                android:name="path"
                android:strokeColor="#fff"
                android:strokeWidth="2"
                android:pathData="M 3, 7 L 21, 7 M 3, 12 L 21, 12 M 3, 17 L 21, 17"/>
        </vector>

    </aapt:attr>
    <target android:name="path" >
        <aapt:attr name="android:animation">
            <set android:interpolator="@android:anim/accelerate_decelerate_interpolator">
                <objectAnimator
                    android:duration="200"
                    android:propertyName="pathData"
                    android:valueFrom="M 3, 7 L 21, 7 M 3, 12 L 21, 12 M 3, 17 L 21, 17"
                    android:valueTo="M 5.5, 5.5 L 12,12  M 5.5, 18.5  L 18.5, 5.5 M 12,12 L 18.5, 18.5"
                    android:valueType="pathType"/>
            </set>
        </aapt:attr>
    </target>
</animated-vector>

1 个答案:

答案 0 :(得分:0)

您的自定义ImageView必须扩展AppCompatImageView

在布局中使用常规ImageView时,此方法可以很好地工作,因为AppCompat会自动将ImageView替换为AppCompatImageView

相关问题