mainActivity:
package com.example.propetyanim;
import android.animation.Animator;
import android.animation.ArgbEvaluator;
import android.animation.ValueAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button bt_start;
private TextView tv;
private Button bt_stop;
private Button bt_cancle;
private ValueAnimator valueAnimator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
public void initView(){
bt_start=(Button)findViewById(R.id.bt_start);
bt_start.setOnClickListener(this);
tv=(TextView)findViewById(R.id.tv);
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "mTv 点击了", Toast.LENGTH_SHORT).show();
}
});
bt_stop=(Button)findViewById(R.id.bt_stop);
bt_stop.setOnClickListener(this);
bt_cancle=(Button)findViewById(R.id.bt_cancle);
bt_cancle.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bt_start:
propAnim();
trans();
propColor();
break;
case R.id.bt_stop:
if(valueAnimator!=null && valueAnimator.isRunning()){
valueAnimator.cancel();
valueAnimator.end();
}
break;
case R.id.bt_cancle:
valueAnimator.removeListener(animatorListener);
break;
}
}
private void trans(){
TranslateAnimation animation = new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 300, Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 400);
animation.setDuration(3000);
animation.setFillAfter(true);
tv.startAnimation(animation);
}
private void propColor(){
ValueAnimator valueAnimator = ValueAnimator.ofInt(0xffff0000, 0xff0000ff);
valueAnimator.setEvaluator(new ArgbEvaluator());
valueAnimator.setDuration(3000);
valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int color = (int)animation.getAnimatedValue();
tv.setBackgroundColor(color);
}
});
valueAnimator.start();
}
private void propAnim(){
valueAnimator = ValueAnimator.ofInt(0, 300);
valueAnimator.setDuration(3000);
valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int value = (int)animation.getAnimatedValue();
tv.layout(value,value,value+tv.getWidth(),value+tv.getWidth());
}
});
valueAnimator.addListener(animatorListener);
valueAnimator.start();
}
private Animator.AnimatorListener animatorListener= new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
Log.e("MainActivity", "onAnimationEnd");
}
@Override
public void onAnimationEnd(Animator animation) {
Log.e("MainActivity", "onAnimationEnd");
}
@Override
public void onAnimationCancel(Animator animation) {
Log.e("MainActivity", "onAnimationCancel");
}
@Override
public void onAnimationRepeat(Animator animation) {
Log.e("MainActivity", "onAnimationRepeat");
}
};
}
main.xml:
<?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="com.example.propetyanim.MainActivity">
<Button
android:id="@+id/bt_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开启动画" />
<Button
android:id="@+id/bt_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止" />
<Button
android:id="@+id/bt_cancle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消监听" />
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World" />
</LinearLayout>