(四)JAVA代码实现补间动画
每个都做了注释,使用和参数的填写
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
//点击TAG
private final int ANIM_TRANSLATE = 0,ANIM_ROATE = 1,ANIM_SCALE = 2,ANIM_ALPH = 3,ANIM_ALL = 4;
private Button button_translate,button_roate,button_scale,button_alpha,button_all;
private ImageView myImage;
private Animation animation;
/**
* 绑定
*/
//private Animation animation;
private void initView(){
button_translate = (Button) findViewById(R.id.button_translate);
button_roate = (Button) findViewById(R.id.button_roate);
button_scale = (Button) findViewById(R.id.button_scale);
button_alpha = (Button) findViewById(R.id.button_alpha);
button_all = (Button) findViewById(R.id.button_all);
button_translate.setTag(ANIM_TRANSLATE);
button_roate.setTag(ANIM_ROATE);
button_scale.setTag(ANIM_SCALE);
button_alpha.setTag(ANIM_ALPH);
button_all.setTag(ANIM_ALL);
myImage = (ImageView) findViewById(R.id.myImage);
}
/**
* 点击事件
*/
private void initCtrl(){
button_translate.setOnClickListener(this);
button_alpha.setOnClickListener(this);
button_roate.setOnClickListener(this);
button_scale.setOnClickListener(this);
button_all.setOnClickListener(this);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initCtrl();
}
/**
* 动画插入器
AccelerateInterpolator 加速,开始时慢中间加速
DecelerateInterpolator 减速,开始时快然后减速
AccelerateDecelerateInterolator 先加速后减速,开始结束时慢,中间加速
AnticipateInterpolator 反向,先向相反方向改变一段再加速播放
AnticipateOvershootInterpolator 反向加超越,先向相反方向改变,再加速播放,会超出目的值然后缓慢移动至目的值
BounceInterpolator 跳跃,快到目的值时值会跳跃,如目的值100,后面的值可能依次为85,77,70,80,90,100
CycleIinterpolator 循环,动画循环一定次数,值的改变为一正弦函数:Math.sin(2* mCycles*Math.PI* input)
LinearInterpolator 线性,线性均匀改变
OvershottInterpolator 超越,最后超出目的值然后缓慢改变到目的值
*/
private void TrandlateAnim(){
// 起始坐标x,y和终点坐标x,y
animation = new TranslateAnimation(0,200,0,200);
//3秒完成动画
animation.setDuration(2000);
//如果fillAfter的值为真的话,动画结束后,控件停留在执行后的状态
animation.setFillAfter(false);
animation.setInterpolator(new AccelerateInterpolator());
//启动动画
myImage.startAnimation(animation);
//动画的监听
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
Log.e("ceshi", "移动动画开始");
}
@Override
public void onAnimationEnd(Animation animation) {
Log.e("ceshi", "移动动画结束");
}
@Override
public void onAnimationRepeat(Animation animation) {
Log.e("ceshi", "移动动画重复");
}
});
}
/**
* 旋转
*/
private void RoateAnim(){
//旋转起始度数,旋转的点
animation = new RotateAnimation(0, 30, myImage.getWidth(), myImage.getHeight());
animation.setInterpolator(new DecelerateInterpolator());
animation.setDuration(2000);
animation.setFillAfter(false);
myImage.startAnimation(animation);
}
private void AlphAnim(){
//构造方法的两个参数是指,开始时候的透明度和结束时候的透明度
animation = new AlphaAnimation(1.0f, 0.7f);
animation.setInterpolator(new AccelerateDecelerateInterpolator());
animation.setDuration(2000);
animation.setFillAfter(false);
myImage.startAnimation(animation);
}
private void ScaleAnim(){
/*float fromX 动画起始时 X坐标上的伸缩尺寸
float toX 动画结束时 X坐标上的伸缩尺寸
float fromY 动画起始时Y坐标上的伸缩尺寸
float toY 动画结束时Y坐标上的伸缩尺寸
int pivotXType 动画在X轴相对于物件位置类型
float pivotXValue 动画相对于物件的X坐标的开始位置
int pivotYType 动画在Y轴相对于物件位置类型
float pivotYValue 动画相对于物件的Y坐标的开始位置 */
animation = new ScaleAnimation(1.0f, 0f, 1.0f, 0f,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setInterpolator(new AnticipateInterpolator());
animation.setDuration(2000);
animation.setFillAfter(false);
myImage.startAnimation(animation);
}
/**
* 动画集合
*/
private void AllAnim(){
// 是否多个animation使用同一个动画插入器
AnimationSet animationSet = new AnimationSet(false);
ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 2.0f, 1.0f, 2.0f);
scaleAnimation.setDuration(2000);
animationSet.addAnimation(scaleAnimation);
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
alphaAnimation.setDuration(2000);
animationSet.addAnimation(alphaAnimation);
TranslateAnimation translateAnimation = new TranslateAnimation(0, 200, 0, 200);
alphaAnimation.setDuration(2000);
animationSet.addAnimation(translateAnimation);
RotateAnimation rotateAnimation = new RotateAnimation(0, 30, myImage.getWidth(), myImage.getHeight());
animationSet.addAnimation(rotateAnimation);
myImage.startAnimation(animationSet);
}
@Override
public void onClick(View view) {
switch ((Integer)view.getTag()){
case ANIM_TRANSLATE:
TrandlateAnim();
break;
case ANIM_ALPH:
AlphAnim();
break;
case ANIM_ROATE:
RoateAnim();
break;
case ANIM_SCALE:
ScaleAnim();
break;
case ANIM_ALL:
AllAnim();
break;
}
}
}
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
//点击事件
private final int ANIM_TRANSLATE = 0,ANIM_ROATE = 1,ANIM_SCALE = 2,ANIM_ALPH = 3,ANIM_ALL = 4;
private Button button_translate,button_roate,button_scale,button_alpha,button_all;
private ImageView myImage;
private Animation animation;
/**
* 初始化按钮
*/
//private Animation animation;
private void initView(){
button_translate = (Button) findViewById(R.id.button_translate);
button_roate = (Button) findViewById(R.id.button_roate);
button_scale = (Button) findViewById(R.id.button_scale);
button_alpha = (Button) findViewById(R.id.button_alpha);
button_all = (Button) findViewById(R.id.button_all);
button_translate.setTag(ANIM_TRANSLATE);
button_roate.setTag(ANIM_ROATE);
button_scale.setTag(ANIM_SCALE);
button_alpha.setTag(ANIM_ALPH);
button_all.setTag(ANIM_ALL);
myImage = (ImageView) findViewById(R.id.myImage);
}
/**
* 点击事件
*/
private void initCtrl(){
button_translate.setOnClickListener(this);
button_alpha.setOnClickListener(this);
button_roate.setOnClickListener(this);
button_scale.setOnClickListener(this);
button_all.setOnClickListener(this);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initCtrl();
}
/**
* 移动
*/
private void TrandlateAnim(){
animation = AnimationUtils.loadAnimation(this,
R.anim.translate);
myImage.startAnimation(animation);
}
/**
* 透明度
*/
private void AlphAnim(){
animation = AnimationUtils.loadAnimation(this,
R.anim.alph);
myImage.startAnimation(animation);
}
/**
* 旋转
*/
private void RoateAnim(){
animation = AnimationUtils.loadAnimation(this,
R.anim.rotate);
myImage.startAnimation(animation);
}
/**
* 缩放
*/
private void ScaleAnim(){
animation = AnimationUtils.loadAnimation(this,
R.anim.scale);
myImage.startAnimation(animation);
}
/**
* 组合动画
*/
private void AllAnim(){
animation = AnimationUtils.loadAnimation(this,
R.anim.set);
myImage.startAnimation(animation);
}
@Override
public void onClick(View view) {
switch ((Integer)view.getTag()){
case ANIM_TRANSLATE:
TrandlateAnim();
break;
case ANIM_ALPH:
AlphAnim();
break;
case ANIM_ROATE:
RoateAnim();
break;
case ANIM_SCALE:
ScaleAnim();
break;
case ANIM_ALL:
AllAnim();
break;
}
}
}
缩放
透明度
组合动画