




已阅读5页,还剩22页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Animations一、Animations介绍Animations是一个实现android UI界面动画效果的API,Animations提供了一系列的动画效果,可以进行旋转、缩放、淡入淡出等,这些效果可以应用在绝大多数的控件中。二、Animations的分类Animations从总体上可以分为两大类:1.Tweened Animations:该类Animations提供了旋转、移动、伸展和淡出等效果。Alpha淡入淡出,Scale缩放效果,Rotate旋转,Translate移动效果。2.Frame-by-frame Animations:这一类Animations可以创建一个Drawable序列,这些Drawable可以按照指定的时间间歇一个一个的显示。三、Animations的使用方法(代码中使用)Animations extends Object implements Cloneable使用TweenedAnimations的步骤:1.创建一个AnimationSet对象(Animation子类);2.增加需要创建相应的Animation对象;3.更加项目的需求,为Animation对象设置相应的数据;4.将Animatin对象添加到AnimationSet对象当中;5.使用控件对象开始执行AnimationSet。Tweened Animations的分类、Alpha:淡入淡出效果、Scale:缩放效果、Rotate:旋转效果、Translate:移动效果Animation的四个子类:AlphaAnimation、TranslateAnimation、ScaleAnimation、RotateAnimation四、具体实现1、main.xml 2、.java文件importandroid.app.Activity;importandroid.os.Bundle;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.view.animation.AlphaAnimation;importandroid.view.animation.Animation;importandroid.view.animation.AnimationSet;importandroid.view.animation.RotateAnimation;importandroid.view.animation.ScaleAnimation;importandroid.view.animation.TranslateAnimation;importandroid.widget.Button;importandroid.widget.ImageView;publicclassAnimation1ActivityextendsActivity privateButtonrotateButton=null;privateButtonscaleButton=null;privateButtonalphaButton=null;privateButtontranslateButton=null;privateImageViewimage=null;OverridepublicvoidonCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);setContentView(R.layout.main);rotateButton= (Button)findViewById(R.id.rotateButton);scaleButton= (Button)findViewById(R.id.scaleButton);alphaButton= (Button)findViewById(R.id.alphaButton);translateButton= (Button)findViewById(R.id.translateButton);image= (ImageView)findViewById(R.id.image);rotateButton.setOnClickListener(newRotateButtonListener();scaleButton.setOnClickListener(newScaleButtonListener();alphaButton.setOnClickListener(newAlphaButtonListener();translateButton.setOnClickListener(newTranslateButtonListener();classAlphaButtonListenerimplementsOnClickListenerpublicvoidonClick(View v) /创建一个AnimationSet对象,参数为Boolean型,/true表示使用Animation的interpolator,false则是使用自己的AnimationSet animationSet =newAnimationSet(true);/创建一个AlphaAnimation对象,参数从完全的透明度,到完全的不透明AlphaAnimation alphaAnimation =newAlphaAnimation(1, 0);/设置动画执行的时间alphaAnimation.setDuration(500);/将alphaAnimation对象添加到AnimationSet当中animationSet.addAnimation(alphaAnimation);/使用ImageView的startAnimation方法执行动画image.startAnimation(animationSet);classRotateButtonListenerimplementsOnClickListenerpublicvoidonClick(View v) AnimationSet animationSet =newAnimationSet(true);/参数1:从哪个旋转角度开始/参数2:转到什么角度/后4个参数用于设置围绕着旋转的圆的圆心在哪里/参数3:确定x轴坐标的类型,有ABSOLUT绝对坐标、RELATIVE_TO_SELF相对于自身坐标、RELATIVE_TO_PARENT相对于父控件的坐标/参数4:x轴的值,0.5f表明是以自身这个控件的一半长度为x轴/参数5:确定y轴坐标的类型/参数6:y轴的值,0.5f表明是以自身这个控件的一半长度为x轴RotateAnimation rotateAnimation =newRotateAnimation(0, 360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);rotateAnimation.setDuration(1000);animationSet.addAnimation(rotateAnimation);image.startAnimation(animationSet);classScaleButtonListenerimplementsOnClickListenerpublicvoidonClick(View v) AnimationSet animationSet =newAnimationSet(true);/参数1:x轴的初始值/参数2:x轴收缩后的值/参数3:y轴的初始值/参数4:y轴收缩后的值/参数5:确定x轴坐标的类型/参数6:x轴的值,0.5f表明是以自身这个控件的一半长度为x轴/参数7:确定y轴坐标的类型/参数8:y轴的值,0.5f表明是以自身这个控件的一半长度为x轴ScaleAnimation scaleAnimation =newScaleAnimation(0, 0.1f,0,0.1f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);scaleAnimation.setDuration(1000);animationSet.addAnimation(scaleAnimation);image.startAnimation(animationSet);classTranslateButtonListenerimplementsOnClickListenerpublicvoidonClick(View v) AnimationSet animationSet =newAnimationSet(true);/参数12:x轴的开始位置/参数34:y轴的开始位置/参数56:x轴的结束位置/参数78:x轴的结束位置TranslateAnimationtranslateAnimation =newTranslateAnimation(Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0.5f);translateAnimation.setDuration(1000);animationSet.addAnimation(translateAnimation);image.startAnimation(animationSet);Tween Animations的通用方法、setDuration(long durationMills)设置动画持续时间(单位:毫秒)、setFillAfter(Boolean fillAfter)如果fillAfter的值为true,则动画执行后,控件将停留在执行结束的状态、setFillBefore(Boolean fillBefore)如果fillBefore的值为true,则动画执行后,控件将回到动画执行之前的状态、setStartOffSet(long startOffSet)设置动画执行之前的等待时间、setRepeatCount(int repeatCount)设置动画重复执行的次数在代码中使用Animations可以很方便的调试、运行,但是代码的可重用性差,重复代码多。同样可以在xml文件中配置Animations,这样做可维护性变高了,只不过不容易进行调试。一、在xml中使用Animations步骤1.在res文件夹下建立一个anim文件夹;2.创建xml文件,并首先加入set标签,更改标签如下:3.在该标签当中加入rotate,alpha,scale或者translate标签;4.在代码当中使用AnimationUtils当中装载xml文件,并生成Animation对象。因为Animation是AnimationSet的子类,所以向上转型,用Animation对象接收。Animation animation = AnimationUtils.loadAnimation(Animation1Activity.this, R.anim.alpha);/启动动画image.startAnimation(animation);二、具体实现1、alpha.xml2、rotate.xml3、scale.xml4、translate.xml5、.java文件importandroid.app.Activity;importandroid.os.Bundle;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.view.animation.Animation;importandroid.view.animation.AnimationUtils;importandroid.widget.Button;importandroid.widget.ImageView;publicclassAnimation1ActivityextendsActivity privateButtonrotateButton=null;privateButtonscaleButton=null;privateButtonalphaButton=null;privateButtontranslateButton=null;privateImageViewimage=null;OverridepublicvoidonCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);setContentView(R.layout.main);rotateButton= (Button) findViewById(R.id.rotateButton);scaleButton= (Button) findViewById(R.id.scaleButton);alphaButton= (Button) findViewById(R.id.alphaButton);translateButton= (Button) findViewById(R.id.translateButton);image= (ImageView) findViewById(R.id.image);rotateButton.setOnClickListener(newRotateButtonListener();scaleButton.setOnClickListener(newScaleButtonListener();alphaButton.setOnClickListener(newAlphaButtonListener();translateButton.setOnClickListener(newTranslateButtonListener();classAlphaButtonListenerimplementsOnClickListener publicvoidonClick(View v) /使用AnimationUtils装载动画配置文件Animation animation = AnimationUtils.loadAnimation(Animation1Activity.this, R.anim.alpha);/启动动画image.startAnimation(animation);classRotateButtonListenerimplementsOnClickListener publicvoidonClick(View v) Animation animation = AnimationUtils.loadAnimation(Animation1Activity.this, R.anim.rotate);image.startAnimation(animation);classScaleButtonListenerimplementsOnClickListener publicvoidonClick(View v) Animation animation = AnimationUtils.loadAnimation(Animation1Activity.this, R.anim.scale);image.startAnimation(animation);classTranslateButtonListenerimplementsOnClickListener publicvoidonClick(View v) Animation animation = AnimationUtils.loadAnimation(Animation1Activity.this, R.anim.translate);image.startAnimation(animation);AnimationSet的具体使用方法1.AnimationSet是Animation的子类;2.一个AnimationSet包含了一系列的Animation;3.针对AnimationSet设置一些Animation的常见属性(如startOffset,duration等),可以被包含在AnimationSet当中的Animation集成;例:一个AnimationSet中有两个Animation,效果叠加第一种方法:doubleani.xml.java文件中classDoubleButtonListenerimplementsOnClickListener publicvoidonClick(View v) /使用AnimationUtils装载动画配置文件Animationanimation = AnimationUtils.loadAnimation(Animation2Activity.this, R.anim.doubleani);/启动动画image.startAnimation(animation);第二种方法:.java文件中classDoubleButtonListenerimplementsOnClickListener publicvoidonClick(View v) AnimationSet animationSet =newAnimationSet(true);AlphaAnimation alphaAnimation =newAlphaAnimation(1, 0);RotateAnimationrotateAnimation =newRotateAnimation(0, 360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);rotateAnimation.setDuration(1000);animationSet.addAnimation(rotateAnimation);animationSet.addAnimation(alphaAnimation);image.startAnimation(animationSet);Interpolator的具体使用方法Interpolator定义了动画变化的速率,在Animations框架当中定义了一下几种Interpolator?AccelerateDecelerateInterpolator:在动画开始与结束的地方速率改变比较慢,在中间的时候速率快。?AccelerateInterpolator:在动画开始的地方速率改变比较慢,然后开始加速?CycleInterpolator:动画循环播放特定的次数,速率改变沿着正弦曲线?DecelerateInterpolator:在动画开始的地方速率改变比较慢,然后开始减速?LinearInterpolator:动画以均匀的速率改变分为以下几种情况:1、在set标签中2、如果在一个set标签中包含多个动画效果,如果想让这些动画效果共享一个Interpolator。android:shareInterpolator=true3、如果不想共享一个interpolator,则设置android:shareInterpolator=true,并且需要在每一个动画效果处添加interpolator。4、如果是在代码上设置共享一个interpolator,则可以在AnimationSet设置interpolator。AnimationSet animationSet =newAnimationSet(true);animationSet.setInterpolator(newAccelerateInterpolator();5、如果不设置共享一个interpolator则可以在每一个Animation对象上面设置interpolator。AnimationSet animationSet =newAnimationSet(false);alphaAnimation.setInterpolator(newAccelerateInterpolator();rotateAnimation.setInterpolator(newDecelerateInterpolator();Frame-By-Frame Animations的使用方法Frame-By-Frame Animations是一帧一帧的格式显示动画效果。类似于电影胶片拍摄的手法。main.xml3、anim.xml4、.java文件importandroid.app.Activity;importandroid.graphics.drawable.AnimationDrawable;importandroid.os.Bundle;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.widget.Button;importandroid.widget.ImageView;publicclassAnimationsActivityextendsActivity privateButtonbutton=null;privateImageViewimageView=null;OverridepublicvoidonCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);setContentView(R.layout.main);button= (Button)findViewById(R.id.
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 软件开发流程面临的挑战试题及答案
- 企业文化与风险管理考题及答案
- 制定职业晋升的长期规划计划
- 2024年甘肃陇南事业单位招聘笔试真题
- VB最佳编程习惯与技巧试题及答案
- 2024年东莞市市场监督管理局招聘笔试真题
- 移动设备安全性测试试题及答案
- 软件工程项目管理中的挑战试题及答案
- 未来市场竞争中的风险识别试题及答案
- 自然语言处理技术试题及答案
- 人工智能标准化白皮书
- 2021译林版高中英语选择性必修一课文翻译
- 0720小罐茶品牌介绍
- 二级、三级电箱接线图
- 2022年食品卫生通则第三版(中文版)
- 颈椎功能障碍指数,Neck Disabilitv Index,NDI
- 名著导读《红楼梦》PPT课件(完整版)
- 吉林省办学基本标准手册
- 4车道高速公路30米预应力混凝土简支T梁桥上部结构设计_论文
- 2020年广东省中考物理试卷分析
- 脱氨蒸氨工段操作规程
评论
0/150
提交评论