animation 控件之代码控制

Q群: 241359063 更精彩,欢迎共同走向创业学习之旅。
原创:kylin_zeng  http://blog.chinaunix.net/uid/23795897.html在此感谢mars 老师的帮助。转载请注明原创出处,尊重他人的劳动成果


1、animations 提供一个动画的效果。
2、animation 分成两类:
   第一类: Tweened Animations,提供旋转Rotate,移动Translate,伸展缩放Scale,淡入淡出Alpha.
   第二类: Frame-by-Frame animation. 这类可以创建一个drawable 系列,可以按照指定时间间隔显示。




3、第一类详解:步骤:
   3.1 创建一个AnimationSet 对象;
   3.2 根据需要创建队友的Animation 对象;
   3.3 根据软件动画的需要为Animation对象设置对应的数据。
   3.4 将Animation 对象添加到AnimationSet 对象里面。
   3.5 使用空间对象开始执行AnimationSet.


 创建ImageView图片来查看效果:
<ImageView android:id="@+id/imageViewId"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_centerInParent="true" android:layout_marginTop="100dip"
android:src="@drawable/icon" />

1、创建几个按钮来控制:

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical" android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent">

  5.     <Button android:id="@+id/scaleButtonId" android:layout_width="fill_parent"
  6.         android:layout_height="wrap_content" android:layout_alignParentBottom="true"
  7.         android:text="测试scale动画效果" />
  8.         
  9.     <Button android:id="@+id/rotateButtonId" android:layout_width="fill_parent"
  10.         android:layout_height="wrap_content" android:layout_above="@id/scaleButtonId"
  11.         android:text="测试rotate动画效果" />
  12.         
  13.     <Button android:id="@+id/alphaButtonId" android:layout_width="fill_parent"
  14.         android:layout_height="wrap_content" android:layout_above="@id/rotateButtonId"
  15.         android:text="测试alpha动画效果" />

  16.     <Button android:id="@+id/translateButtonId"
  17.         android:layout_width="fill_parent" android:layout_height="wrap_content"
  18.         android:layout_above="@id/alphaButtonId" android:text="测试translate动画效果" />
  19.         
  20.     <LinearLayout android:orientation="vertical"
  21.         android:layout_width="fill_parent" android:layout_height="fill_parent">

  22.         <ImageView android:id="@+id/imageViewId"
  23.             android:layout_width="wrap_content" android:layout_height="wrap_content"
  24.             android:layout_centerInParent="true" android:layout_marginTop="100dip"
  25.             android:src="@drawable/icon" />
  26.     </LinearLayout>
  27. </RelativeLayout>

2、

点击(此处)折叠或打开

  1. package mars.animations01;

  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.RotateAnimation;
  10. import android.view.animation.ScaleAnimation;
  11. import android.view.animation.TranslateAnimation;
  12. import android.widget.Button;
  13. import android.widget.ImageView;

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

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

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

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

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

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

  35.     private class RotateButtonListener implements OnClickListener {

  36.         @Override
  37.         public void onClick(View view) {
  38.             AnimationSet animationSet = new AnimationSet(true);
  39.             RotateAnimation rotateAnimation = new RotateAnimation(0, 360,    /*从0旋转到360度*/
  40.                     Animation.RELATIVE_TO_PARENT, 1f,                       /*x轴以父控件为参照物,1f -> 从0~1,如果是0.5表示一半*/
  41.                     Animation.RELATIVE_TO_PARENT, 0f);                      /*y轴以父控件为参照物,0f -> 从0~1,如果是0.5表示一半*/
  42.             rotateAnimation.setDuration(5000);                  //持续时间5000ms
  43.             animationSet.addAnimation(rotateAnimation);
  44.             imageView.startAnimation(animationSet);
  45.         }
  46.     }

  47.     private class ScaleButtonListener implements OnClickListener {

  48.         @Override
  49.         public void onClick(View view) {
  50.             AnimationSet animationSet = new AnimationSet(true);
  51.             ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1, 0.5f,   /*1,0.1f, x轴从1到10%。 1,0.5f, y轴从1到0.5*/
  52.                     Animation.RELATIVE_TO_SELF, 0.5f,
  53.                     Animation.RELATIVE_TO_SELF, 0.5f);
  54.             animationSet.addAnimation(scaleAnimation);
  55.             animationSet.setStartOffset(1000);
  56.             animationSet.setFillAfter(true);
  57.             animationSet.setFillBefore(false);
  58.             animationSet.setDuration(2000);
  59.             imageView.startAnimation(animationSet);
  60.         }

  61.     }

  62.     private class AlphaButtonListener implements OnClickListener {

  63.         @Override
  64.         public void onClick(View view) {
  65.             //创建一个AnimationSet对象
  66.             AnimationSet animationSet = new AnimationSet(true);
  67.             //创建一个AlphaAnimation对象
  68.             AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0)
  69.             //设置动画执行的时间(单位:毫秒)
  70.             alphaAnimation.setDuration(1000);
  71.             //将AlphaAnimation对象添加到AnimationSet当中
  72.             animationSet.addAnimation(alphaAnimation);
  73.             //使用ImageView的startAnimation方法开始执行动画
  74.             imageView.startAnimation(animationSet);
  75.         }

  76.     }

  77.     private class TranslateButtonListener implements OnClickListener {

  78.         @Override
  79.         public void onClick(View view) {
  80.             AnimationSet animationSet = new AnimationSet(true);
  81.             TranslateAnimation translateAnimation = new TranslateAnimation(
  82.                     Animation.RELATIVE_TO_SELF, 0f,           /*x轴移动开始值*/
  83.                     Animation.RELATIVE_TO_SELF, 0.5f,         /*x轴移动后的值*/
  84.                     Animation.RELATIVE_TO_SELF, 0f,           /*y轴移动开始值*/
  85.                     Animation.RELATIVE_TO_SELF, 1.0f);        /*y轴移动后的值*/
  86.             translateAnimation.setDuration(1000);
  87.             animationSet.addAnimation(translateAnimation);
  88.             imageView.startAnimation(animationSet);
  89.         }
  90.     }
  91. }

02_08.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>
阅读(34) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值