Ion*_*gru 3 animation android checkmark android-vectordrawable
我正在尝试实现类似于BEMAnimationTypeStroke的东西,它可以在iOS库BEMCheckBox中找到.
我已经尝试使用动画矢量drawable来实现这一点,但我不知道如何设置圆圈内的复选标记从0起点到最终位置的动画.(圆圈可以是静态的,我希望为复选标记设置动画).这是我为此尝试的实现:
绘制/ vector_drawable_ic_check_circle_white_48px.xml
<path
android:name="check"
android:fillColor="#FFFFFF"
android:pathData="M37.1,13.6L18.4,32.3h1.7l-8.5-8.5L10,25.5l8.5,8.5l0.8,0.8l0.8-0.8l18.7-18.7L37.1,13.6L37.1,13.6z"/>
<path
android:name="circle"
android:fillColor="#FFFFFF"
android:pathData="M24,48c13.3,0,24-10.7,24-24S37.3,0,24,0S0,10.7,0,24S10.7,48,24,48L24,48z
M24,45.6
C12.1,45.6,2.4,35.9,2.4,24S12.1,2.4,24,2.4S45.6,12.1,45.6,24S35.9,45.6,24,45.6L24,45.6z"/>
</vector>
Run Code Online (Sandbox Code Playgroud)
动画/ transform.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:duration="1000"
android:propertyName="fillColor"
android:valueFrom="@color/transparent"
android:valueTo="@color/white"/>
</set>
Run Code Online (Sandbox Code Playgroud)
绘制/ animation.xml
<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/vector_drawable_ic_check_circle_white_48px">
<target
android:name="check"
android:animation="@anim/change_color"/>
</animated-vector>
Run Code Online (Sandbox Code Playgroud)
设置布局后,我使用以下命令启动动画:
ImageView mCpuImageView = (ImageView) findViewById(R.id.animated_check);
Drawable drawable = mCpuImageView.getDrawable();
if (drawable instanceof Animatable) {
((Animatable) drawable).start();
}
Run Code Online (Sandbox Code Playgroud)
谁能帮我这个?有没有更简单的方法来实现这一点(自定义视图或现有库)?
我的想法是,我希望在一个圆圈中有一个复选标记,我想要勾选复选标记的路径.显示时,只显示圆圈,然后创建复选标记动画.如果其他自定义,例如同时为复选标记设置动画的动画,则很容易实现它会更好,但起初我只想为复选标记设置动画.
LE: 最后我选择了通过自定义View手动创建动画的方法.如果有人知道如何通过矢量绘制实现这一点,它将是一个很好的开发实践.谢谢.
我一直在寻找同样的东西,这篇文章几乎解释了这一切.该帖子的代码:
绘制/ check_mark.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<group android:name="background">
<path
android:name="circle"
android:fillColor="@color/colorPrimary"
android:pathData="M12,12m-10,0a10,10 0,1 1,20 0a10,10 0,1 1,-20 0" />
</group>
<group android:name="check">
<path
android:name="tick"
android:pathData="M6,11 l0,0 l0,0"
android:strokeColor="@color/colorAccent"
android:strokeWidth="1" />
</group>
</vector>
Run Code Online (Sandbox Code Playgroud)
可绘制-V21/animated_check.xml
<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/check_mark">
<target
android:name="tick"
android:animation="@anim/check_animation" />
</animated-vector>
Run Code Online (Sandbox Code Playgroud)
动画/ check_animation.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:ordering="sequentially"
android:shareInterpolator="false">
<!-- Step 1 -->
<objectAnimator
android:duration="@android:integer/config_shortAnimTime"
android:propertyName="pathData"
android:valueFrom="M6,11 l0,0 l0,0"
android:valueTo="M6,11 l3.5,4 l0,0"
android:valueType="pathType" />
<!-- Step 2 -->
<objectAnimator
android:duration="@android:integer/config_shortAnimTime"
android:propertyName="pathData"
android:valueFrom="M6,11 l3.5,4 l0,0"
android:valueTo="M6,11 l3.5,4 l8,-7"
android:valueType="pathType" />
</set>
Run Code Online (Sandbox Code Playgroud)
用法
布局XML
<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:visibility="visible"
app:srcCompat="@drawable/animated_check" />
Run Code Online (Sandbox Code Playgroud)
Java类
mImgCheck = (ImageView) findViewById(R.id.imageView);
((Animatable) mImgCheck.getDrawable()).start();
Run Code Online (Sandbox Code Playgroud)
复选标记创建的动画可以通过对复选标记路径的trimPathEnd
属性进行动画处理来实现(请参阅https://developer.android.com/reference/android/graphics/drawable/VectorDrawable.html)。该属性的值trimPathEnd
表示路径结束位置的百分比,修剪超出该百分比的所有内容。通过trimPathEnd
从 0 到 1 进行动画处理,将显示复选标记路径的 0% 到 100%。
在下面的 AnimatedVectorDrawable 定义中,我们引用了一个名为 的动画trim_path
:
<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/vector_drawable_ic_check_circle_white_48px">
<target
android:name="check"
android:animation="@anim/trim_path"/>
</animated-vector>
Run Code Online (Sandbox Code Playgroud)
然后在anim/trim_path.xml中:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:duration="1000"
android:propertyName="trimPathEnd"
android:valueFrom="0"
android:valueTo="1"/>
</set>
Run Code Online (Sandbox Code Playgroud)
这应该会生成一个 AnimatedVectorDrawable,其复选标记从 0% 增长到 100%。同样,您trimPathEnd
也可以在圆圈上制作动画。请注意,您可能需要复制圆形路径,因此一个圆形为灰色且静态,另一个圆形为蓝色且在顶部进行动画处理。
归档时间: |
|
查看次数: |
13960 次 |
最近记录: |