Android:tweenAnimation

简介:

tweenAnimation在代码中的定义和实现方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public  void  onClick(View v)
     {
         switch  (v.getId())
         {
             case  R.id.button1:
                 mImageView.startAnimation(AnimationUtils.loadAnimation( this , R.anim.alpha)); //点击按钮播放动画
                 break ;
             case  R.id.button2:
                 Animation animation = AnimationUtils.loadAnimation( this , R.anim.translate);
                 mImageView.startAnimation(animation);
                                                                                                      
                 animation.setAnimationListener( new  AnimationListener() //监听动画的方法
                 {
                     @Override
                     public  void  onAnimationStart(Animation animation)
                     {
                     }
                                                                                                          
                     @Override
                     public  void  onAnimationRepeat(Animation animation)
                     {
                     }
                                                                                                          
                     @Override
                     public  void  onAnimationEnd(Animation animation)
                     {
                         Toast.makeText(MainActivity. this "移动完成" , Toast.LENGTH_LONG).show();
                     }
                 });
                 break ;
         }
     }


1.透明度alpha:

1
2
3
4
5
6
<?xml version= "1.0"  encoding= "utf-8" ?>
<alpha xmlns:android= "http://schemas.android.com/apk/res/android"
     android:fromAlpha= "0.0"
     android:toAlpha= "1.0"
     android:duration= "5000" >
</alpha>


2.位移translate:

1
2
3
4
5
6
7
8
<?xml version= "1.0"  encoding= "utf-8" ?>
<translate xmlns:android= "http://schemas.android.com/apk/res/android"
     android:fromXDelta= "0"
     android:fromYDelta= "60"
     android:toXDelta= "0"
     android:toYDelta= "0"
     android:duration= "5000" >
</translate>


1
translate android:fromYDelta= "-100%p"



3.大小scale:

1
2
3
4
5
6
7
8
9
10
11
<?xml version= "1.0"  encoding= "utf-8" ?>
<scale xmlns:android= "http://schemas.android.com/apk/res/android"
     android:fromXScale= "0.01"
     android:fromYScale= "0.01"
     android:toXScale= "1"
     android:toYScale= "1"
     android:pivotX= "50%"
     android:pivotY= "50%"
     android:duration= "10000"
     >
</scale>


4.角度rotate

1
2
3
4
5
6
7
8
9
10
<?xml version= "1.0"  encoding= "utf-8" ?>
<rotate xmlns:android= "http://schemas.android.com/apk/res/android"
     android:fromDegrees= "-360"
     android:toDegrees= "0"
     android:pivotX= "90%"
     android:pivotY= "90%"
     android:repeatCount= "infinite"
     android:duration= "10000"
     >
</rotate>


5.set:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version= "1.0"  encoding= "utf-8" ?>
<set xmlns:android= "http://schemas.android.com/apk/res/android"  >
     <alpha
         android:duration= "3000"
         android:fromAlpha= "0.0"
         android:toAlpha= "1.0"  >
     </alpha>
     <scale
         android:duration= "3000"
         android:fromXScale= "0.1"
         android:fromYScale= "2"
         android:pivotX= "50%"
         android:pivotY= "50%"
         android:toXScale= "1"
         android:toYScale= "1"  />
</set>



6.代码写法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
private  void  initAnimation() {
         //透明度控制动画效果 alpha
         animation_alpha= new  AlphaAnimation( 0 .1f, 1 .0f);
         //第一个参数fromAlpha为 动画开始时候透明度
         //第二个参数toAlpha为 动画结束时候透明度
         animation_alpha.setRepeatCount(- 1 ); //设置循环
         animation_alpha.setDuration( 5000 ); //设置时间持续时间为 5000毫秒
         
         // 旋转效果rotate
         animation_rotate =  new  RotateAnimation( 0 , - 720 ,
                 RotateAnimation.RELATIVE_TO_SELF,  0 .5f,
                 RotateAnimation.RELATIVE_TO_SELF,  0 .5f);
           //第一个参数fromDegrees为动画起始时的旋转角度 //第二个参数toDegrees为动画旋转到的角度
           //第三个参数pivotXType为动画在X轴相对于物件位置类型 //第四个参数pivotXValue为动画相对于物件的X坐标的开始位置
           //第五个参数pivotXType为动画在Y轴相对于物件位置类型 //第六个参数pivotYValue为动画相对于物件的Y坐标的开始位置
         animation_rotate.setRepeatCount(- 1 );
         animation_rotate.setDuration( 5000 ); //设置时间持续时间为 5000毫秒
         
         //尺寸伸缩动画效果 scale
         animation_scale= new  ScaleAnimation( 0 .1f, 3 .0f, 0 .1f, 3 .0f,Animation.RELATIVE_TO_SELF, 0 .5f,Animation.RELATIVE_TO_SELF, 0 .5f);
         //第一个参数fromX为动画起始时 X坐标上的伸缩尺寸    
         //第二个参数toX为动画结束时 X坐标上的伸缩尺寸     
         //第三个参数fromY为动画起始时Y坐标上的伸缩尺寸    
         //第四个参数toY为动画结束时Y坐标上的伸缩尺寸  
         /*说明:
                             以上四种属性值    
               0.0表示收缩到没有 
               1.0表示正常无伸缩     
                            值小于1.0表示收缩  
                            值大于1.0表示放大
         */
         //第五个参数pivotXType为动画在X轴相对于物件位置类型  
         //第六个参数pivotXValue为动画相对于物件的X坐标的开始位置
         //第七个参数pivotXType为动画在Y轴相对于物件位置类型   
         //第八个参数pivotYValue为动画相对于物件的Y坐标的开始位置
         animation_scale.setRepeatCount(- 1 );
         animation_scale.setDuration( 5000 ); //设置时间持续时间为 5000毫秒
         
         //移动动画效果translate
         animation_translate= new  TranslateAnimation(-20f,300f,-20f,300f);
         //第一个参数fromXDelta为动画起始时 X坐标上的移动位置    
         //第二个参数toXDelta为动画结束时 X坐标上的移动位置      
         //第三个参数fromYDelta为动画起始时Y坐标上的移动位置 
         //第三个参数toYDelta为动画结束时Y坐标上的移动位置 
         animation_translate.setRepeatCount(- 1 ); //设置动画执行多少次,如果是-1的话就是一直重复
         animation_translate.setDuration( 5000 ); //设置时间持续时间为 5000毫秒
         
         animationSet= new  AnimationSet( true );
         
         animationSet.addAnimation(animation_alpha); //透明度
         animationSet.addAnimation(animation_rotate); //旋转
         animationSet.addAnimation(animation_scale); //尺寸伸缩
         animationSet.addAnimation(animation_translate); //移动
         image.startAnimation(animationSet); //开始播放
     }




=====================================================================

常用范例:

1.仿iphone控件抖动


左右抖动R.anim.shake_x代码:

1
2
3
4
5
6
<?xml version= "1.0"  encoding= "utf-8" ?>
<translate xmlns:android= "http://schemas.android.com/apk/res/android" 
android:fromXDelta= "0" 
android:toXDelta= "10" 
android:duration= "1000" 
android:interpolator= "@anim/cycle"  />


上下抖动R.anim.shake_y代码:

1
2
3
4
5
6
7
<?xml version= "1.0"  encoding= "utf-8" ?>
<translate xmlns:android= "http://schemas.android.com/apk/res/android"
     android:duration= "1000"
     android:fromYDelta= "0"
     android:interpolator= "@anim/cycle"
     android:toYDelta= "10"  >
</translate>


cycle.xml代码:

1
2
3
<?xml version= "1.0"  encoding= "utf-8" ?>
<cycleInterpolator xmlns:android= "http://schemas.android.com/apk/res/android"
     android:cycles= "20"  />



2.其中一个角边抖动

1
2
3
4
5
6
7
8
9
<?xml version= "1.0"  encoding= "utf-8" ?>
<rotate xmlns:android= "http://schemas.android.com/apk/res/android"
     android:duration= "180"
     android:fromDegrees= "-2"
     android:pivotX= "100%"
     android:pivotY= "100%"
     android:repeatCount= "infinite"
     android:repeatMode= "reverse"
     android:toDegrees= "2"  />








本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1225357,如需转载请自行联系原作者
目录
相关文章
|
10月前
|
存储 安全 物联网
Android:Android 应用权限详解
这篇文章为大家系统的梳理一下 Android 权限相关的知识,在日常开发中,我们都用过权限,但是对于权限的一些细节我们可能掌握的还不够全面,这篇文章会全面的为大家介绍权限相关的知识。
600 0
Android:Android 应用权限详解
|
XML Java Android开发
Android 中的 StateListDrawable
Android 中的 StateListDrawable
70 0
|
Java 测试技术 Android开发
Android LayoutAnimation不生效
Android LayoutAnimation不生效
105 0
|
Android开发
android 完美隐藏软键盘
android 完美隐藏软键盘
|
Android开发
Android书籍分享
在这里将我收集的一些书籍,与大家分享。 1、介绍Android 与GPhone的书籍 Introduction To Android (下载)Google官方文档,简要的介绍了Android的各个方面,值得大家看下 GPhone说明书 (下载)是每个开发人员必须阅读的,关于GPhone的使用、功能...
746 0
|
Android开发 数据格式 XML
|
Java Android开发 数据格式
Android Robolectric使用
Rebolectic 在src中有三个包,分别是:test、androidTest 和java test:是测试不涉及Activity,UI组件的纯Java方法。
1125 0
|
XML Java 开发工具
|
Android开发 数据格式 XML
|
Java 开发工具 Android开发
Android OpenCV配置不使用OpenCVManager
如果不会Android Studio中生成.so文件,请先查看Android JNI Windows配置 OpenCV官网下载Android版SDK 进入官网之后左下角有OpenCV 3.3蓝色字体,不一定是3.3, 这只是版本信息,点进去之后可以在最下方下载Android版的SDK。
1321 0

热门文章

最新文章