首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

Animation(2)

2012-07-04 
Animation(二)本篇介绍如何用配置文件进行控件的动画设置。步骤如下:1. 在res目录下建立anim目录2. 在anim

Animation(二)
  本篇介绍如何用配置文件进行控件的动画设置。步骤如下:
  1. 在res目录下建立anim目录
  2. 在anim目录下创建动画的xml文件
  3. 通过AnimationUtils这个类加载动画的xml文件
  4. 给你需要的控件绑定Animation
  话不多说,代码如下:
 

package com.kevin.animation;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import android.widget.ImageView;public class AnimationDemo extends Activity {private ImageView img;private int flag = 0;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        img = (ImageView)findViewById(R.id.imageView1);        img.setOnClickListener(new ImgOnClickListenr());    }        class ImgOnClickListenr implements OnClickListener{@Overridepublic void onClick(View v) {switch (flag) {case 0:// 加载动画的配置文件Animation alphaAnimation = AnimationUtils.loadAnimation(AnimationDemo.this, R.anim.alpha);// 调用animationimg.startAnimation(alphaAnimation);flag++;break;case 1:Animation rotateAnimation = AnimationUtils.loadAnimation(AnimationDemo.this, R.anim.rotate);img.startAnimation(rotateAnimation);flag++;break;case 2:Animation scaleAnimation = AnimationUtils.loadAnimation(AnimationDemo.this, R.anim.back_scale);img.startAnimation(scaleAnimation);flag++;break;case 3:Animation translateAnimation = AnimationUtils.loadAnimation(AnimationDemo.this, R.anim.translate);img.startAnimation(translateAnimation);flag = 0;default:break;}}        }}  

这里举个配置文件说明一下,代码如下:
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"><scale android:fromXScale="0.0"   android:toXScale="1.0"   android:fromYScale="1.0"   android:toYScale="1.0"   android:pivotX="50%"   android:pivotY="50%"   android:duration="5000"/>   </set>

  注意这边的piovtX属性后面跟的值,因为scale缩放时,轴心的位置相对于控件的位置有三种:
  1. 当你写50时,它是采用的absolute去取轴心的位置
  2. 当你写50%时,它是采用的RELATIVE_TO_SELF去取轴心的位置
  3. 当你写50%p时,它是采用的RELATIVE_TO_PARENT去取轴心的位置
好了就写到这里吧。

热点排行