XML:
<?xml version="1.0" encoding="utf-8"?>
<Button
android:id="@+id/button_translate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="补间动画" />
<View
android:id="@+id/view_translate"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="20dp"
android:background="#ff0000" />
<Button
android:id="@+id/button_property"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="属性动画" />
<View
android:id="@+id/view_property"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="20dp"
android:background="#00ff00" />
<Button
android:id="@+id/button_property_other"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="属性动画其他" />
<View
android:id="@+id/view_property_other"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="20dp"
android:background="#0000ff" />
</LinearLayout>
Activity:
package com.example.work10_13;
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
/**
* 补间动画
*/
private Button mButtonTranslate;
private View mViewTranslate;
/**
* 属性动画
*/
private Button mButtonProperty;
private View mViewProperty;
/**
* 属性动画其他
*/
private Button mButtonPropertyOther;
private View mViewPropertyOther;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mButtonTranslate = (Button) findViewById(R.id.button_translate);
mButtonTranslate.setOnClickListener(this);
mViewTranslate = (View) findViewById(R.id.view_translate);
mButtonProperty = (Button) findViewById(R.id.button_property);
mButtonProperty.setOnClickListener(this);
mViewProperty = (View) findViewById(R.id.view_property);
mButtonPropertyOther = (Button) findViewById(R.id.button_property_other);
mButtonPropertyOther.setOnClickListener(this);
mViewPropertyOther = (View) findViewById(R.id.view_property_other);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
default:
break;
//位移
case R.id.button_translate:
TranslateAnimation animation = new TranslateAnimation(0, 500, 0, 0);
animation.setDuration(3000);
//动画结束后停留在结束位置
animation.setFillAfter(true);
mViewTranslate.startAnimation(animation);
break;
//属性
case R.id.button_property:
//想要移动的view,指定这个动画要操作这个控件的哪个属性、这个属性值是从哪变到哪
ObjectAnimator translationY = ObjectAnimator.ofFloat(mViewProperty, "rotationX", 0, 360, 0);
translationY.setDuration(3000);
//设置重复次数
translationY.setRepeatCount(0);
translationY.start();
break;
//综合
case R.id.button_property_other:
/** propertyName的值可以使:
*
* //1、透明度:alpha
* //2、旋转度数:rotation、rotationX、rotationY
* //3、平移:translationX、translationY
* //缩放:scaleX、scaleY
*/
ObjectAnimator rotationY = ObjectAnimator.ofFloat(mViewPropertyOther, "rotationY", 0, 180, 250);
ObjectAnimator scaleX = ObjectAnimator.ofFloat(mViewPropertyOther, "scaleX", 1f, 2f);
ObjectAnimator alpha = ObjectAnimator.ofFloat(mViewPropertyOther, "alpha", 1f, 0);
//AnimatorSet
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.setDuration(3000);
animatorSet.playTogether(rotationY,scaleX,alpha);
//动画结束后事件:
animatorSet.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
Toast.makeText(MainActivity.this,"布鲁布鲁布鲁",Toast.LENGTH_LONG).show();
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
animatorSet.start();
break;
}
}
}