android属性动画—自己的测试代码

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);
}
});
}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值