package com.example.animation_demo;
import android.os.Bundle;
import android.R.anim;
import android.animation.ObjectAnimator;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.PointF;
import android.util.Log;
import android.view.Menu;
import android.view.animation.LinearInterpolator;
import android.widget.TextView;
public class MainActivity extends Activity {
protected static final String TAG = "MainActivity";
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tView=(TextView) findViewById(R.id.tv_ani);
// objectAnimator_test(tView);
// valueAnimator_test(tView);
// 制作抛物线
// paowuxian(tView);
// ValueAnimator中自定义bean
ValueAnimator animator=new ValueAnimator();
animator.setTarget(tView);
animator.setDuration(1000);//这里设置的动画事件和下面的fraction参数相关
animator.setObjectValues(new AnimatorBean());
animator.setEvaluator(new TypeEvaluator<AnimatorBean>() {
@Override
public AnimatorBean evaluate(float fraction,
AnimatorBean startValue, AnimatorBean endValue) {
AnimatorBean bean=new AnimatorBean();
Log.e(TAG, fraction*3+"");
bean.alphaX=0.1f;
bean.alphaY=1f;
bean.scaleX=10f*fraction;
bean.scaleY=10f;
bean.tranlateX=100f;
bean.tranlateYf=100f;
return bean;
}
});
animator.start();
animator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
AnimatorBean bean=(AnimatorBean) animation.getAnimatedValue();
tView.setScaleX(bean.scaleX);
tView.setScaleY(bean.scaleY);
tView.setTranslationX(bean.tranlateX);
}
});
}
public class AnimatorBean{
//设置渐变的属性
public float alphaX;
public float alphaY;
//设置缩放的属性
public float scaleX;
public float scaleY;
//设置位移的属性
public float tranlateX;
public float tranlateYf;
}
@SuppressLint("NewApi")
private void paowuxian(final TextView tView) {
ValueAnimator animator=new ValueAnimator();
animator.setTarget(tView);
animator.setDuration(3000);
animator.setObjectValues(new PointF(0,0));//PointF是二维矢量
animator.setInterpolator(new LinearInterpolator());
animator.setEvaluator(new TypeEvaluator<PointF>() {
@Override
public PointF evaluate(float fraction, PointF startValue,
PointF endValue) {
PointF point=new PointF();
point.x=fraction*200*3;
point.y=(fraction*3)*(fraction*3)*0.5f*200;
return point;
}
});
animator.start();
animator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
PointF pointF=(PointF) animation.getAnimatedValue();
tView.setX(pointF.x);
tView.setY(pointF.y);
}
});
}
@SuppressLint("NewApi")
private void valueAnimator_test(final TextView tView) {
ValueAnimator valueAnimator=ValueAnimator.ofFloat(0,getMyScreenHeight()-tView.getHeight());
valueAnimator.setTarget(tView);
valueAnimator.setDuration(3000).start();
valueAnimator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float animatedValue = (Float) animation.getAnimatedValue();
tView.setTranslationX(animatedValue);
tView.setScaleX(animatedValue);
tView.setScaleY(animatedValue);
}
});
}
public int getMyScreenHeight(){
int height = 0;
height= getWindowManager().getDefaultDisplay().getHeight();
return height;
}
@SuppressLint("NewApi")
private void objectAnimator_test(final TextView tView) {
ObjectAnimator animator=ObjectAnimator.ofFloat(tView, "hwz", 0.1f,1f);
animator.start();
animator.setDuration(4000);
animator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float animatedValue = (Float) animation.getAnimatedValue();
tView.setAlpha(animatedValue);
tView.setScaleX(animatedValue);
tView.setScaleY(animatedValue);
tView.setRotationX(180f);
}
});
}
}
import android.os.Bundle;
import android.R.anim;
import android.animation.ObjectAnimator;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.PointF;
import android.util.Log;
import android.view.Menu;
import android.view.animation.LinearInterpolator;
import android.widget.TextView;
public class MainActivity extends Activity {
protected static final String TAG = "MainActivity";
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tView=(TextView) findViewById(R.id.tv_ani);
// objectAnimator_test(tView);
// valueAnimator_test(tView);
// 制作抛物线
// paowuxian(tView);
// ValueAnimator中自定义bean
ValueAnimator animator=new ValueAnimator();
animator.setTarget(tView);
animator.setDuration(1000);//这里设置的动画事件和下面的fraction参数相关
animator.setObjectValues(new AnimatorBean());
animator.setEvaluator(new TypeEvaluator<AnimatorBean>() {
@Override
public AnimatorBean evaluate(float fraction,
AnimatorBean startValue, AnimatorBean endValue) {
AnimatorBean bean=new AnimatorBean();
Log.e(TAG, fraction*3+"");
bean.alphaX=0.1f;
bean.alphaY=1f;
bean.scaleX=10f*fraction;
bean.scaleY=10f;
bean.tranlateX=100f;
bean.tranlateYf=100f;
return bean;
}
});
animator.start();
animator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
AnimatorBean bean=(AnimatorBean) animation.getAnimatedValue();
tView.setScaleX(bean.scaleX);
tView.setScaleY(bean.scaleY);
tView.setTranslationX(bean.tranlateX);
}
});
}
public class AnimatorBean{
//设置渐变的属性
public float alphaX;
public float alphaY;
//设置缩放的属性
public float scaleX;
public float scaleY;
//设置位移的属性
public float tranlateX;
public float tranlateYf;
}
@SuppressLint("NewApi")
private void paowuxian(final TextView tView) {
ValueAnimator animator=new ValueAnimator();
animator.setTarget(tView);
animator.setDuration(3000);
animator.setObjectValues(new PointF(0,0));//PointF是二维矢量
animator.setInterpolator(new LinearInterpolator());
animator.setEvaluator(new TypeEvaluator<PointF>() {
@Override
public PointF evaluate(float fraction, PointF startValue,
PointF endValue) {
PointF point=new PointF();
point.x=fraction*200*3;
point.y=(fraction*3)*(fraction*3)*0.5f*200;
return point;
}
});
animator.start();
animator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
PointF pointF=(PointF) animation.getAnimatedValue();
tView.setX(pointF.x);
tView.setY(pointF.y);
}
});
}
@SuppressLint("NewApi")
private void valueAnimator_test(final TextView tView) {
ValueAnimator valueAnimator=ValueAnimator.ofFloat(0,getMyScreenHeight()-tView.getHeight());
valueAnimator.setTarget(tView);
valueAnimator.setDuration(3000).start();
valueAnimator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float animatedValue = (Float) animation.getAnimatedValue();
tView.setTranslationX(animatedValue);
tView.setScaleX(animatedValue);
tView.setScaleY(animatedValue);
}
});
}
public int getMyScreenHeight(){
int height = 0;
height= getWindowManager().getDefaultDisplay().getHeight();
return height;
}
@SuppressLint("NewApi")
private void objectAnimator_test(final TextView tView) {
ObjectAnimator animator=ObjectAnimator.ofFloat(tView, "hwz", 0.1f,1f);
animator.start();
animator.setDuration(4000);
animator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float animatedValue = (Float) animation.getAnimatedValue();
tView.setAlpha(animatedValue);
tView.setScaleX(animatedValue);
tView.setScaleY(animatedValue);
tView.setRotationX(180f);
}
});
}
}