zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

Android开发之视图动画基础

2023-09-14 08:56:49 时间

XML中 
alpha
渐变透明度动画效果

scale
渐变尺寸伸缩动画效果

translate
画面转换位置移动动画效果

rotate
画面转移旋转动画效果

JavaCode中 
AlphaAnimation
渐变透明度动画效果

ScaleAnimation
渐变尺寸伸缩动画效果

TranslateAnimation
画面转换位置移动动画效果

RotateAnimation
画面转移旋转动画效果


Android动画模式

Animation主要有两种动画模式:
一种是tweened animation(渐变动画) 
XML中
JavaCode

alpha
AlphaAnimation

scale
ScaleAnimation

一种是frame by frame(画面转换动画) 
XML中
JavaCode

translate
TranslateAnimation

rotate
RotateAnimation


如何在XML文件中定义动画

① 打开Eclipse,新建Android工程
② 在res目录中新建anim文件夹
③ 在anim目录中新建一个myanim.xml(注意文件名小写)
④ 加入XML的动画代码


           int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) 
//第一个参数fromX为动画起始时 X坐标上的伸缩尺寸    
//第二个参数toX为动画结束时 X坐标上的伸缩尺寸     
//第三个参数fromY为动画起始时Y坐标上的伸缩尺寸    
//第四个参数toY为动画结束时Y坐标上的伸缩尺寸  
/*说明:
                    以上四种属性值    
                    0.0表示收缩到没有 
                    1.0表示正常无伸缩     
                    值小于1.0表示收缩  
                    值大于1.0表示放大
*/
//第五个参数pivotXType为动画在X轴相对于物件位置类型  
//第六个参数pivotXValue为动画相对于物件的X坐标的开始位置
//第七个参数pivotXType为动画在Y轴相对于物件位置类型   
//第八个参数pivotYValue为动画相对于物件的Y坐标的开始位置
myAnimation_Scale =new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f,
             Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//设置动画持续时间
myAnimation_Scale.setDuration(700);
            int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
//第一个参数fromDegrees为动画起始时的旋转角度    
//第二个参数toDegrees为动画旋转到的角度   
//第三个参数pivotXType为动画在X轴相对于物件位置类型  
//第四个参数pivotXValue为动画相对于物件的X坐标的开始位置
//第五个参数pivotXType为动画在Y轴相对于物件位置类型   
//第六个参数pivotYValue为动画相对于物件的Y坐标的开始位置
myAnimation_Rotate=new RotateAnimation(0.0f, +350.0f,
               Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
myAnimation_Rotate.setDuration(3000);
如何使用Java代码中的动画效果
使用从View父类继承过来的方法startAnimation()来为View或是子类View等等添加一个动画效果

public void startAnimation (Animation animation)


【Android】视图绑定与网络编程webView 通过视图绑定viewBinding功能,您可以更轻松地编写可与视图交互的代码。在模块中启用视图绑定之后,系统会为该模块中的每个 XML 布局文件生成一个绑定类。绑定类的实例包含对在相应布局中具有 ID 的所有视图的直接引用。