形状的描边宽度动画化

4

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

<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>

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

1个回答

7

只有在对象动画中可以使用以下列出的属性进行动画处理,详见此处。这可以通过许多方式来实现,其中最简单的一种是:

以编程方式创建形状并使用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 API中探索更多信息。此外,在这里查看,祝你好运。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接