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