#Android学习#Animation简单使用
来源:互联网 发布:mac dwg文件怎么打开 编辑:程序博客网 时间:2024/06/05 21:24
合理地使用动画效果,可以会用户带来更好的体验,Android也自带了很多的动画效果,如透明、移动、旋转、缩放,同时也能将这些动画效果混合起来使用。
Android使用动画效果可以有两种方式,一种是通过调用动画资源文件,另一种是直接在代码里编写。
我们通过上面提到的几种动画效果,来简单说下Animation的使用.
通过Android Studio新建一个工程,取名LearnAnimation,(PS. Android Studio让安卓开发变得更加简单,尤其是代码提示的功能,很赞,如果刚开始接触Android开发,推荐用Android Studio作为开发工具,个人觉得比Eclipse好用)
在布局文件里,增加几个按钮,点击的时候显示不同的动画效果
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.cyfloel.learnanimation.MainActivity" tools:showIn="@layout/activity_main"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnAlphaAnimation" android:layout_gravity="center_horizontal" android:text="透明"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnRotateRect" android:layout_gravity="center" android:text="旋转"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnTranslate" android:layout_gravity="center" android:text="移动"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnScaleAnimation" android:layout_gravity="center" android:text="缩放"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnAnimate" android:layout_gravity="center" android:text="混合动画"/></LinearLayout>
布局文件搞定后,我们就可以在MainActivity里添加Button的监听事件了,这里前面三个按钮的动画效果通过代码来实现,而后两个按钮的动画效果通过资源文件来实现,我们先看看通过代码是如何实现动画效果的
// MainActivity onCreate函数 // 透明动画效果 findViewById(R.id.btnAlphaAnimation).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { alphaAnimation = new AlphaAnimation(0, 1); alphaAnimation.setDuration(1000); v.startAnimation(alphaAnimation); } }); // 旋转动画效果 findViewById(R.id.btnRotateRect).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 以自身为中心旋转360度 // 通过使用Animation.RELATIVE_TO_SELF,0.5f来控制中心点 rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); rotateAnimation.setDuration(1000); v.startAnimation(rotateAnimation); } }); // 移动动画效果 findViewById(R.id.btnTranslate).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {// translateAnimation = new TranslateAnimation(0,200,0,200);// translateAnimation.setDuration(1000);// v.startAnimation(translateAnimation); v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate_animation)); } });
上面的是通过代码的方式来实现,如果是通过资源文件的方式,我们需要新建名为anim的资源文件夹,然后再建立相关动画效果的资源文件,如缩放效果
<!-- 缩放效果 scale_animation.xml--><?xml version="1.0" encoding="utf-8"?><scale xmlns:android="http://schemas.android.com/apk/res/android" android:fromXScale="0" android:toXScale="1" android:fromYScale="0" android:toYScale="1" android:pivotX="50%" android:pivotY="50%" android:duration="1000"></scale>
<!-- 混合动画效果 mix_animation.xml--><?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000" android:shareInterpolator="true"> <alpha android:fromAlpha="0" android:toAlpha="1" /> <scale android:fromXScale="0" android:fromYScale="0" android:pivotX="50%" android:pivotY="50%" android:toXScale="1" android:toYScale="1" /> <rotate android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:toDegrees="360" /></set>
然后在MainActivity的onCreate函数里,找到点击触发该效果的Button,并添加监听事件。
混合动画效果,实际就是将单个的动画效果放到set
标签下,同时设置shareInterpolato
r属性为true
// MainActivity onCreate// 缩放动画效果 findViewById(R.id.btnScaleAnimation).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale_animation)); } }); findViewById(R.id.btnAnimate).setOnClickListener(new View.OnClickListener() { @Override public void onClick( View v) { //v.startAnimation(animationSet); // 调用资源文件方法 Animation a = AnimationUtils.loadAnimation(MainActivity.this, R.anim.mix_animation); // 添加动画监听事件 a.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { Snackbar.make(findViewById(R.id.container),"混合效果",Snackbar.LENGTH_SHORT).show(); } @Override public void onAnimationRepeat(Animation animation) { } }); v.startAnimation(a); } });
在混合动画效果里,还增加了对动画效果的监听事件,当点击该按钮时,动画显示完毕后,显示消息提示。
注意,如果是通过代码的方式来实现混合动画效果的,则是通过AnimationSet来控制
animationSet = new AnimationSet(true); animationSet.setDuration(1000); AlphaAnimation aa = new AlphaAnimation(0,1); aa.setDuration(1000); animationSet.addAnimation(aa); TranslateAnimation ta = new TranslateAnimation(200,0,200,0); ta.setDuration(1000); animationSet.addAnimation(ta); // 在startAnimation的时候,将animationSet作为参数传入 v.startAnimation(animationSet);
- #Android学习#Animation简单使用
- Android:Animation的简单学习
- android animation的简单使用
- Android学习之笔记---Animation的使用
- android animation 简单介绍
- android 不使用xml的 Animation 简单例子
- android 动画-Tween Animation(二) 动画的简单使用
- Android Animation学习笔记
- Android Animation学习笔记
- Android Animation学习笔记
- Android Animation学习笔记
- Android Animation学习笔记
- Android Animation学习笔记
- Android Animation学习总结
- Android Animation学习笔记
- Android-------------Animation学习笔记
- android Animation学习
- Android Animation学习笔记
- 从今天开始学习写技术博客
- UITabBarItem UIBarButtonItem改变title颜色
- cximage之tiff格式
- VS2008配色问题
- java第八天
- #Android学习#Animation简单使用
- HDOJ-1521 排列组合(母函数)
- 文章标题
- 设计模式C++学习笔记之七(AbstractFactory抽象工厂模式)
- fastjson转换json格式数据为List<HashMap>转换异常问题
- 2014蓝桥杯 小朋友排队
- 【Scala类型系统】自身类型(self type)引用
- VLC搭建RTSP服务器的过程 -测试通过
- struts2---struts.xml文件头