属性动画跟补间动画实现一样简单
<?xml version="1.0" encoding="utf-8"?>
<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="yangxuexue.bwie.com.propertyanimation.MainActivity">
<Button
android:onClick="apale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="渐变"/>
<Button
android:onClick="translation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="移动"/>
<Button
android:onClick="rotation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="旋转"/>
<Button
android:onClick="scale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="缩放"/>
<Button
android:onClick="set"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="组合"/>
<ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@mipmap/ic_launcher"/>
</LinearLayout>
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageview);
}
public void apale(View v) {
//透明度起始为1,结束时为0
ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0f,1f);
animator.setDuration(1000);//时间1s
animator.start();
}
//旋转
public void rotation(View v) {
ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "rotation", 0f, 360f, 0f);
animator.setDuration(2000);
animator.start();
}
//缩放
public void scale(View v) {
ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "scaleX", 1f, 2f, 1f);
animator.setDuration(2000);
animator.start();
}
//移动
public void translation(View v) {
ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "translationX", 0f, -300f, 0f);
animator.setDuration(2000);
animator.start();
}
//组合
public void set(View v) {
//沿x轴放大
ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(imageView, "scaleX", 1f, 2f, 1f);
//沿y轴放大
ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(imageView, "scaleY", 1f, 2f, 1f);
//移动
ObjectAnimator translationXAnimator = ObjectAnimator.ofFloat(imageView, "translationX", 0f, 200f, 0f);
//透明动画
ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0f, 1f);
AnimatorSet set = new AnimatorSet();
//同时沿X,Y轴放大,且改变透明度,然后移动
set.play(scaleXAnimator).with(scaleYAnimator).with(animator).before(translationXAnimator);
//都设置3s,也可以为每个单独设置
set.setDuration(3000);
set.start();
}
}