安卓控件属性动画使用大全

来源:互联网 发布:unity3d 技能释放 编辑:程序博客网 时间:2024/06/07 06:20
首先写一个xml布局文件,用于显示效果,如下
<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>


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 家里井口处有很多鼻涕虫怎么办 花生地里有蜗牛怎么办 菜园里有好多虫怎么办 心里莫名的急该怎么办 老感觉心烦气燥怎么办 什么事都不想做怎么办心里烦躁 咳嗽流鼻涕身体发热怕冷怎么办 不感冒流清鼻涕怎么办 刚怀孕感冒了怎么办鼻塞流鼻涕 孕妇打喷嚏流鼻涕怎么办速效办法 宝宝流鼻涕一个月了怎么办 一个月婴儿感冒咳嗽流鼻涕怎么办 怎么办感冒能好得快些 一个月的宝宝流鼻涕怎么办 鼻炎犯了一直流鼻涕怎么办 宝宝流鼻涕鼻子擦破了怎么办 宝宝流鼻涕鼻子擦红了怎么办 上班忘记穿内衣了怎么办 早上上班忘记穿内衣了怎么办 高中知识都忘了怎么办 留鼻涕跟水一样怎么办 鼻涕像水一样流怎么办 肩膀很疼怎么办睡不着觉 夏天穿内衣出汗后很臭怎么办 脸过敏后严重缺水怎么办 过敏后脸上反复出现湿疹怎么办 孕妇脸上长湿疹过敏红肿怎么办 一岁宝宝一直流鼻涕怎么办 三岁宝宝一直流鼻涕怎么办 3岁宝宝一直流鼻涕不好怎么办 7岁半边鼻子不通气怎么办 感冒流鼻涕鼻子不通气怎么办 鼻子不通气干的怎么办 7个月婴儿流鼻涕怎么办 鼻子火辣辣的光想流鼻涕怎么办? 三岁宝宝鼻塞流鼻涕怎么办 一岁宝宝感冒流鼻涕鼻塞怎么办 1岁宝宝鼻塞流鼻涕怎么办 一岁多宝宝感冒鼻塞流鼻涕怎么办 宝宝鼻塞流鼻涕怎么办速效办法 八个月宝宝感冒流鼻涕鼻塞怎么办