透明度动画
设置动画的主要方法 :
Animation alphaAnimation= AnimationUtils.loadAnimation(this,R.anim.alpha);
view.startAnimation(alphaAnimation);
每个视图对象都有一个startAnimation方法,所以每个view都可以显示视图动画。
在资源文件中定义动画:
每个alpha标签都是一个AlphaAnimation类的对象
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.1"
android:duration="1000"
/>
</set>
源码解析:
进入Annimation类查看一下:
每个视图对象都要通过变换矩阵来映射到屏幕上,在这个applyTransformation()方法中,他的不同子类实现了他的不同方法。
protected void applyTransformation(float interpolatedTime, Transformation t) {
}
/**
* Convert the information in the description of a size to an actual
* dimension
*
* @param type One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
* Animation.RELATIVE_TO_PARENT.
* @param value The dimension associated with the type parameter
* @param size The size of the object being animated
* @param parentSize The size of the parent of the object being animated
* @return The dimension to use for the animation
*/
子类AlphaAnimation()重写方法applyTransformation(),
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
final float alpha = mFromAlpha;
t.setAlpha(alpha + ((mToAlpha - alpha) * interpolatedTime));
}
缩放动画
在资源文件中,每个scale标签都对应一个ScaleAnimation对象,ScaleAnimation为Animation的子类,能够控制视图在x轴和y轴的缩放程度。
缩放动画实现:
animation xml资源文件代码:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000">
<scale
android:fromXScale="1.0"
android:toXScale="2.0"
android:fromYScale="1.0"
android:toYScale="1.0"
/>
</set>
布局代码:
<TextView
android:id="@+id/viewScaleAnimation"
android:background="@color/colorPrimary"
android:textColor="@android:color/white"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scale"
android:onClick="onClick"
/>
Activity中的代码:
case R.id.viewScaleAnimation:
Animation scaleAnimation=AnimationUtils.loadAnimation(this,R.anim.scale);
view.startAnimation(scaleAnimation);
break;
缩放动画需要注意默认基准点在左上角,改变基准点可以改变缩放方向,也可以通过pivotX,pivotY来调整。百分数+p是相对于父控件来说的,纯百分数是相对于自身来说的。
位移动画
在资源文件中,每个translate标签都对应一个TranslateAnimation对象。
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fillAfter="true">
<translate
android:fromXDelta="0"
android:toXDelta="100%"
android:toYDelta="0" />
</set>
布局和activity中的代码和前面的类似,这里不贴了,后面附上源码地址。
旋转动画
在资源文件中,每个rotate标签都对应一个RotateAnimation对象。
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fillAfter="true">
<rotate
android:fromDegrees="0"
android:toDegrees="90"
/>
</set>
repeatCount可以设置旋转次数,当为infinitede的时侯无限循环,repeatMode的设置可以改变是原路返还还是回到起点返回,没有设置的话默认是restart,回到起始位置返回。
集合动画
在资源文件中,每个set标签都对应一个RotateAnimation对象。
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:duration="1000"
android:pivotX="50%"
android:pivotY="50%"
android:fromDegrees="0"
android:toDegrees="720"
/>
<translate
android:duration="1000"
android:startoffset="1000"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="100%"
android:toYDelta="0" />
</set>
如果不设置startoffset的值,那么旋转和平移是同时进行的。设置之后先旋转再平移。
插值器
用插值器来控制动画的速率
<View
android:id="@+id/viewAccelerate"
android:background="@color/colorPrimary"
android:layout_margin="8dp"
android:onClick="onClick"
android:layout_width="50dp"
android:layout_height="50dp"/>
<View
android:id="@+id/viewLinear"
android:alpha="0.5"
android:background="@color/colorPrimary"
android:layout_margin="8dp"
android:onClick="onClick"
android:layout_width="50dp"
android:layout_height="50dp"/>
case R.id.viewLinear:
case R.id.viewAccelerate:
View viewLinear=findViewById(R.id.viewLinear);
View viewAccerlerate=findViewById(R.id.viewAccelerate);
Animation animationLinear=AnimationUtils.loadAnimation(this,R.anim.trainlate);
Animation animationAccerlerate=AnimationUtils.loadAnimation(this,R.anim.trainlate);
animationLinear.setInterpolator(new LinearInterpolator());
animationAccerlerate.setInterpolator(new AccelerateInterpolator());
viewLinear.startAnimation(animationLinear);
viewAccerlerate.startAnimation(animationAccerlerate);
break;