直接上demo,用法都在程序的注释里了,首先上五渣效果图,
文章图片
布局代码:
主Activity
package com.example.animation;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity { private TextView textview ;
private Button scaleX ;
private Button scaleY ;
private Button alpha ;
private Button rotate ;
private Button translationX ;
private Button translationY ;
private ValueAnimator va ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.textview = (TextView) findViewById(R.id.textview) ;
this.alpha = (Button) findViewById(R.id.alpha) ;
this.rotate = (Button) findViewById(R.id.rotate) ;
this.translationX = (Button)findViewById(R.id.translationX) ;
this.translationY = (Button)findViewById(R.id.translationY) ;
this.scaleX = (Button)findViewById(R.id.scaleX) ;
this.scaleY= (Button)findViewById(R.id.scaleY) ;
this.scaleX.setOnClickListener(new OnClickListener() {@Override
public void onClick(View v) {
scaleX() ;
}});
this.scaleY.setOnClickListener(new OnClickListener() {@Override
public void onClick(View v) {
scaleY();
}
});
this.translationY.setOnClickListener(new OnClickListener() {@Override
public void onClick(View v) {
translation(2) ;
}
});
this.translationX.setOnClickListener(new OnClickListener() {@Override
public void onClick(View v) {
translation(1);
}
});
this.alpha.setOnClickListener(new OnClickListener() {@Override
public void onClick(View v) {
alpha() ;
}
});
this.rotate.setOnClickListener(new OnClickListener() {@Override
public void onClick(View v) {
rotate() ;
}
});
}
/*
* 旋转动画
*/
private void rotate() {
this.va = ObjectAnimator.ofFloat(textview, "rotation", 0f,360f);
//从0度旋转到360度
this.va.setDuration(5000) ;
//设置从0度到360度的旋转时间
//this.va.setRepeatCount(5);
重复5次
//this.va.setRepeatMode(ValueAnimator.REVERSE);
播放完毕直接翻转播放
//this.va.setRepeatMode(ValueAnimator.RESTART);
播放完毕直接从头播放
this.va.start();
} /*
*左右移动 soup and downis translationY
*flag==1zuoyou
*flag==2 shangxia
*/
private void translation(int flag) {
if(flag == 1)
this.va = ObjectAnimator.ofFloat(textview, "translationX", 0f,50f,5f);
//left and right
else this.va = ObjectAnimator.ofFloat(textview, "translationY", 0f,50f,5f);
//left and right
this.va.setDuration(1500) ;
//设置从0度到360度的旋转时间
this.va.setRepeatCount(5);
// 重复5次
//this.va.setRepeatMode(ValueAnimator.REVERSE);
播放完毕直接翻转播放
//this.va.setRepeatMode(ValueAnimator.RESTART);
播放完毕直接从头播放
this.va.start();
} /*
*上下移动移动
*/
private void translationY() {
this.va = ObjectAnimator.ofFloat(textview, "translationY", 0f,50f,5f);
//left and right
this.va.setDuration(1500) ;
//设置从0度到360度的旋转时间
this.va.setRepeatCount(5);
// 重复5次
//this.va.setRepeatMode(ValueAnimator.REVERSE);
播放完毕直接翻转播放
//this.va.setRepeatMode(ValueAnimator.RESTART);
播放完毕直接从头播放
this.va.start();
} /*
* 改变透明度(渐渐消失)
*/
private void alpha() {
this.va = ObjectAnimator.ofFloat(this.textview,"alpha",1f,0f,1f) ;
//透明度从1变成0然后变成1,以此类推。。
this.va.setDuration(5000) ;
//设置变化间隔
//this.va.setRepeatCount(5);
重复5次
//this.va.setRepeatMode(ValueAnimator.REVERSE);
播放完毕直接翻转播放
//this.va.setRepeatMode(ValueAnimator.RESTART);
播放完毕直接从头播放
this.va.start();
} /*
* 左右拉伸
*/
private void scaleX() {
this.va = ObjectAnimator.ofFloat(this.textview,"scaleX",1f,5f,1f) ;
this.va.setDuration(5000) ;
//this.va.setRepeatCount(5);
重复5次
//this.va.setRepeatMode(ValueAnimator.REVERSE);
播放完毕直接翻转播放
//this.va.setRepeatMode(ValueAnimator.RESTART);
播放完毕直接从头播放
this.va.start();
}
/*
* 上下拉伸
*/
private void scaleY() {
this.va = ObjectAnimator.ofFloat(this.textview,"scaleY",1f,5f,1f) ;
this.va.setDuration(5000) ;
//this.va.setRepeatCount(5);
重复5次
//this.va.setRepeatMode(ValueAnimator.REVERSE);
播放完毕直接翻转播放
//this.va.setRepeatMode(ValueAnimator.RESTART);
播放完毕直接从头播放
this.va.start();
}
}
【安卓属性动画ValueAnimator与ObjectAnimator详解】