安卓控件属性动画使用大全
2015-10-13 15:57
405 查看
首先写一个xml布局文件,用于显示效果,如下
然后在Activity中抓取所有的按钮
1、透明度动画
在Activity中写法1:
直接通过对象进行设置
通过xml文件设置
按照把动画都写在xml中比较清晰可靠,下面的例子全是依据xml的写法
下面是xml文件的写法
1、透明度渐变
2、旋转
3、大小变化
4、平移
如果想要加入插补效果,比如平移到头超出一块,再弹回来,可以这样写
5、多重属性变化
6、帧动画,注意帧动画要用到imgView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.dialogtest.AnimationActivity" > <Button android:id="@+id/alpha" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="透明度按钮" /> <Button android:id="@+id/rotate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="旋转按钮" /> <Button android:id="@+id/translate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="平移按钮" /> <Button android:id="@+id/scale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="大小按钮" /> <Button android:id="@+id/set" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="多重动画按钮" /> <Button android:id="@+id/prop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="属性动画按钮" /> <ImageView android:id="@+id/imgList" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/animation_list" /> </LinearLayout>
然后在Activity中抓取所有的按钮
Button alphaButton = (Button) findViewById(R.id.alpha); Button rotateButton = (Button) findViewById(R.id.rotate); Button transButton = (Button) findViewById(R.id.translate); Button scaleButton = (Button) findViewById(R.id.scale); Button setButton =(Button) findViewById(R.id.set); Button propButton = (Button) findViewById(R.id.prop);
1、透明度动画
在Activity中写法1:
直接通过对象进行设置
Animation alpha = new AlphaAnimation(0,1); alpha.setDuration(5000); alphaButton.setAnimation(alpha);写法2:
通过xml文件设置
Animation alpha2 = AnimationUtils.loadAnimation(this,R.anim.animation_alpha); alphaButton.setAnimation(alpha2);
按照把动画都写在xml中比较清晰可靠,下面的例子全是依据xml的写法
//旋转动画 Animation rotate = AnimationUtils.loadAnimation(this, R.anim.animation_rotate); rotateButton.setAnimation(rotate); //平移动画 Animation translate = AnimationUtils.loadAnimation(this, R.anim.animation_translate); transButton.setAnimation(translate); //大小动画 Animation scale = AnimationUtils.loadAnimation(this, R.anim.animation_scale); scaleButton.setAnimation(scale); //多重动画 Animation set = AnimationUtils.loadAnimation(this, R.anim.animation_set); setButton.setAnimation(set); //逐帧动画 ImageView imgList = (ImageView) findViewById(R.id.imgList); AnimationDrawable animationDrawable = (AnimationDrawable) imgList.getDrawable(); animationDrawable.start(); //属性动画 ObjectAnimator oa=ObjectAnimator.ofFloat(propButton, "rotationX", 0.0f, 360f); oa.setDuration(5000); oa.start();
下面是xml文件的写法
1、透明度渐变
<?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:fromAlpha="0" android:toAlpha="1" android:duration="5000"> </alpha>
2、旋转
<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0" android:toDegrees="720" android:duration="5000"> </rotate>
3、大小变化
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:fromXScale="0" android:toXScale="200" android:fromYScale="0" android:toYScale="200" android:duration="5000" > </scale>
4、平移
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="0" android:toXDelta="500" android:duration="5000"> </translate>
如果想要加入插补效果,比如平移到头超出一块,再弹回来,可以这样写
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="0" android:toXDelta="500" android:duration="5000" android:interpolator="@android:anim/overshoot_interpolator"> </translate>
5、多重属性变化
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="5000"> <alpha android:fromAlpha="0" android:toAlpha="1"></alpha> <translate android:fromXDelta="0" android:toXDelta="200"></translate> </set>
6、帧动画,注意帧动画要用到imgView
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@drawable/btn_qq_nor" android:duration="150"></item> <item android:drawable="@drawable/btn_qq_press" android:duration="150"></item> <item android:drawable="@drawable/btn_sina_nor" android:duration="150"></item> <item android:drawable="@drawable/btn_sina_press" android:duration="150"></item> <item android:drawable="@drawable/btn_weichat_nor" android:duration="150"></item> <item android:drawable="@drawable/btn_weichat_press" android:duration="150"></item> <item android:drawable="@drawable/btn_tenteweibo_nor" android:duration="150"></item> <item android:drawable="@drawable/btn_tenteweibo_press" android:duration="150"></item> </animation-list>
相关文章推荐
- delphi正则表达式学习笔记(一)
- 团队开发共用一个开发者账号
- Python中的并发编程
- 杂谈:用 Sublime Text 2 写 ActionScript3
- cucumber java从入门到精通(1)初体验
- json字符串转换
- vs2010+win7 64bit OpenGL程序无法启动此应用程序,因为计算机中丢失glut32.dll
- vs2010+win7 64bit OpenGL程序无法启动此应用程序,因为计算机中丢失glut32.dll
- jQuery-autoComplete实例
- jQuery-autoComplete实例
- jQuery-autoComplete实例
- 中缀表达式转换为前缀及后缀表达式并求值【摘】
- Basics sqrt函数
- 浅谈TCP优化
- 基于ELK Stack和Spark Streaming的日志处理平台设计与实现
- IE 无法正常打开,刚启动就关闭
- ios- block循环引用问题
- git 查看 本地仓库的commit记录
- String 转json方法
- 使用VS2013调试FluorineFx程序