加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

animation 之xml控制

发布时间:2020-12-16 06:33:27 所属栏目:百科 来源:网络整理
导读:Q群:241359063 更精彩,欢迎共同走向创业学习之旅。 原创:kylin_zeng http://blog.chinaunix.net/uid/23795897.html在此感谢mars 老师的帮助。转载请注明原创出处,尊重他人的劳动成果。 二、第二种使用: 1、在res文件夹厦门新建一个anim的文件夹; 2、创
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. <alpha
  5. android:fromAlpha="1.0"
  6. android:toAlpha="0.0"
  7. android:startOffset="500"
  8. android:duration="500" />

  9. </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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读