【问题标题】:Animate the stroke width of a Shape为 Shape 的笔画宽度设置动画
【发布时间】:2015-03-25 20:10:30
【问题描述】:

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

<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 只能操作单个参数属性。

【问题讨论】:

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


    【解决方案1】:

    您可以使用对象动画器制作动画的唯一属性在here 中列出。这可以通过许多方式完成,其中最简单的一种是:

    以编程方式创建您的形状并使用ValueAnimator 设置值。您创建一个 ValueAnimator 的 float 并添加一个 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,祝您好运。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-11
      • 2016-11-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多