AnimatedVectorDrawable(粘合剂)
用于粘合SVG和动画
MainAc
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button) findViewById(R.id.button); iv = (ImageView) findViewById(R.id.iv); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ((Animatable)iv.getDrawable()).start(); } }); } }
anim_search
tips:trimPathStarts属性可以像画出一个圆一样来绘制一个圆
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" android:duration="400" android:propertyName="trimPathStart" android:valueFrom="1" android:valueTo="0" android:valueType="floatType" android:interpolator="@android:anim/accelerate_decelerate_interpolator"> </objectAnimator>
search.xml(drawable)
创建基于xml的SVG图形
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="160dp" android:height="30dp" android:viewportHeight="30" android:viewportWidth="160"> <path android:name="search" android:pathData="M141,17 A 9,9 0 1,1 142,16 L149,23" android:strokeColor="#ff3570be" android:strokeWidth="2" android:strokeLineCap="round" android:strokeAlpha="0.8"/> <path android:name="line" android:pathData="M0,23 L149,23" android:strokeColor="#ff3570be" android:strokeWidth="2" android:strokeLineCap="square" android:strokeAlpha="0.8" /> </vector>
AnimatedVectorDrawable
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:drawable="@drawable/search" tools:targetApi="lollipop"> <target android:animation="@animator/anim_search" android:name="search"/> </animated-vector>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.yrc.part7.MainActivity"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="@string/app_name" /> <ImageView android:id="@+id/iv" android:layout_width="200dp" android:layout_height="200dp" android:layout_gravity="center" app:srcCompat="@drawable/search_sys"/> </LinearLayout>