zoukankan      html  css  js  c++  java
  • Android UI开发第十二篇——动画效果Animation(一)

    Android框架本身就使用了大量的动画效果,比如Activity切换的动画效果,Dialog弹出和关闭时的渐变动画效果以及Toast显示信息时的淡入淡出效果等等。Android系统框架为我们提供了一些动画类及其工具类,所以在Andorid应用中使用动画效果非常简单。Android中可以在xml中定义Animation,也可以在java code中定义。

    Android中动画的实现分两种方式,一种方式是补间动画 Tween Animation,就是说你定义一个开始和结束,中间的部分由android自身实现。另一种叫逐帧动画 Frame Animation,就是说一帧一帧的连起来播放就变成了动画。

    一、Tween Animation

    xml中实现:

    alpha渐变透明度动画效果
    scale渐变尺寸伸缩动画效果
    translate画面转换位置移动动画效果
    rotate画面转移旋转动画效果


    JavaCode

    AlphaAnimation渐变透明度动画效果
    ScaleAnimation渐变尺寸伸缩动画效果
    TranslateAnimation画面转换位置移动动画效果
    RotateAnimation画面转移旋转动画效果

    使用XML文件定义Tween Animation时XML文件的根节点可以是<alpha>、<scale> <translate>、<rotate>或者是把它们都放入<set>节点中。如下:

    <?xml version="1.0" encoding="utf-8"?>
    < set xmlns:android="http://schemas.android.com/apk/res/android">
      <alpha/>
      <scale/>
      <translate/>
      <rotate/>
    < /set>

    Java Code实现如下:

    AlphaAnimation:


    AnimationSet animationSet = new AnimationSet(true);//创建一个AnimationSet对象  
    AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);//创建一个AlphaAnimation对象           
    alphaAnimation.setDuration(1000);//设置动画执行的时间(单位:毫秒)       
    animationSet.addAnimation(alphaAnimation);//将AlphaAnimation对象添加到AnimationSet当中        
    view.startAnimation(animationSet);//使用view的startAnimation方法开始执行动画    


    RotateAnimation :

    AnimationSet animationSet = new AnimationSet(true);
    /** 
    * 前两个参数定义旋转的起始和结束的度数,后两个参数定义圆心的位置 
    */
    RotateAnimation rotateAnimation = new RotateAnimation(0, 360,    
                        Animation.RELATIVE_TO_PARENT, 1f,    
                        Animation.RELATIVE_TO_PARENT, 0f);    
                rotateAnimation.setDuration(5000);    
                animationSet.addAnimation(rotateAnimation);    
                imageView.startAnimation(animationSet); 


    TranslateAnimation:

    AnimationSet animationSet = new AnimationSet(true);    
    /** 
                 * x和y轴的起始和结束位置 
    */
    TranslateAnimation translateAnimation = new TranslateAnimation    
                (    
                        Animation.RELATIVE_TO_SELF, 0f,     
                        Animation.RELATIVE_TO_SELF,0.5f,     
                        Animation.RELATIVE_TO_SELF, 0f,    
                        Animation.RELATIVE_TO_SELF, 1.0f    
                );    
                translateAnimation.setDuration(1000);    
                animationSet.addAnimation(translateAnimation);    
    view.startAnimation(animationSet);  
    



    ScaleAnimation:


    AnimationSet animationSet = new AnimationSet(true);    
    /** 
                 * 围绕一个点伸缩 
    */
    ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1,    
                        0.1f, Animation.RELATIVE_TO_SELF, 0.5f,    
                        Animation.RELATIVE_TO_SELF, 0.5f);    
                animationSet.addAnimation(scaleAnimation);    
                animationSet.setStartOffset(1000);    
                animationSet.setFillAfter(true);    
                animationSet.setFillBefore(false);    
                animationSet.setDuration(2000);    
    view.startAnimation(animationSet); 

        

    代码下载地址:

    http://www.devdiv.com/forum.php?mod=viewthread&tid=88504&pid=546599&page=1&extra=#pid546599

     /**
    * @author 张兴业
    * 邮箱:xy-zhang#163.com
    * android开发进阶群:278401545
    *
    */

  • 相关阅读:
    原型设计工具比较及实践
    原型设计工具比较及实践
    原型设计工具比较及实践
    原型设计工具比较及实践
    原型设计工具比较及实践
    原型设计工具比较及实践
    原型设计工具比较及实践
    软件工程基础大作业感想
    博客园里留下你的成长足迹
    软件工程培训—粗读《构建之法》
  • 原文地址:https://www.cnblogs.com/xyzlmn/p/3168168.html
Copyright © 2011-2022 走看看