您的位置:首页 > 移动开发 > Android开发

android 属性动画

2015-06-28 16:33 465 查看
package com.example.objectanimator;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.TextView;

/***********************************
* android 属性动画是在android 3.0出现的,
*
* 要想向低版本兼容可以使用开源框架nieoldandroid动画
*
*
* **********************************/
public class MainActivity extends Activity {
TextView textview;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

textview = (TextView) this.findViewById(R.id.textview);
Button button = (Button) this.findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
//textviewAnimation(textview);
//valueAnimatorTest();
//animationSet(textview);
viewPropertyAnimatorTest(textview);
}
});

}

private void textviewAnimation(TextView textview){
float startx = textview.getTranslationX();
ObjectAnimator animator = ObjectAnimator.ofFloat(textview, "translationX", startx,-500f,startx);
animator.setRepeatCount(100);
//animator.setRepeatMode(RE);
animator.setInterpolator(new LinearInterpolator());//线性运行
animator.setDuration(1000);
animator.start();
}

private void valueAnimatorTest(){
ValueAnimator valueAnimator = ValueAnimator.ofFloat(0,94,9,58);//值的改变从0-94-9-58依次改变
valueAnimator.setDuration(1000);
//监听值的变化(哈哈)
valueAnimator.addUpdateListener(new AnimatorUpdateListener() {

@Override
public void onAnimationUpdate(ValueAnimator anim) {
float value = (float) anim.getAnimatedValue();
Log.e("current value is ", value+"");
}
});
valueAnimator.start();
}

//集合动画
private void animationSet(TextView textview){

float  transx = (float) textview.getTranslationX();
//动画集合对象
AnimatorSet animatorSet = new AnimatorSet();
//动画监听
animatorSet.addListener(new AnimatorListenerAdapter(){
@Override
public void onAnimationEnd(Animator animation) {
Log.i("----------AnimatorSet anim is finish-------------", "aa");
}
});

ObjectAnimator transAnim = ObjectAnimator.ofFloat(textview, "translationX", transx,-500f,transx);//平移
ObjectAnimator alphaAnim = ObjectAnimator.ofFloat(textview, "alpha", 1f,0f,1f);//像素
ObjectAnimator rotateAnim = ObjectAnimator.ofFloat(textview, "rotation", 0f,360f);//旋转
animatorSet.play(rotateAnim).with(transAnim).after(alphaAnim);//先变化像素,再同时旋转动画和平移动画
animatorSet.setDuration(2000);
animatorSet.setInterpolator(new AccelerateInterpolator());//加速
animatorSet.start();
}

//ViewPropertyAnimator 让动画变得更加简单(此属性是在android 3.1出现的)
private void viewPropertyAnimatorTest(TextView textview){
//textview.animate().alpha(0f).setDuration(2000);//将textview由不透明变为透明,动画时长为2秒
textview.animate().alpha(0f).x(500f).y(500f).setDuration(2000);//将textview由不透明变为透明,动画时长为2秒
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: