Chinaunix首页 | 论坛 | 博客
  • 博客访问: 305140
  • 博文数量: 65
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 284
  • 用 户 组: 普通用户
  • 注册时间: 2013-11-21 15:10
文章分类
文章存档

2016年(3)

2015年(51)

2014年(8)

2013年(3)

分类: Android平台

2015-04-13 17:11:54

一、Animations:提供一系列的动画效果,可以应用在绝大多数控件
二、总体分为两类:
    1、Tween Aniomations 提供旋转、移动、伸展、淡出等效果
    2、Freme-by-Frame Animations 创建一个Drawable序列,这些序列按照一定的时间间隔逐个显示
三、 TweenAnimations分类:
    1、Alpha 淡入淡出
    2、Scale 缩放效果
    3、Rotate 旋转效果
    4、Translate 移动效果
四、使用animation的步骤
    1、创建一个AnimationSet对象
    2、根据需要创建对应的Animation对象
    3、根据动画的需求为Animation对象设置数据
    4、将Animation对象添加到AnimationSet当中
    5、使用控件对象执行AnimationSet
五、TweenAnimation的通用属性
    1、setFillAfter(boolean fillAfter);   当fillAfter为true,控件停留在动画执行结束的状态
    2、setFillBefore(boolean fillBefore); 当fillBefore为true,控件停留在动画执行之前的状态
    3、setStartOffSet(long startOffSet); 设置动画执行前等待的时间
    4、setRepeatCount(int repeatCount); 设置动画重复的次数
    5、setDuration(long durationMills); 设置动画执行的时间
六、Animation的实现方法
    1、在代码中实现
    2、在xml文件中实现
七、在代码中实现Animation
    1、MainActicity.java

点击(此处)折叠或打开

  1. public class MainActivity extends Activity
  2. {
  3.     //定义几个button
  4.     private Button alphaButton = null;
  5.     private Button scaleButton = null;
  6.     private Button rotateButton = null;
  7.     private Button translateButton = null;
  8.     private ImageView imageView = null;
  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.             //创建AnimationSet对象
  33.             AnimationSet animationSet = new AnimationSet(true);
  34.             // TODO Auto-generated method stub
  35.             switch(v.getId())
  36.             {
  37.                 case R.id.alphaBtn:
  38.                     //透明效果
  39.                     //创建一个alphaAnimation,从完全透明到完全不透明
  40.                     AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
  41.                     //动画执行的时间,单位ms
  42.                     alphaAnimation.setDuration(1000);
  43.                     
  44.                     //将animation添加到AnimationSet对象中
  45.                     animationSet.addAnimation(alphaAnimation);
  46.                     //开始执行动画
  47.                     imageView.startAnimation(animationSet);
  48.                     break;
  49.                 case R.id.scaleBtn:
  50.                     //缩放效果
  51.                     //创建ScaleAnimation,横纵都是从原来缩小到10%
  52.                     ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1, 0.1f,
  53.                                 Animation.RELATIVE_TO_SELF, 0.5f,
  54.                                 Animation.RELATIVE_TO_SELF, 0.5f);
  55.                     //动画执行的时间,单位ms
  56.                     scaleAnimation.setDuration(3000);
  57.                     //将animation添加到AnimationSet对象中
  58.                     animationSet.addAnimation(scaleAnimation);
  59.                     //开始执行动画
  60.                     imageView.startAnimation(animationSet);
  61.                     break;
  62.                 case R.id.rotateBtn:
  63.                     /*旋转效果
  64.                     第一个参数是起始角度(12点位置是0度)
  65.                     第二个参数是停止角度
  66.                     第三个参数旋转圆心横坐标类型
  67.                     第四个参数旋转圆心横坐标大小
  68.                     第五个参数旋转圆心纵坐标类型
  69.                     第六个参数旋转圆心纵坐标大小
  70.                     Animation.RELATIVE_TO_PARENT相对于父控件的坐标
  71.                     Animation.RELATIVE_TO_SELF相对于自身的坐标
  72.                     手机的原点在左上角
  73.                     */
  74.                     RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
  75.                                                         Animation.RELATIVE_TO_PARENT, 1f,
  76.                                                         Animation.RELATIVE_TO_PARENT, 0f);
  77.                     //设置动画时间
  78.                     rotateAnimation.setDuration(3000);
  79.                     //将animation添加到AnimationSet对象中
  80.                     animationSet.addAnimation(rotateAnimation);
  81.                     //开始执行动画
  82.                     imageView.startAnimation(animationSet);
  83.                     break;
  84.                 case R.id.translateBtn:
  85.                     TranslateAnimation translateAnimation = new TranslateAnimation(
  86.                             Animation.RELATIVE_TO_SELF, 0f,
  87.                             Animation.RELATIVE_TO_SELF, 0.5f,
  88.                             Animation.RELATIVE_TO_SELF, 0f,
  89.                             Animation.RELATIVE_TO_SELF, 0.5f
  90.                             );
  91.                     //设置动画时间
  92.                     translateAnimation.setDuration(3000);
  93.                     //将animation添加到AnimationSet对象中
  94.                     animationSet.addAnimation(translateAnimation);
  95.                     //开始执行动画
  96.                     imageView.startAnimation(animationSet);
  97.                     break;
  98.                 default:
  99.                         break;
  100.             }
  101.             
  102.         }
  103.         
  104.     }

  105.     @Override
  106.     public boolean onCreateOptionsMenu(Menu menu)
  107.     {
  108.         // Inflate the menu; this adds items to the action bar if it is present.
  109.         getMenuInflater().inflate(R.menu.main, menu);
  110.         return true;
  111.     }

  112. }
    2、布局文件

点击(此处)折叠或打开

  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>
阅读(1704) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~