//ObjectAnimation动画
package com.example.propertyanimdemo01; import android.animation.AnimatorSet; import android.animation.ObjectAnimator; import android.animation.PropertyValuesHolder; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.animation.AccelerateDecelerateInterpolator; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button bt; private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { bt = (Button) findViewById(R.id.bt); bt.setOnClickListener(this); tv = (TextView) findViewById(R.id.tv); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.bt: //baseAnim(); //zhuheAnim(); oneByOne(); break; } } /** * 按顺序执行 */ private void oneByOne() { ObjectAnimator rotationY = ObjectAnimator.ofFloat(tv, "RotationY", 0f, 360f, 0f); ObjectAnimator scaleX = ObjectAnimator.ofFloat(tv, "ScaleX", 0.5f, 2f, 0.5f); ObjectAnimator translationX = ObjectAnimator.ofFloat(tv, "TranslationX", 0f, 100f, -100f); AnimatorSet animatorSet=new AnimatorSet(); animatorSet.setDuration(5000); //顺序执行 //animatorSet.playSequentially(rotationY,scaleX,translationX); //同步执行 //animatorSet.playTogether(rotationY,scaleX,translationX); //按要求 animatorSet.play(rotationY).after(scaleX).before(rotationY); animatorSet.start(); } /** * 组合动画 */ private void zhuheAnim() { PropertyValuesHolder rotationY = PropertyValuesHolder.ofFloat("RotationY", 0f, 360f, 0f); PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("ScaleX", 0.5f, 2f, 0.5f); ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(tv, rotationY, scaleX); objectAnimator.setDuration(3000); objectAnimator.start(); } /** * 基本动画 */ private void baseAnim() { //TranslationX //ObjectAnimator translationX = ObjectAnimator.ofFloat(tv, "TranslationX", 1, 200); //ObjectAnimator translationY = ObjectAnimator.ofFloat(tv, "TranslationY", 0, 200); //ObjectAnimator rotationX = ObjectAnimator.ofFloat(tv, "RotationX", 0, 200); //ObjectAnimator rotationY = ObjectAnimator.ofFloat(tv, "RotationY", 0, 200); // ObjectAnimator translationX = ObjectAnimator.ofFloat(tv, "ScaleX", 2); //ObjectAnimator translationX = ObjectAnimator.ofFloat(tv, "ScaleY", 2); ObjectAnimator translationX = ObjectAnimator.ofFloat(tv, "Alpha",0.1f,1f,0.5f); translationX.setInterpolator(new AccelerateDecelerateInterpolator()); //translationX.setRepeatCount(3); translationX.setDuration(3000); translationX.start(); } }
package com.example.propertyanimdemo01;
import android.animation.Animator;
import android.animation.ArgbEvaluator;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class Main3Activity extends AppCompatActivity implements View.OnClickListener {
/**
* 开启动画
*/
private Button mBtStart;
/**
* 停止
*/
private Button mBtStop;
/**
* 取消监听
*/
private Button mBtCancleListener;
/**
* Hello World!
*/
private TextView mTv;
private ValueAnimator valueAnimator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
initView();
}
private void initView() {
mBtStart = (Button) findViewById(R.id.bt_start);
mBtStart.setOnClickListener(this);
mBtStop = (Button) findViewById(R.id.bt_stop);
mBtStop.setOnClickListener(this);
mBtCancleListener = (Button) findViewById(R.id.bt_cancle_listener);
mBtCancleListener.setOnClickListener(this);
mTv = (TextView) findViewById(R.id.tv);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_start:
//propAnim();
propColor();
break;
case R.id.bt_stop:
if (valueAnimator!=null && valueAnimator.isRunning()){
valueAnimator.cancel();
valueAnimator.end();
}
break;
case R.id.bt_cancle_listener:
valueAnimator.removeListener(animatorListener);
break;
}
}
private void propColor(){
ValueAnimator valueAnimation=ValueAnimator.ofInt(0xffff0000, 0xff0000ff);
valueAnimation.setEvaluator(new ArgbEvaluator());
valueAnimation.setDuration(3000);
valueAnimation.setRepeatCount(ValueAnimator.INFINITE);
valueAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int color= (int) animation.getAnimatedValue();
mTv.setBackgroundColor(color);
}
});
valueAnimation.start();
}
private void propAnim(){
//先创建一个ValueAnimator
valueAnimator = ValueAnimator.ofInt(0, 300);
//设置动画执行的时间
valueAnimator.setDuration(3000);
//设置重复次数
//valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
//设置动画重复模式
//valueAnimator.setRepeatMode(ValueAnimator.RESTART);
//设置监听
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int value= (int) animation.getAnimatedValue();
mTv.layout(value,value,value+mTv.getWidth(),value+mTv.getHeight());
}
});
valueAnimator.addListener(animatorListener);
//开启动画
valueAnimator.start();
}
private Animator.AnimatorListener animatorListener=new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
Toast.makeText(Main3Activity.this,"onAnimationStart",Toast.LENGTH_LONG).show();
}
@Override
public void onAnimationEnd(Animator animation) {
Toast.makeText(Main3Activity.this,"onAnimationEnd",Toast.LENGTH_LONG).show();
Intent intent = new Intent(Main3Activity.this, Main2Activity.class);
startActivity(intent);
}
@Override
public void onAnimationCancel(Animator animation) {
Toast.makeText(Main3Activity.this,"onAnimationCancel",Toast.LENGTH_LONG).show();
}
@Override
public void onAnimationRepeat(Animator animation) {
Toast.makeText(Main3Activity.this,"onAnimationRepeat",Toast.LENGTH_LONG).show();
}
};
}