此demo实现了多种 android 渐变动画,主要包括:渐隐,旋转,缩放,移动和这些动画的综合效果。
demo:下载地址
源码:
package com.bobo.study.study_2_4;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.CycleInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity implements View.OnClickListener {
Button scaleBut,alphaBut,translateBut,rotateBut,animsBut;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView=(ImageView)findViewById(R.id.imageView);
scaleBut=(Button)findViewById(R.id.scaleBut);
scaleBut.setOnClickListener(this);
rotateBut=(Button)findViewById(R.id.rotateBut);
rotateBut.setOnClickListener(this);
translateBut=(Button)findViewById(R.id.translateBut);
translateBut.setOnClickListener(this);
alphaBut=(Button)findViewById(R.id.alphaBut);
alphaBut.setOnClickListener(this);
animsBut=(Button)findViewById(R.id.animsBut);
animsBut.setOnClickListener(this);
}
@Override
public void onClick(View v) {
//1,创建AnimationSet对象(可以包含多个Animation对象,是Animation的子类)
//可以当作animation的集合,能用来设置animation们的一些公共属性
//参数true:可以为animationSet设置Interpolator,否则只能分别分别为animation设置Interpolator
AnimationSet animationSet=new AnimationSet(true);
//设置动画简便的速率变化(AccelerateInterpolator逐渐加速)
//DecelerateInterpolator:逐渐减速
//AccelerateDecelerateInterpolator:中间快,两头慢
//LinearInterpolator:匀速
//CycleInterpolator(n):动画连续播放n次,速率沿着正弦曲线变化
animationSet.setInterpolator(new CycleInterpolator(3));
//2,创建Animation对象
Animation animation=null;
if (v == alphaBut) {
System.out.println("alpha淡入淡出");
//3,为Animation对象设置属性
animation=new AlphaAnimation(0,1);//(从完全透明0到不透明1)
} else if (v == rotateBut) {
System.out.println("rotate旋转");
animation=new RotateAnimation(0,360,//从0度到360度
//下面几个参数是用来设置旋转中心点的座标
Animation.RELATIVE_TO_PARENT, 0.5f,//横座标类型(相对父控件的),0.5个父控件X轴长
Animation.RELATIVE_TO_PARENT,0.5f);//纵座标类型(相对父控件的),0.5个父控件Y轴长
} else if (v == scaleBut) {
System.out.println("scale缩放");
animation=new ScaleAnimation(1,0.2f,1,0.2f,//X轴从1缩放到0.2,Y轴从1缩放到0.2(相对自身)
//下面是缩放中心点的座标
Animation.RELATIVE_TO_SELF, 1.0f,
Animation.RELATIVE_TO_SELF,1.0f);
} else if (v == translateBut) {
System.out.println("translate移动");
animation=new TranslateAnimation(
Animation.RELATIVE_TO_SELF,0f,//X轴开始位置
Animation.RELATIVE_TO_SELF,1f,//X轴结束位置
Animation.RELATIVE_TO_SELF,0f,//Y轴开始位置
Animation.RELATIVE_TO_SELF,1f//Y轴结束位置
);
}else if(v==animsBut){
System.out.println("多animation综合效果");
animation=new AlphaAnimation(0,1);
animation.setDuration(2000);
animation.setRepeatCount(1);
animationSet.addAnimation(animation);
//animationSet可以添加多个animation来实现综合效果
animation=new RotateAnimation(0,360,
Animation.RELATIVE_TO_PARENT, 0.5f,
Animation.RELATIVE_TO_PARENT,0.5f);
}
animation.setDuration(2000);//动画执行时间
animation.setRepeatCount(1);//动画重复执行的次数(重复一次,共两次)
//注意animationSet设置的属性会应用到其内所有animation上
animationSet.setFillAfter(true);//为true,动画结束后停留在结束的状态
animationSet.setFillBefore(false);//为true,动画结束后回到最初的状态
animationSet.setStartOffset(1000);//动画开始前的等待时间
//4,为animationSet添加animation
animationSet.addAnimation(animation);
//5,通过控件执行animationSet
imageView.startAnimation(animationSet);
//动画效果也可以提前写好:1,在res创建anim文件夹,里面创建各种anim效果xml文件
//2,动画效果的xml文件格式参考例子中的
//3,用AnimationUtils.loadAnimation()方法调用这些xml文件
/* Animation animation=null;
if (v == alphaBut) {
System.out.println("alpha");
animation= AnimationUtils.loadAnimation(this, R.anim.alpha);
} else if (v == rotateBut) {
System.out.println("translate");
animation=AnimationUtils.loadAnimation(this,R.anim.rotate);
} else if (v == scaleBut) {
System.out.println("scale");
animation=AnimationUtils.loadAnimation(this,R.anim.scale);
} else if (v == translateBut) {
animation = AnimationUtils.loadAnimation(this, R.anim.translate);
System.out.println("rotate");
} else if (v == animsBut) {
animation = AnimationUtils.loadAnimation(this, R.anim.anims);
System.out.println("anims");
}
imageView.startAnimation(animation);*/
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
anim效果xml文件(所有):
<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<translate
android:fromXDelta="50%"
android:fromYDelta="50%"
android:toXDelta="100%"
android:toYDelta="100%"
android:duration="2000"/>
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<!--pivotX="180"是绝对定位,"50%"是相对自身定位,"50%p"是相对父控件定位-->
<scale
android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale="0.0"
android:toYScale="0.0"
android:pivotX="180"
android:pivotY="180"
android:duration="2000"/>
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<!--pivotX="180"是绝对定位,"50%"是相对自身定位,"50%p"是相对父控件定位-->
<rotate
android:fromDegrees="0"
android:toDegrees="-360"
android:pivotX="180"
android:pivotY="180"
android:duration="2000"/>
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="2000"/>
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:shareInterpolator="false">
<!--如果上面的shareInterpolator为true,就不用在下面分别为每个动画效果添加interpolator-->
<alpha
android:interpolator="@android:anim/decelerate_interpolator"
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="2000"/>
<!--pivotX="180"是绝对定位,"50%"是相对自身定位,"50%p"是相对父控件定位-->
<rotate
android:interpolator="@android:anim/accelerate_interpolator"
android:fromDegrees="0"
android:toDegrees="-360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000"/>
</set>
运行截图: