1.帧动画
把连续图片放入 drawable 目录 以及在该目录创建创建一个xml 文件
程序片段
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView) findViewById(R.id.iv); iv.setBackgroundResource(R.drawable.gril_list); an = (AnimationDrawable) iv.getBackground(); } public void play(View view) { an.start(); }
##2. 程序创建动画
//平移 public void playTranslate(View view) { TranslateAnimation ta = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f); ta.setDuration(2000); ta.setRepeatCount(2); ta.setRepeatMode(Animation.REVERSE); lanucher.startAnimation(ta); } //透明度 public void alpha(View view) { AlphaAnimation an = new AlphaAnimation(0.0f, 1.0f); an.setDuration(2000); an.setRepeatCount(2); an.setRepeatMode(Animation.REVERSE); lanucher.startAnimation(an); } //缩放 public void scale(View view) { ScaleAnimation sa = new ScaleAnimation(0.2f, 2.0f, 0.2f, 2.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); sa.setDuration(2000); sa.setRepeatCount(2); sa.setRepeatMode(Animation.REVERSE); lanucher.startAnimation(sa); } //旋转 public void rotate(View view) { RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); ra.setDuration(2000); ra.setRepeatCount(2); ra.setRepeatMode(Animation.REVERSE); lanucher.startAnimation(ra); } //动画集合 public void set(View view) { AnimationSet set = new AnimationSet(false); RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); ra.setDuration(2000); ra.setRepeatCount(2); ra.setRepeatMode(Animation.REVERSE); TranslateAnimation ta = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f); ta.setDuration(2000); ta.setRepeatCount(2); ta.setRepeatMode(Animation.REVERSE); lanucher.startAnimation(ta); set.addAnimation(ra); set.addAnimation(ta); lanucher.startAnimation(set); }
##3. xml配置动画
- 透明度
- 旋转
- 缩放
- 平移
- 集合
程序片段
private ImageView xiv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.xml_main); xiv = (ImageView) findViewById(R.id.xiv); } //透明度 public void alpha(View view) { Animation am = AnimationUtils.loadAnimation(this, R.anim.alpha); xiv.startAnimation(am); }