<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);
//旋转动画 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>
<?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>
<?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>
<?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>
<?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>
<?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>