为Shape的笔触宽度设置动画

时间:2015-03-25 20:10:30

标签: android animation android-5.0-lollipop geometry stroke

我有一个带有透明中心的粗圆:

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid
        android:color="@android:color/transparent"/>
    <stroke
        android:width="24dp"
        android:color="@android:color/white"/>
    <size
        android:width="72dp"
        android:height="72dp"/>
</shape>

并希望动画减少笔画值,使透明中心像虹膜一样扩张。

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <objectAnimator
        android:propertyName="????"
        android:valueFrom="24dp"
        android:valueTo="4dp"
        android:duration="750"
        />
</set>

...但我不知道要指定什么作为属性名称。 &#34; strokeWidth&#34;似乎不起作用。我甚至不确定如何以编程方式将其拉出,因为GradientDrawable.setStroke()需要宽度和颜色,而objectAnimator只能操作单个参数属性。

1 个答案:

答案 0 :(得分:4)

列出了here列出的唯一可以使用对象动画制作动画的属性。这可以通过多种方式完成,其中最简单的方法如下:

以编程方式创建Shape并使用ValueAnimator设置值。您创建一个ValueAnimator浮点数并添加一个UpdateListener,以便在每个刻度线中,您可以编辑您的笔触大小。我会给你一个例子:

示例:

final ShapeDrawable circle = new ShapeDrawable(new OvalShape());

//Create your shape programatically
ValueAnimator animation = ValueAnimator.ofFloat(24.0f,12.0f);
animation.setDuration(1000);
animation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            circle.getPaint().setStrokeWidth((Float)animation.getAnimatedValue());
        }
});

该示例会将您的笔触宽度从24秒更改为12秒,您可以在ValueAnimatorShapeDrawable apis中查看更多信息,同时查看here,祝你好运。