android 四种实用常见的补间动画

package com.example.my1104_k;


import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;


public class MainActivity extends Activity {


private View iv;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = findViewById(R.id.iv_fly);
}


// 透明度
// 第一个参数fromAlpha:动画起始时的透明度, 第二个参数toAlpha: 动画结束时的透明度。
public void alpha(View v) {
AlphaAnimation alpha = new AlphaAnimation(1f, 0.1f);
alpha.setDuration(2000);
alpha.setRepeatCount(3);
alpha.setRepeatMode(Animation.REVERSE);
iv.startAnimation(alpha);
}


// 位移
/*
* 参数1,参数3,参数5,参数7: 设置参照点的方式(相对自己)Animation.RELATIVE_TO_SELF 参数2:x轴起始移动的位置
* (0表示原图位置左上角x轴的坐标) 参数4:x轴停止移动的位置(2表示移动原图宽度的两倍) 参数6:y轴起始移动的位置
* (0表示原图位置左上角y轴的坐标) 参数8:y轴停止移动的位置(2表示移动原图高度的两倍)
*/
public void translate(View v) {
TranslateAnimation t = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 2,
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 2);
t.setDuration(2000);
t.setRepeatCount(3);
t.setRepeatMode(Animation.REVERSE);
iv.startAnimation(t);
}


// 旋转
/*
* 参数1:旋转的起始角度 参数2:旋转的终止角度 参数3:旋转中心的x轴取值参照方式 参数4:中心点x轴的取值(0.5f表示相对与原图的0.5倍)
* 参数5:旋转中心的y轴取值参照方式 参数6:中心点y轴的取值(0.5f表示相对与原图的0.5倍)
*/
public void rotate(View v) {
RotateAnimation rotate = new RotateAnimation(360, 0,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
rotate.setDuration(2000);
rotate.setRepeatCount(3);
rotate.setRepeatMode(Animation.REVERSE);
iv.startAnimation(rotate);
}


// 缩放
// 第一个参数fromAlpha:动画起始时的透明度, 第二个参数toAlpha: 动画结束时的透明度。
public void scale(View v) {
ScaleAnimation scale = new ScaleAnimation(8f, 0.2f, 8f, 0.2f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
scale.setDuration(2000);
scale.setRepeatCount(3);
scale.setRepeatMode(Animation.REVERSE);
iv.startAnimation(scale);
}


// 集合
public void jh(View v) {
//动画集合
AnimationSet set = new AnimationSet(false);

//旋转动画
RotateAnimation rotate = new RotateAnimation(360, 0,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
rotate.setDuration(3000);
rotate.setRepeatCount(3);
rotate.setRepeatMode(Animation.RELATIVE_TO_PARENT);

//透明动画
AlphaAnimation alpha = new AlphaAnimation(1f, 0.1f);
alpha.setDuration(3000);
alpha.setRepeatCount(3);
alpha.setRepeatMode(Animation.REVERSE);

//平移动画
TranslateAnimation t = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 2,
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 2);
t.setDuration(3000);
t.setRepeatCount(3);
t.setRepeatMode(Animation.REVERSE);

//缩放动画
ScaleAnimation scale = new ScaleAnimation(4f, 0.2f, 4f, 0.2f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
scale.setDuration(3000);
scale.setRepeatCount(3);
scale.setRepeatMode(Animation.REVERSE);


set.addAnimation(t);
set.addAnimation(alpha);
set.addAnimation(rotate);
set.addAnimation(scale);

//播放动画
iv.startAnimation(set);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值