当我们看到别人的界面都是充满了特技的时候,你有没有那么一丝冲动,想让自己的APP也能拥有这样的特技呢?那我来带大家来介绍一下安卓的一般动画Animation.
安卓提供的一般动画一共有四种:
AlphaAnimation 透明度
TranslateAnimation 位移
ScaleAnimation 缩放
RotateAnimation 旋转
1.二话不说,先上个布局文件先
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/targetTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#2b2b2b"
android:gravity="center"
android:padding="30dp"
android:text="This is a test!"
android:textColor="#fff" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<Button
android:id="@+id/rotateBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="旋转" />
<Button
android:id="@+id/translateBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="平移" />
<Button
android:id="@+id/alphaBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="透明" />
<Button
android:id="@+id/scaleBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="缩放" />
<Button
android:id="@+id/mixBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="混合" />
</LinearLayout>
</RelativeLayout>
2.先来实现一个最简单的透明动画
AlphaAnimation alphaAnimation;
alphaAnimation = new AlphaAnimation(1, 0.2f);
alphaAnimation.setDuration(1000);
targetTV = (TextView) findViewById(R.id.targetTV);
alphaBtn = (Button) findViewById(R.id.alphaBtn);
alphaBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
targetTV.startAnimation(alphaAnimation);
}
});
setDuration是设置一个持续的时间,我们使用的AlphaAnimation构造器第一个参数是初始化透明度,结束透明度.下面是Animation一些共用的方法:
setInterpolator(Interpolator i) //修饰动画效果的方法,安卓内置的有Interpolator:
AccelerateDecelerateInterpolator 在动画开始与结束的地方速率改变比较慢,在中间的时候加速
AccelerateInterpolator 在动画开始的地方速率改变比较慢,然后开始加速
AnticipateInterpolator 开始的时候向后然后向前甩
AnticipateOvershootInterpolator 开始的时候向后然后向前甩一定值后返回最后的值
BounceInterpolator 动画结束的时候弹起
CycleInterpolator 动画循环播放特定的次数,速率改变沿着正弦曲线
DecelerateInterpolator 在动画开始的地方快然后慢
LinearInterpolator 以常量速率改变
OvershootInterpolator 向前甩一定值后再回到原来位置
setRepeatCount(int count) //设置重复次数,如果是不停止的话就是Animation.INFINITE
setRepeatMode(int mode) //设置重复类型
Animation.RESTART(重新开始)
Animation.REVERSE(从动画结束回到动画起始)
setAnimationListener(AnimationListener listener) //设置动画的监听器,有开始,结束,重复三个状态的回调
setFillAfter(boolean flag) //是否把View定格在动画结束那一刻,例如旋转1次180度之后.false是则恢复原样,true则你懂的
其实对于RotateAnimation动画,我们更多使用它
RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue,int pivotYType, float pivotYValue)
这样的一个构造器.
一般写成(以自己的宽度50%和高度50%的点为中心点旋转)
rotateAnimation = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
还有一个AnimationSet就是把动画装在一起,然后一起施展黑魔法...
然后我们把动画全部用一遍,不是为了看看效果有多好,只是为了凑个整体...
3.MainActivity代码,布局还是原来的布局
package com.example.august.animator;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
TextView targetTV;
Button rotateBtn;
Button translateBtn;
Button scaleBtn;
Button alphaBtn;
Button mixBtn;
RotateAnimation rotateAnimation;
TranslateAnimation translateAnimation;
ScaleAnimation scaleAnimation;
AlphaAnimation alphaAnimation;
AnimationSet set;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
initViews();
rotateAnimation = new RotateAnimation(0, 180);
rotateAnimation.setDuration(1000);
translateAnimation = new TranslateAnimation(0, 0, 100, 100);
translateAnimation.setDuration(1000);
scaleAnimation = new ScaleAnimation(0, 300, 0, 300);
scaleAnimation.setDuration(1000);
alphaAnimation = new AlphaAnimation(1, 0.2f);
alphaAnimation.setDuration(1000);
alphaAnimation.setFillAfter(true);
set = new AnimationSet(true);
set.addAnimation(rotateAnimation);
set.addAnimation(translateAnimation);
set.addAnimation(scaleAnimation);
set.addAnimation(alphaAnimation);
}
private void initViews() {
targetTV = (TextView) findViewById(R.id.targetTV);
rotateBtn = (Button) findViewById(R.id.rotateBtn);
translateBtn = (Button) findViewById(R.id.translateBtn);
scaleBtn = (Button) findViewById(R.id.scaleBtn);
alphaBtn = (Button) findViewById(R.id.alphaBtn);
mixBtn = (Button) findViewById(R.id.mixBtn);
rotateBtn.setOnClickListener(this);
translateBtn.setOnClickListener(this);
scaleBtn.setOnClickListener(this);
alphaBtn.setOnClickListener(this);
mixBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.rotateBtn:
targetTV.startAnimation(rotateAnimation);
break;
case R.id.scaleBtn:
targetTV.startAnimation(scaleAnimation);
break;
case R.id.alphaBtn:
targetTV.startAnimation(alphaAnimation);
break;
case R.id.translateBtn:
targetTV.startAnimation(translateAnimation);
break;
case R.id.mixBtn:
targetTV.startAnimation(set);
break;
}
}
}