Chinaunix首页 | 论坛 | 博客
  • 博客访问: 551705
  • 博文数量: 156
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1183
  • 用 户 组: 普通用户
  • 注册时间: 2013-11-22 11:42
文章分类

全部博文(156)

文章存档

2015年(67)

2014年(89)

分类: Android平台

2015-04-13 16:53:14

在xml中实现animation
    一、在res文件夹下新建一个名为anim的文件夹
    二、创建xml文件,首先加入set标签
                     xmlns:android=""
            android:interpolator="@android:anim/accelerate_interpolator"
        >
        
    三、在set标签中加入rotate、translate、alpha、scale标签,每一个动画效果占一个xml文件

点击(此处)折叠或打开

  1.     (1)alpha.xml:    
  2.          <?xml version="1.0" encoding="utf-8"?>
  3.         <set
  4.          xmlns:android=""
  5.          android:interpolator="@android:anim/accelerate_interpolator">
  6.          <alpha                           <?透明效果?>            
  7.          android:fromAlpha="0.1"          <?开始透明度10%?>
  8.          android:toAlpha="1.0"            <?结束透明度?>
  9.          android:duration="3000"          <?设置动画执行时间?>
  10.          android:startOffset="500"        <?设置动画执行前的等待时间?>
  11.          />
  12.         </set>
  13.     
  14.     (2)rotate.xml:
  15.         <?xml version="1.0" encoding="utf-8"?>
  16.         <set
  17.          xmlns:android=""
  18.          android:interpolator="@android:anim/accelerate_interpolator">
  19.              <rotate                       <?旋转效果?>
  20.              android:fromDegrees="0"       <?起始角度?>
  21.              android:toDegrees="+350"      <?结束角度?>
  22.              android:pivotX="50%"          <?旋转点的x坐标 “50”代表绝对位置,“50%”相对于控件本身,“50%p”相对于父控件?>
  23.              android:pivotY="50%"          <?旋转点的y坐标?>
  24.              android:duration="3000"       <?动画执行的时间?>
  25.          />
  26.         </set>
  27.     (3)scale.xml
  28.         <?xml version="1.0" encoding="utf-8"?>
  29.         <set
  30.          xmlns:android=""
  31.          android:interpolator="@android:anim/accelerate_interpolator">
  32.             <scale                              <?缩放效果?>
  33.                 android:fromXScale="1.0"        <?起始x坐标?>
  34.                 android:toXScale="0.1"          <?结束x坐标?>
  35.                 android:fromYScale="1.0"        <?起始y坐标?>
  36.                 android:toYScale="0.1"          <?结束y坐标?>
  37.                 android:pivotX="50%"            <?缩放原点x坐标?>
  38.                 android:pivotY="50%"            <?缩放原点y坐标?>
  39.                 android:duration="3000"         <?动画持续时间?>
  40.              />
  41.         </set>
    四、在代码中使用AnimationUtils装载xml文件,并生成Animation对象
        Animation animation_alpha = AnimationUtils.loadAnimation(MainActivity.this , R.anim.alpha);
        imageView.startAnimation(animation_alpha);
    、Animation的实现

点击(此处)折叠或打开

  1. public class MainActivity extends Activity
  2. {
  3.     private Button alphaButton = null;
  4.     private Button scaleButton = null;
  5.     private Button rotateButton = null;
  6.     private Button translateButton = null;
  7.     private ImageView imageView = null;
  8.     
  9.     @Override
  10.     protected void onCreate(Bundle savedInstanceState)
  11.     {
  12.         super.onCreate(savedInstanceState);
  13.         setContentView(R.layout.activity_main);
  14.         //找到控件
  15.         alphaButton = (Button)findViewById(R.id.alphaBtn);
  16.         scaleButton = (Button)findViewById(R.id.scaleBtn);
  17.         rotateButton = (Button)findViewById(R.id.rotateBtn);
  18.         translateButton= (Button)findViewById(R.id.translateBtn);
  19.         imageView = (ImageView)findViewById(R.id.imageViewId);
  20.         //为控件添加事件
  21.         alphaButton.setOnClickListener(new btnListener());
  22.         scaleButton.setOnClickListener(new btnListener());
  23.         rotateButton.setOnClickListener(new btnListener());
  24.         translateButton.setOnClickListener(new btnListener());
  25.     }
  26.     
  27.     class btnListener implements OnClickListener
  28.     {

  29.         @Override
  30.         public void onClick(View v)
  31.         {
  32.             // TODO Auto-generated method stub
  33.             switch(v.getId())
  34.             {
  35.                 case R.id.alphaBtn:
  36.                     //透明效果
  37.                     Animation animation_alpha = AnimationUtils.loadAnimation(MainActivity.this , R.anim.alpha);
  38.                     imageView.startAnimation(animation_alpha);
  39.                     break;
  40.                 case R.id.scaleBtn:
  41.                     //缩放效果
  42.                     Animation animation_scale = AnimationUtils.loadAnimation(MainActivity.this , R.anim.scale);
  43.                     imageView.startAnimation(animation_scale);
  44.                     break;
  45.                 case R.id.rotateBtn:
  46.                     //旋转效果
  47.                     Animation animation_rotate = AnimationUtils.loadAnimation(MainActivity.this , R.anim.rotate);
  48.                     imageView.startAnimation(animation_rotate);
  49.                     break;
  50.                 case R.id.translateBtn:
  51.                     //移动效果
  52.                     Animation animation_translate = AnimationUtils.loadAnimation(MainActivity.this , R.anim.translate);
  53.                     imageView.startAnimation(animation_translate);
  54.                     break;
  55.                 default:
  56.                         break;
  57.             }            
  58.         }
  59.         
  60.     }
  61.     @Override
  62.     public boolean onCreateOptionsMenu(Menu menu)
  63.     {
  64.         // Inflate the menu; this adds items to the action bar if it is present.
  65.         getMenuInflater().inflate(R.menu.main, menu);
  66.         return true;
  67.     }

  68. }
    xml文件
    1、alpha.xml

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set
  3.     xmlns:android=""
  4.     android:interpolator="@android:anim/accelerate_interpolator">
  5.  
  6.     <alpha
  7.         android:fromAlpha="0.1"
  8.         android:toAlpha="1.0"
  9.         android:duration="3000"
  10.         android:startOffset="500"
  11.         />
  12. </set>
    2、scalse.xml

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set
  3.     xmlns:android=""
  4.     android:interpolator="@android:anim/accelerate_interpolator">
  5.     
  6.     <scale
  7.         android:fromXScale="1.0"
  8.         android:toXScale="0.1"
  9.         android:fromYScale="1.0"
  10.         android:toYScale="0.1"
  11.         android:pivotX="50%"
  12.         android:pivotY="50%"
  13.         android:duration="3000"
  14.     />
  15.    
  16. </set>
    3、rotate.xml

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set
  3.     xmlns:android=""
  4.     android:interpolator="@android:anim/accelerate_interpolator">
  5.     
  6.     <rotate
  7.            android:fromDegrees="0"
  8.            android:toDegrees="+350"
  9.            android:pivotX="50%"
  10.            android:pivotY="50%"
  11.            android:duration="3000"
  12.         />
  13.    
  14. </set>
    4、translate.xml

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set
  3.     xmlns:android=""
  4.     android:interpolator="@android:anim/accelerate_interpolator"
  5.     >
  6.    <translate
  7.        
  8.        />
  9.    
  10. </set>
    布局文件

点击(此处)折叠或打开

  1. <RelativeLayout xmlns:android=""
  2.     xmlns:tools=""
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:paddingBottom="@dimen/activity_vertical_margin"
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"
  7.     android:paddingRight="@dimen/activity_horizontal_margin"
  8.     android:paddingTop="@dimen/activity_vertical_margin"
  9.     tools:context=".MainActivity" >

  10.     <TextView
  11.         android:layout_width="wrap_content"
  12.         android:layout_height="wrap_content"
  13.         android:text="@string/hello_world" />
  14.     <Button
  15.         android:id="@+id/alphaBtn"
  16.         android:layout_width="fill_parent"
  17.         android:layout_height="wrap_content"
  18.         android:layout_alignParentBottom="true"
  19.         android:text="alpha动画"
  20.         />
  21.     <Button
  22.         android:id="@+id/scaleBtn"
  23.         android:layout_width="fill_parent"
  24.         android:layout_height="wrap_content"
  25.         android:layout_above="@id/alphaBtn"
  26.         android:text="scale动画"
  27.         />
  28.     <Button
  29.         android:id="@+id/rotateBtn"
  30.         android:layout_width="fill_parent"
  31.         android:layout_height="wrap_content"
  32.         android:layout_above="@id/scaleBtn"
  33.         android:text="rotate动画"
  34.         />
  35.     <Button
  36.         android:id="@+id/translateBtn"
  37.         android:layout_width="fill_parent"
  38.         android:layout_height="wrap_content"
  39.         android:layout_above="@id/rotateBtn"
  40.         android:text="translate动画"
  41.         />
  42.     <ImageView
  43.         android:id="@+id/imageViewId"
  44.         android:layout_height="wrap_content"
  45.         android:layout_width="wrap_content"
  46.         android:layout_centerInParent="true"
  47.         android:layout_marginTop="100dip"
  48.         android:src="@drawable/ic_launcher"
  49.         />
  50. </RelativeLayout>

阅读(1294) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~