布局
<?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=".MainActivity">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textColor="#000"
android:textSize="48sp" />
<Button
android:id="@+id/btn_showAnimator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示动画" />
</LinearLayout>
**
MainActivity
**
public class MainActivity extends AppCompatActivity {
@BindView(R.id.tv_title)
TextView tvTitle;
@BindView(R.id.btn_showAnimator)
Button btnShowAnimator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
@SuppressLint("ObjectAnimatorBinding")
@OnClick(R.id.btn_showAnimator)
public void onViewClicked() {
//第一种 xml文件实现动画效果
//给控件设置属性动画
/*Animator animator = AnimatorInflater.loadAnimator(this, R.animator.textview_animator);
animator.setTarget(tvTitle);
animator.start();*/
//这种方法不太推荐
//tvTitle.animate().translationX(300f);
//第二种 代码实现动画效果
//推荐以下方式
ObjectAnimator animator = ObjectAnimator.ofFloat(tvTitle, "rotationX", 0, 200);
animator.setInterpolator(new AccelerateInterpolator());
animator.setDuration(2000);
animator.start();
}
}
**
xml文件、textview_animator
**
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:duration="2000"
android:propertyName="textColor"
android:valueFrom="@color/green"
android:valueTo="@color/yellow"></objectAnimator>
<objectAnimator
android:duration="2000"
android:propertyName="translationX"
android:valueFrom="0"
android:valueTo="300"></objectAnimator>
<objectAnimator
android:duration="2000"
android:propertyName="translationY"
android:valueFrom="0"
android:valueTo="300"></objectAnimator>
<objectAnimator
android:duration="2000"
android:propertyName="rotationY"
android:valueFrom="0"
android:valueTo="360"></objectAnimator>
<objectAnimator
android:duration="2000"
android:propertyName="scaleX"
android:valueFrom="0"
android:valueTo="2"></objectAnimator>
<objectAnimator
android:duration="2000"
android:propertyName="alpha"
android:valueFrom="0.1"
android:valueTo="0.8"></objectAnimator>
</set>