android中的动画

帧动画(属性动画)

View动画(补间动画)

 

帧动画:

1、建立文件夹drawable

2、在文件夹drawable建立xml文件

3、布局文件:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <!--oneshot:一次通过  -->
    <item android:drawable="@drawable/girl_1" android:duration="200" />
    <item android:drawable="@drawable/girl_2" android:duration="200" />
    <item android:drawable="@drawable/girl_3" android:duration="200" />
    <item android:drawable="@drawable/girl_4" android:duration="200" />
    <item android:drawable="@drawable/girl_5" android:duration="200" />
    <item android:drawable="@drawable/girl_6" android:duration="200" />
    <item android:drawable="@drawable/girl_7" android:duration="200" />
        <item android:drawable="@drawable/girl_6" android:duration="200" />
    <item android:drawable="@drawable/girl_7" android:duration="200" />
        <item android:drawable="@drawable/girl_6" android:duration="200" />
    <item android:drawable="@drawable/girl_7" android:duration="200" />
        <item android:drawable="@drawable/girl_6" android:duration="200" />
    <item android:drawable="@drawable/girl_7" android:duration="200" />
    <item android:drawable="@drawable/girl_8" android:duration="200" />
    <item android:drawable="@drawable/girl_9" android:duration="200" />
    <item android:drawable="@drawable/girl_10" android:duration="200" />
    <item android:drawable="@drawable/girl_11" android:duration="200" />
</animation-list>

   Activity:

public class MainActivity extends Activity {

	//动画引用
	AnimationDrawable rocketAnimation;

	public void onCreate(Bundle savedInstanceState) {
	  super.onCreate(savedInstanceState);
	  setContentView(R.layout.activity_main);

	  ImageView rocketImage = (ImageView) findViewById(R.id.iv);
	  rocketImage.setBackgroundResource(R.drawable.girl);
	  rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
	}

	public boolean onTouchEvent(MotionEvent event) {
	  if (event.getAction() == MotionEvent.ACTION_DOWN) {
	    rocketAnimation.start();
	    return true;
	  }
	  return super.onTouchEvent(event);
	}
}

补间动画(VIew动画):

public class MainActivity extends Activity {
 private ImageView iv;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  iv = (ImageView) findViewById(R.id.iv);
 }
 /**
  * 透明度变化的动画
  * 
  * @param view
  */
 public void alpha(View view) {
  // 声明动画 完全透明--》完全不透明  0:透明  1:不透明
  AlphaAnimation aa = new AlphaAnimation(0.0f, 1.0f);
  // 设置动画播放的时间
  aa.setDuration(2000);
  // 重复播放的次数
  aa.setRepeatCount(2);
  // 倒序播放
  aa.setRepeatMode(Animation.REVERSE);
  iv.startAnimation(aa);
 }
 /**
  * 缩放动画
  * 
  * @param view
  */
 public void scale(View view) {
  // 0.5:图片中心轴
  ScaleAnimation sa = new ScaleAnimation(0.1f, 2.0f, 0.1f, 2.0f,
    Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
    0.5f);
  // 设置动画播放的时间
  sa.setDuration(2000);
  // 重复播放的次数
  sa.setRepeatCount(2);
  // 倒序播放
  sa.setRepeatMode(Animation.REVERSE);
  iv.startAnimation(sa);
 }
 /**
  * 位移动画
  * 
  * @param view
  */
 public void trans(View view) {
  TranslateAnimation ta = new TranslateAnimation(
    Animation.RELATIVE_TO_PARENT, -0.5f,
    Animation.RELATIVE_TO_PARENT, 0.0f,
    Animation.RELATIVE_TO_PARENT, 0.0f,
    Animation.RELATIVE_TO_PARENT, 0.0f);
  // 设置动画播放的时间
  ta.setDuration(2000);
  // 重复播放的次数
  ta.setRepeatCount(2);
  // 倒序播放
  ta.setRepeatMode(Animation.REVERSE);
  iv.startAnimation(ta);
 }
 public void rotate(View view) {
  RotateAnimation ra = new RotateAnimation(0, 360,
    Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
    0.0f);
  // 设置动画播放的时间
  ra.setDuration(2000);
  // 重复播放的次数
  ra.setRepeatCount(2);
  // 倒序播放
  ra.setRepeatMode(Animation.REVERSE);
  iv.startAnimation(ra);
 }
 /**
  * 动画集合
  * @param view
  */
 public void set(View view){
  AnimationSet set = new AnimationSet(false);//每个动画时间变化的情况都是独立的
  ScaleAnimation sa = new ScaleAnimation(0.1f, 2.0f, 0.1f, 2.0f,
    Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
    0.5f);
  // 设置动画播放的时间
  sa.setDuration(2000);
  // 重复播放的次数
  sa.setRepeatCount(2);
  // 倒序播放
  sa.setRepeatMode(Animation.REVERSE);
  TranslateAnimation ta = new TranslateAnimation(
    Animation.RELATIVE_TO_PARENT, -0.2f,
    Animation.RELATIVE_TO_PARENT, 0.2f,
    Animation.RELATIVE_TO_PARENT, -0.2f,
    Animation.RELATIVE_TO_PARENT, 0.2f);
  // 设置动画播放的时间
  ta.setDuration(2000);
  // 重复播放的次数
  ta.setRepeatCount(2);
  // 倒序播放
  ta.setRepeatMode(Animation.REVERSE);
  RotateAnimation ra = new RotateAnimation(0, 360,
    Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
    0.0f);
  // 设置动画播放的时间
  ra.setDuration(2000);
  // 重复播放的次数
  ra.setRepeatCount(2);
  // 倒序播放
  ra.setRepeatMode(Animation.REVERSE);
  
  set.addAnimation(sa);//缩放
  set.addAnimation(ta);//位移
  set.addAnimation(ra);//旋转
  iv.startAnimation(set);
 }
}

猜你喜欢

转载自huanxiang0220.iteye.com/blog/2240844