animation 之xml控制

来源:互联网 发布:淘宝售后怎么赚钱 编辑:程序博客网 时间:2024/05/22 13:24
Q群: 241359063 更精彩,欢迎共同走向创业学习之旅。
原创:kylin_zeng  http://blog.chinaunix.net/uid/23795897.html在此感谢mars 老师的帮助。转载请注明原创出处,尊重他人的劳动成果。


二、第二种使用:
 
1、在res文件夹厦门新建一个anim的文件夹;


2、创建xml文件,并首先要加入set标签,如下:
  <set xmlns:android="http://schemas.android.com/apk/res/android"
      android:interpolator="@android:anim/accelerate_interpolator">
  </set>


3、在该标签当中加入rotate,alpha,scale或者translate标签。


4、在代码中使用AnimationUtils当中转载xml 文件,并生产Animation对象。


例如:
1、在res下面建立一个anim文件夹。

2、里面创建了四个xml文件,alpha.xml,

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:interpolator="@android:anim/accelerate_interpolator">

  4.         
  5.     <alpha
  6.         android:fromAlpha="1.0"
  7.         android:toAlpha="0.0"
  8.         android:startOffset="500"
  9.         android:duration="500" />

  10. </set>
rotate.xml,

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:interpolator="@android:anim/accelerate_interpolator">

  4.     <rotate android:fromDegrees="0"
  5.         android:toDegrees="360"
  6.         android:pivotX="50%"            
  7.         //这里有三种表示方法:50  :是绝对坐标,表示屏幕上真正的50,对应代码控制中的toplant,
  8.         //                  50% : 是相对坐标,表示相对自身控件的50%,即控件的中心点。
  9.         //                  50%p: 是相对于父控件,表示父控件的50%位置。
  10.         android:pivotY="50%"
  11.         android:duration="5000" />
  12. </set>

scale.xml,

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:interpolator="@android:anim/accelerate_interpolator">

  4.     <scale android:fromXScale="1.0"
  5.         android:toXScale="0.0"
  6.         android:fromYScale="1.0"
  7.         android:toYScale="0.0"
  8.         android:pivotX="50%"
  9.         android:pivotY="50%"
  10.         android:duration="2000" />

  11. </set>

translate.xml


点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:interpolator="@android:anim/accelerate_interpolator">

  4.     <translate
  5.         android:fromXDelta="50%"
  6.         android:toXDelta="100%"
  7.         android:fromYDelta="0%"
  8.         android:toYDelta="100%"
  9.         android:duration="2000" />

  10. </set>

4、调用:

点击(此处)折叠或打开

  1. package mars.animations02;

  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.view.View.OnClickListener;
  6. import android.view.animation.AlphaAnimation;
  7. import android.view.animation.Animation;
  8. import android.view.animation.AnimationSet;
  9. import android.view.animation.AnimationUtils;
  10. import android.view.animation.RotateAnimation;
  11. import android.view.animation.ScaleAnimation;
  12. import android.view.animation.TranslateAnimation;
  13. import android.widget.Button;
  14. import android.widget.ImageView;

  15. public class MainActivity extends Activity {
  16.     /** Called when the activity is first created. */
  17.     private ImageView imageView = null;
  18.     private Button rotateButton = null;
  19.     private Button scaleButton = null;
  20.     private Button alphaButton = null;
  21.     private Button translateButton = null;

  22.     @Override
  23.     public void onCreate(Bundle savedInstanceState) {
  24.         super.onCreate(savedInstanceState);
  25.         setContentView(R.layout.main);
  26.         imageView = (ImageView) findViewById(R.id.imageViewId);

  27.         rotateButton = (Button) findViewById(R.id.rotateButtonId);
  28.         rotateButton.setOnClickListener(new RotateButtonListener());

  29.         scaleButton = (Button) findViewById(R.id.scaleButtonId);
  30.         scaleButton.setOnClickListener(new ScaleButtonListener());

  31.         alphaButton = (Button) findViewById(R.id.alphaButtonId);
  32.         alphaButton.setOnClickListener(new AlphaButtonListener());

  33.         translateButton = (Button) findViewById(R.id.translateButtonId);
  34.         translateButton.setOnClickListener(new TranslateButtonListener());
  35.     }

  36.     private class RotateButtonListener implements OnClickListener {

  37.         @Override
  38.         public void onClick(View view) {
  39.             Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate);
  40.             imageView.startAnimation(animation);
  41.         }
  42.     }

  43.     private class ScaleButtonListener implements OnClickListener {

  44.         @Override
  45.         public void onClick(View view) {
  46.             Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.sacle);
  47.             imageView.startAnimation(animation);
  48.         }

  49.     }

  50.     private class AlphaButtonListener implements OnClickListener {

  51.         @Override
  52.         public void onClick(View view) {
  53.             //使用AnimationUtils装载动画设置文件
  54.             Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);
  55.             imageView.startAnimation(animation);
  56.         }

  57.     }

  58.     private class TranslateButtonListener implements OnClickListener {

  59.         @Override
  60.         public void onClick(View view) {
  61.             Animation animation = (Animation) AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate);
  62.             imageView.startAnimation(animation);
  63.         }

  64.     }
  65. }


02_09.zip




<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(65) | 评论(0) | 转发(0) |
0

上一篇:Ubuntu文件系统层 系统启动过程详解

下一篇:animation 之动画时间的控制

相关热门文章
  • Android之开发环境搭建
  • Android自定义View的实现...
  • AndroidManifest.xml配置文件...
  • Android源码调试方法详解...
  • 不用vs和cygwin!Eclipse+cdt...
  • 请问Linux默认shell的是什么 ...
  • 谁能够帮我解决LINUX 2.6 10...
  • 现在的博客积分不会更新了吗?...
  • shell怎么读取网页内容...
  • ssh等待连接的超时问题...
给主人留下些什么吧!~~
原创粉丝点击