Android仿今日頭條刷新vector動(dòng)畫
一般的刷新動(dòng)畫是一個(gè)圈圈在轉(zhuǎn),而頭條的比較特殊,直接上寫好的效果圖:

動(dòng)畫思路:
整個(gè)view可以分為五個(gè)部分:
1、最外面的殼(不需要?jiǎng)赢嫞?/span>
2、中間矩形框
3、矩形框中的灰色矩形塊
4、三根短線
5、三根長線
第一步:在drawable文件夾下新建名為refresh的vector文件:
<vector xmlns:android="http://schemas.android.com/apk/res/android"android:width="200dp"android:height="200dp"android:viewportWidth="200"android:viewportHeight="200"><!--外面大框--><pathandroid:name="out_rect"android:pathData="M70,60L130,60Q140,60 140,70L140,130Q140,140 130,140L70,140Q60,140 60,130L60,70Q60,60 70,60"android:strokeWidth="2"android:strokeColor="#e5e5e5" /><!--小長方形框--><pathandroid:name="middle_rect"android:pathData="M71,75L100,75L100,95L72,95L72,75"android:strokeWidth="2"android:strokeColor="#e5e5e5" /><!--里面的正方形--><pathandroid:name="inner_rect"android:fillColor="#e5e5e5"android:pathData="M73,76L99,76L99,94L73,94" /><!--短線條--><pathandroid:name="short_lines"android:pathData="M108,75L128,75M108,85L128,85M108,95L128,95"android:strokeWidth="2"android:strokeColor="#e5e5e5" /><!--長線條--><pathandroid:name="long_lines"android:pathData="M72,105L128,105M72,115L128,115M72,125L128,125"android:strokeWidth="2"android:strokeColor="#e5e5e5" /></vector>
打開xml右邊的預(yù)覽,我們可以看到效果:

第二步:在drawable文件夾下新建名為refresh_vector的animated-vector
<?xml version="1.0" encoding="utf-8"?><animated-vector xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:drawable="@drawable/refresh"tools:ignore="NewApi"><targetandroid:name="middle_rect"android:animation="@animator/anim_middle_rect" /><targetandroid:name="short_lines"android:animation="@animator/anim_short_lines" /><targetandroid:name="long_lines"android:animation="@animator/anim_long_lines" /><targetandroid:name="inner_rect"android:animation="@animator/anim_inner_rect" /></animated-vector>
這里需要注意幾點(diǎn):
1、這里一定要引用上面的refresh文件
android:drawable="@drawable/refresh"
2、target標(biāo)簽下的name一定要和refresh中的path標(biāo)簽下的名字一致
第三步:新建animator文件夾(不是anim),并在下面新建短線、長線、中間矩形框和里面矩形對應(yīng)的objectAnimator集合:
anim_middle_rect.xml:
<set xmlns:android="http://schemas.android.com/apk/res/android"android:ordering="sequentially"><!--中間長方形--><objectAnimatorandroid:duration="500"android:interpolator="@android:interpolator/decelerate_cubic"android:propertyName="pathData"android:valueFrom="M71,75L100,75L100,95L72,95L72,75"android:valueTo="M99,75L128,75L128,95L100,95L100,75"android:valueType="pathType" /><objectAnimatorandroid:duration="500"android:interpolator="@android:interpolator/decelerate_cubic"android:propertyName="pathData"android:valueFrom="M99,75L128,75L128,95L100,95L100,75"android:valueTo="M99,105L128,105L128,125L100,125L100,105"android:valueType="pathType" /><objectAnimatorandroid:duration="500"android:interpolator="@android:interpolator/decelerate_cubic"android:propertyName="pathData"android:valueFrom="M99,105L128,105L128,125L100,125L100,105"android:valueTo="M71,105L100,105L100,125L72,125L72,105"android:valueType="pathType" /><objectAnimatorandroid:duration="500"android:interpolator="@android:interpolator/decelerate_cubic"android:propertyName="pathData"android:valueFrom="M71,105L100,105L100,125L72,125L72,105"android:valueTo="M71,75L100,75L100,95L72,95L72,75"android:valueType="pathType" /></set>
anim_short_lines.xml:
<set xmlns:android="http://schemas.android.com/apk/res/android"android:ordering="sequentially"><!--起點(diǎn)為短線--><objectAnimatorandroid:duration="500"android:interpolator="@android:interpolator/decelerate_cubic"android:propertyName="pathData"android:valueFrom="M108,75L128,75M108,85L128,85M108,95L128,95"android:valueTo="M72,105L128,105M72,115L128,115M72,125L128,125"android:valueType="pathType" /><objectAnimatorandroid:duration="500"android:interpolator="@android:interpolator/decelerate_cubic"android:propertyName="pathData"android:valueFrom="M72,105L128,105M72,115L128,115M72,125L128,125"android:valueTo="M72,105L92,105M72,115L92,115M72,125L92,125"android:valueType="pathType" /><objectAnimatorandroid:duration="500"android:interpolator="@android:interpolator/decelerate_cubic"android:propertyName="pathData"android:valueFrom="M72,105L92,105M72,115L92,115M72,125L92,125"android:valueTo="M72,75L128,75M72,85L128,85M72,95L128,95"android:valueType="pathType" /><objectAnimatorandroid:duration="500"android:interpolator="@android:interpolator/decelerate_cubic"android:propertyName="pathData"android:valueFrom="M72,75L128,75M72,85L128,85M72,95L128,95"android:valueTo="M108,75L128,75M108,85L128,85M108,95L128,95"android:valueType="pathType" /></set>
anim_long_lines.xml:
<set xmlns:android="http://schemas.android.com/apk/res/android"android:ordering="sequentially"><!--開始為長線--><objectAnimatorandroid:duration="500"android:interpolator="@android:interpolator/decelerate_cubic"android:propertyName="pathData"android:valueFrom="M72,105L128,105M72,115L128,115M72,125L128,125"android:valueTo="M72,75L92,75M72,85L92,85M72,95L92,95"android:valueType="pathType" /><objectAnimatorandroid:duration="500"android:interpolator="@android:interpolator/decelerate_cubic"android:propertyName="pathData"android:valueFrom="M72,75L92,75M72,85L92,85M72,95L92,95"android:valueTo="M72,75L128,75M72,85L128,85M72,95L128,95"android:valueType="pathType" /><objectAnimatorandroid:duration="500"android:interpolator="@android:interpolator/decelerate_cubic"android:propertyName="pathData"android:valueFrom="M72,75L128,75M72,85L128,85M72,95L128,95"android:valueTo="M108,105L128,105M108,115L128,115M108,125L128,125"android:valueType="pathType" /><objectAnimatorandroid:duration="500"android:interpolator="@android:interpolator/decelerate_cubic"android:propertyName="pathData"android:valueFrom="M108,105L128,105M108,115L128,115M108,125L128,125"android:valueTo="M72,105L128,105M72,115L128,115M72,125L128,125"android:valueType="pathType" /></set>
anim_inner_rect.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"android:ordering="sequentially"><!--里面的長方形--><objectAnimatorandroid:duration="500"android:interpolator="@android:interpolator/decelerate_cubic"android:propertyName="pathData"android:valueFrom="M73,76L99,76L99,94L73,94"android:valueTo="M101,76L127,76L127,94L101,94"android:valueType="pathType" /><objectAnimatorandroid:duration="500"android:interpolator="@android:interpolator/decelerate_cubic"android:propertyName="pathData"android:valueFrom="M101,76L127,76L127,94L101,94"android:valueTo="M101,106L127,106L127,124L101,124"android:valueType="pathType" /><objectAnimatorandroid:duration="500"android:interpolator="@android:interpolator/decelerate_cubic"android:propertyName="pathData"android:valueFrom="M101,106L127,106L127,124L101,124"android:valueTo="M73,106L99,106L99,124L73,124"android:valueType="pathType" /><objectAnimatorandroid:duration="500"android:interpolator="@android:interpolator/decelerate_cubic"android:propertyName="pathData"android:valueFrom="M73,106L99,106L99,124L73,124"android:valueTo="M73,76L99,76L99,94L73,94"android:valueType="pathType" /></set>
布局文件:
<ImageViewandroid:id="@+id/imageView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"/>
最后一步,在activity中:
final AnimatedVectorDrawable animatedVectorDrawable = (AnimatedVectorDrawable) ContextCompat.getDrawable(this, R.drawable.refresh_vector);ImageView imageView = findViewById(R.id.imageView);imageView.setImageDrawable(animatedVectorDrawable);animatedVectorDrawable.start();//循環(huán)動(dòng)畫final Handler mainHandler = new Handler(Looper.getMainLooper());animatedVectorDrawable.registerAnimationCallback(new Animatable2.AnimationCallback() {public void onAnimationEnd(Drawable drawable) {mainHandler.post(new Runnable() {public void run() {animatedVectorDrawable.start();}});}});
需要源碼的童鞋公眾號回復(fù):"頭條刷新"即可獲取哦!
到這里就結(jié)束啦!
評論
圖片
表情
