前言
之前做開發(fā)的時候也會用到動畫,用搜索引擎查詢基本也能滿足需求,但是為了進步,是時候總結梳理一波了,于是就有了下文!
效果圖

1.補間動畫的分類
1.AlphaAnimation(漸變)
2.TranslateAnimation(平移)
3.ScaleAnimation(縮放)
4.RotateAnimation(旋轉)
5.AnimationSet(組合)

2.了解Interpolator
用來控制動畫的變化速度,可以自行實現(xiàn)Interpolator接口來控制,也可以使用官方提供的接口來控制,這個我們一般都是在xml里面使用,屬性是:android:interpolator,具體看下面的xml代碼
LinearInterpolator:動畫以均勻的速度改變
AccelerateInterpolator:在動畫開始的地方改變速度較慢,然后開始加速
AccelerateDecelerateInterpolator:在動畫開始、結束的地方改變速度較慢,中間時加速
CycleInterpolator:動畫循環(huán)播放特定次數(shù),變化速度按正弦曲線改變: Math.sin(2 * mCycles * Math.PI * input)
DecelerateInterpolator:在動畫開始的地方改變速度較快,然后開始減速
AnticipateInterpolator:反向,先向相反方向改變一段再加速播放
AnticipateOvershootInterpolator:開始的時候向后然后向前甩一定值后返回最后的值
BounceInterpolator: 跳躍,快到目的值時值會跳躍,如目的值100,后面的值可能依次為85,77,72,83,90,100
OvershottInterpolator:回彈,最后超出目的值然后緩慢改變到目的值
3.實現(xiàn)代碼
首先在項目的res創(chuàng)建anim文件夾,然后就是創(chuàng)建各個動畫的xml文件(如圖)
-
第1步
1.png -
第2步
2.png
1.漸變alpha.xml
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:duration="3000"
android:fromAlpha="1.0"
android:toAlpha="0.1"
/>
<!--
duration:動畫時長
fromAlpha :起始透明度
toAlpha:結束透明度
透明度的范圍為:0-1,完全透明-完全不透明
-->
2.平移translate.xml
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXDelta="0"
android:toXDelta="300"
android:fromYDelta="0"
android:toYDelta="0"
android:duration="3000"/>
<!--
duration:動畫時長
fromXDelta/fromYDelta:動畫起始位置的X/Y坐標
toXDelta/toYDelta:動畫結束位置的X/Y坐標
-->
3.縮放scale.xml
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromXScale="0.5"
android:toXScale="1.5"
android:fromYScale="0.5"
android:toYScale="1.5"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000"/>
<!--
duration:動畫時長
fromXScale/fromYScale:沿著X軸/Y軸縮放的起始比例
toXScale/toYScale:沿著X軸/Y軸縮放的結束比例
pivotX/pivotY:縮放的中軸點X/Y坐標,即距離自身左邊緣的位置,比如50%就是以圖像的 中心為中軸點
-->
4.旋轉rotate.xml
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:duration="1000"
android:fromDegrees="0"
android:repeatCount="1"
android:repeatMode="reverse"
android:toDegrees="360" />
<!--
duration:動畫時長
fromDegrees/toDegrees:旋轉的起始/結束角度
repeatCount:旋轉的次數(shù),默認值為0,代表一次,假如是其他值,
比如3,則旋轉4次 另外,值為-1或者infinite時,表示動畫永不停止
repeatMode:設置重復模式,默認restart,但只有當repeatCount大于0或者infinite或-1時 才有效。
還可以設置成reverse,表示偶數(shù)次顯示動畫時會做方向相反的運動!
-->
5.組合set.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator"
android:shareInterpolator="true" >
<!-- 縮放-->
<scale
android:duration="2000"
android:fromXScale="0.5"
android:fromYScale="0.5"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.5"
android:toYScale="1.5" />
<!-- 旋轉-->
<rotate
android:duration="2000"
android:fromDegrees="0"
android:repeatCount="1"
android:repeatMode="reverse"
android:toDegrees="360" />
<!-- 平移-->
<translate
android:duration="2000"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="-300"
android:toYDelta="0" />
<!-- 漸變-->
<alpha
android:duration="2000"
android:fromAlpha="1.0"
android:toAlpha="0.1" />
</set>
TweenAnimation類的代碼如下:
package android.chm.com.animation.Activity;
import android.chm.com.animation.R;
import android.chm.com.animation.Utils.T;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
/**
* Created by admin-chen on 2017/4/13.
* 補間動畫
*/
public class TweenAnimation extends AppCompatActivity implements View.OnClickListener {
private static final String TAG="TweenAnimation";
ImageView img;
Button bt1, bt2, bt3, bt4,bt5;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tween);
initview();
}
private void initview() {
img = (ImageView) findViewById(R.id.img1);
bt1 = (Button) findViewById(R.id.bt1);
bt2 = (Button) findViewById(R.id.bt2);
bt3 = (Button) findViewById(R.id.bt3);
bt4 = (Button) findViewById(R.id.bt4);
bt5 = (Button) findViewById(R.id.bt5);
bt1.setOnClickListener(this);
bt2.setOnClickListener(this);
bt3.setOnClickListener(this);
bt4.setOnClickListener(this);
bt5.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.bt1:
mAnimation(img,R.anim.anim_alpha);//漸變
break;
case R.id.bt2:
mAnimation(img,R.anim.anim_translate);//平移
break;
case R.id.bt3:
mAnimation(img,R.anim.anim_scale);//縮放
break;
case R.id.bt4:
mAnimation(img,R.anim.anim_rotate);//旋轉
break;
case R.id.bt5:
mAnimation(img,R.anim.anim_set);//旋轉
break;
default:
break;
}
}
public void mAnimation(final ImageView img, int anim_type) {
Animation animation = AnimationUtils.loadAnimation(this,anim_type);
img.startAnimation(animation);
//動畫監(jiān)聽
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
//動畫開始
//T.showShort(TweenAnimation.this,"動畫開始");
}
@Override
public void onAnimationEnd(Animation animation) {
//動畫結束
// T.showShort(TweenAnimation.this,"動畫結束");
}
@Override
public void onAnimationRepeat(Animation animation) {
//動畫重復
T.showShort(TweenAnimation.this,"動畫重復");
}
});
}
}
4.擴展
1.上面的代碼對ImageView設置了動畫,當然也可以對TextView、Button等view控件都設置動畫
Animation animation = AnimationUtils.loadAnimation(this,R.anim.xxxx);
view.startAnimation(animation);
2.Fragment設置過渡動畫
在Fragment的Transition里調用方法fragmentTransaction.setCustomAnimations()來設置左邊的進入和退出,右邊的進入和退出動畫。
xml寫法
<!-- 平移有translationX和translationY平移-->
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:duration="@android:integer/config_mediumAnimTime"
android:interpolator="@android:interpolator/decelerate_quint"
android:propertyName="translationX"
android:valueFrom="0dp"
android:valueTo="-100dp"
android:valueType="floatType" />
<!-- 漸變-->
<objectAnimator
android:duration="@android:integer/config_mediumAnimTime"
android:interpolator="@android:interpolator/decelerate_quint"
android:propertyName="alpha"
android:valueFrom="1.0"
android:valueTo="0.0"
android:valueType="floatType" />
</set>
java代碼如下
protected void addFragmentToStack(int index){
DetailFragment detail = DetailFragment.newInstance(index);
FragmentTransaction ft = getFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.animator.xxx,
R.animator.xxx,
R.animator.xxx,
R.animator.xxx)//分別是左邊進入、退出和右邊進入、退出
ft.replace(R.id.details, detail);
ft.addToBackStack("detail");
ft.commit();
}
以上就是Tween動畫(補間動畫)的介紹,如果對你有所幫助,請點喜歡哦!

