本節(jié)帶來(lái)的是Android三種動(dòng)畫(huà)中的第二種——補(bǔ)間動(dòng)畫(huà)(Tween),和前面學(xué)的幀動(dòng)畫(huà)不同,幀動(dòng)畫(huà) 是通過(guò)連續(xù)播放圖片來(lái)模擬動(dòng)畫(huà)效果,而補(bǔ)間動(dòng)畫(huà)開(kāi)發(fā)者只需指定動(dòng)畫(huà)開(kāi)始,以及動(dòng)畫(huà)結(jié)束"關(guān)鍵幀", 而動(dòng)畫(huà)變化的"中間幀"則由系統(tǒng)計(jì)算并補(bǔ)齊!好了,開(kāi)始本節(jié)學(xué)習(xí)~
Andoird所支持的補(bǔ)間動(dòng)畫(huà)效果有如下這五種,或者說(shuō)四種吧,第五種是前面幾種的組合而已~
- AlphaAnimation:透明度漸變效果,創(chuàng)建時(shí)許指定開(kāi)始以及結(jié)束透明度,還有動(dòng)畫(huà)的持續(xù) 時(shí)間,透明度的變化范圍(0,1),0是完全透明,1是完全不透明;對(duì)應(yīng)標(biāo)簽!
- ScaleAnimation:縮放漸變效果,創(chuàng)建時(shí)需指定開(kāi)始以及結(jié)束的縮放比,以及縮放參考點(diǎn), 還有動(dòng)畫(huà)的持續(xù)時(shí)間;對(duì)應(yīng)標(biāo)簽!
- TranslateAnimation:位移漸變效果,創(chuàng)建時(shí)指定起始以及結(jié)束位置,并指定動(dòng)畫(huà)的持續(xù) 時(shí)間即可;對(duì)應(yīng)標(biāo)簽!
- RotateAnimation:旋轉(zhuǎn)漸變效果,創(chuàng)建時(shí)指定動(dòng)畫(huà)起始以及結(jié)束的旋轉(zhuǎn)角度,以及動(dòng)畫(huà) 持續(xù)時(shí)間和旋轉(zhuǎn)的軸心;對(duì)應(yīng)標(biāo)簽
- AnimationSet:組合漸變,就是前面多種漸變的組合,對(duì)應(yīng)標(biāo)簽
在開(kāi)始講解各種動(dòng)畫(huà)的用法之前,我們先要來(lái)講解一個(gè)東西:Interpolator
用來(lái)控制動(dòng)畫(huà)的變化速度,可以理解成動(dòng)畫(huà)渲染器,當(dāng)然我們也可以自己實(shí)現(xiàn)Interpolator 接口,自行來(lái)控制動(dòng)畫(huà)的變化速度,而Android中已經(jīng)為我們提供了五個(gè)可供選擇的實(shí)現(xiàn)類:
- LinearInterpolator:動(dòng)畫(huà)以均勻的速度改變
- AccelerateInterpolator:在動(dòng)畫(huà)開(kāi)始的地方改變速度較慢,然后開(kāi)始加速
- AccelerateDecelerateInterpolator:在動(dòng)畫(huà)開(kāi)始、結(jié)束的地方改變速度較慢,中間時(shí)加速
- CycleInterpolator:動(dòng)畫(huà)循環(huán)播放特定次數(shù),變化速度按正弦曲線改變: Math.sin(2 mCycles Math.PI * input)
- DecelerateInterpolator:在動(dòng)畫(huà)開(kāi)始的地方改變速度較快,然后開(kāi)始減速
- AnticipateInterpolator:反向,先向相反方向改變一段再加速播放
- AnticipateOvershootInterpolator:開(kāi)始的時(shí)候向后然后向前甩一定值后返回最后的值
- BounceInterpolator: 跳躍,快到目的值時(shí)值會(huì)跳躍,如目的值100,后面的值可能依次為85,77,70,80,90,100
- OvershottInterpolator:回彈,最后超出目的值然后緩慢改變到目的值
而這個(gè)東東,我們一般是在寫(xiě)動(dòng)畫(huà)xml文件時(shí)會(huì)用到,屬性是:android:interpolator, 而上面對(duì)應(yīng)的值是:@android:anim/linear_interpolator,其實(shí)就是駝峰命名法變下劃線而已 AccelerateDecelerateInterpolator對(duì)應(yīng):@android:anim/accelerate_decelerate_interpolator!
這里的android:duration都是動(dòng)畫(huà)的持續(xù)時(shí)間,單位是毫秒~
anim_alpha.xml:
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromAlpha="1.0"
android:toAlpha="0.1"
android:duration="2000"/>
屬性解釋:
fromAlpha :起始透明度
toAlpha:結(jié)束透明度
透明度的范圍為:0-1,完全透明-完全不透明
anim_scale.xml:
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromXScale="0.2"
android:toXScale="1.5"
android:fromYScale="0.2"
android:toYScale="1.5"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000"/>
屬性解釋:
- fromXScale/fromYScale:沿著X軸/Y軸縮放的起始比例
- toXScale/toYScale:沿著X軸/Y軸縮放的結(jié)束比例
- pivotX/pivotY:縮放的中軸點(diǎn)X/Y坐標(biāo),即距離自身左邊緣的位置,比如50%就是以圖像的 中心為中軸點(diǎn)
anim_translate.xml:
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXDelta="0"
android:toXDelta="320"
android:fromYDelta="0"
android:toYDelta="0"
android:duration="2000"/>
屬性解釋:
- fromXDelta/fromYDelta:動(dòng)畫(huà)起始位置的X/Y坐標(biāo)
- toXDelta/toYDelta:動(dòng)畫(huà)結(jié)束位置的X/Y坐標(biāo)
anim_rotate.xml:
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromDegrees="0"
android:toDegrees="360"
android:duration="1000"
android:repeatCount="1"
android:repeatMode="reverse"/>
屬性解釋:
- fromDegrees/toDegrees:旋轉(zhuǎn)的起始/結(jié)束角度
- repeatCount:旋轉(zhuǎn)的次數(shù),默認(rèn)值為0,代表一次,假如是其他值,比如3,則旋轉(zhuǎn)4次 另外,值為-1或者infinite時(shí),表示動(dòng)畫(huà)永不停止
- repeatMode:設(shè)置重復(fù)模式,默認(rèn)restart,但只有當(dāng)repeatCount大于0或者infinite或-1時(shí) 才有效。還可以設(shè)置成reverse,表示偶數(shù)次顯示動(dòng)畫(huà)時(shí)會(huì)做方向相反的運(yùn)動(dòng)!
非常簡(jiǎn)單,就是前面幾個(gè)動(dòng)畫(huà)組合到一起而已~
anim_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.2"
android:fromYScale="0.2"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.5"
android:toYScale="1.5" />
<rotate
android:duration="1000"
android:fromDegrees="0"
android:repeatCount="1"
android:repeatMode="reverse"
android:toDegrees="360" />
<translate
android:duration="2000"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="320"
android:toYDelta="0" />
<alpha
android:duration="2000"
android:fromAlpha="1.0"
android:toAlpha="0.1" />
</set>
好的,下面我們就用上面寫(xiě)的動(dòng)畫(huà)來(lái)寫(xiě)一個(gè)例子,讓我們體會(huì)體會(huì)何為補(bǔ)間動(dòng)畫(huà): 首先來(lái)個(gè)簡(jiǎn)單的布局:activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn_alpha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="透明度漸變" />
<Button
android:id="@+id/btn_scale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="縮放漸變" />
<Button
android:id="@+id/btn_tran"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="位移漸變" />
<Button
android:id="@+id/btn_rotate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="旋轉(zhuǎn)漸變" />
<Button
android:id="@+id/btn_set"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="組合漸變" />
<ImageView
android:id="@+id/img_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="48dp"
android:src="@mipmap/img_face" />
</LinearLayout>
好噠,接著到我們的MainActivity.java,同樣非常簡(jiǎn)單,只需調(diào)用AnimationUtils.loadAnimation() 加載動(dòng)畫(huà),然后我們的View控件調(diào)用startAnimation開(kāi)啟動(dòng)畫(huà)即可~
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button btn_alpha;
private Button btn_scale;
private Button btn_tran;
private Button btn_rotate;
private Button btn_set;
private ImageView img_show;
private Animation animation = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindViews();
}
private void bindViews() {
btn_alpha = (Button) findViewById(R.id.btn_alpha);
btn_scale = (Button) findViewById(R.id.btn_scale);
btn_tran = (Button) findViewById(R.id.btn_tran);
btn_rotate = (Button) findViewById(R.id.btn_rotate);
btn_set = (Button) findViewById(R.id.btn_set);
img_show = (ImageView) findViewById(R.id.img_show);
btn_alpha.setOnClickListener(this);
btn_scale.setOnClickListener(this);
btn_tran.setOnClickListener(this);
btn_rotate.setOnClickListener(this);
btn_set.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_alpha:
animation = AnimationUtils.loadAnimation(this,
R.anim.anim_alpha);
img_show.startAnimation(animation);
break;
case R.id.btn_scale:
animation = AnimationUtils.loadAnimation(this,
R.anim.anim_scale);
img_show.startAnimation(animation);
break;
case R.id.btn_tran:
animation = AnimationUtils.loadAnimation(this,
R.anim.anim_translate);
img_show.startAnimation(animation);
break;
case R.id.btn_rotate:
animation = AnimationUtils.loadAnimation(this,
R.anim.anim_rotate);
img_show.startAnimation(animation);
break;
case R.id.btn_set:
animation = AnimationUtils.loadAnimation(this,
R.anim.anim_set);
img_show.startAnimation(animation);
break;
}
}
}
運(yùn)行效果圖:
嘿嘿,有點(diǎn)意思是吧,還不動(dòng)手試試,改點(diǎn)東西,或者自由組合動(dòng)畫(huà),做出酷炫的效果吧~
我們可以對(duì)動(dòng)畫(huà)的執(zhí)行狀態(tài)進(jìn)行監(jiān)聽(tīng),調(diào)用動(dòng)畫(huà)對(duì)象的:
即可完成動(dòng)畫(huà)執(zhí)行狀態(tài)的監(jiān)聽(tīng)~
先調(diào)用AnimationUtils.loadAnimation(動(dòng)畫(huà)xml文件),然后View控件調(diào)用startAnimation(anim) 開(kāi)始動(dòng)畫(huà)~這是靜態(tài)加載的方式,當(dāng)然你也可以直接創(chuàng)建一個(gè)動(dòng)畫(huà)對(duì)象,用Java代碼完成設(shè)置,再調(diào)用 startAnimation開(kāi)啟動(dòng)畫(huà)~
這里要注意一點(diǎn),就是Fragment是使用的v4包還是app包下的Fragment! 我們可以調(diào)用FragmentTransaction對(duì)象的setTransition(int transit) 為Fragment指定標(biāo)準(zhǔn)的過(guò)場(chǎng)動(dòng)畫(huà),transit的可選值如下:
上面的標(biāo)準(zhǔn)過(guò)程動(dòng)畫(huà)是兩個(gè)都可以調(diào)用的,而不同的地方則在于自定義轉(zhuǎn)場(chǎng)動(dòng)畫(huà)
setCustomAnimations()方法!
app包下的Fragment: setCustomAnimations(int enter, int exit, int popEnter, int popExit) 分別是添加,移除,入棧,以及出棧時(shí)的動(dòng)畫(huà)! 另外要注意一點(diǎn)的是,對(duì)應(yīng)的動(dòng)畫(huà)類型是:屬性動(dòng)畫(huà)(Property),就是動(dòng)畫(huà)文件 的根標(biāo)簽要是:,或者是前面兩者放到一個(gè)里;
另外要注意一點(diǎn)的是,對(duì)應(yīng)的動(dòng)畫(huà)類型是:補(bǔ)間動(dòng)畫(huà)(Tween),和上面的View一樣~
可能你會(huì)有疑惑,你怎么知道對(duì)應(yīng)的動(dòng)畫(huà)類型,其實(shí)只要你到Fragment源碼那里找下:
onCreateAnimation()方法的一個(gè)返回值就知道了:
v4包:
app包:
Activty設(shè)置過(guò)場(chǎng)動(dòng)畫(huà)非常簡(jiǎn)單,調(diào)用的方法是:overridePendingTransition(int enterAnim, int exitAnim)
用法很簡(jiǎn)單:在startActivity(intent)或者finish()后添加
參數(shù)依次是:新Activity進(jìn)場(chǎng)時(shí)的動(dòng)畫(huà),以及舊Activity退場(chǎng)時(shí)的動(dòng)畫(huà)
下面提供幾種比較簡(jiǎn)單而且常用的過(guò)場(chǎng)動(dòng)畫(huà)供大家使用~
下載傳送門(mén):Activity常用過(guò)渡動(dòng)畫(huà).zip
運(yùn)行效果圖:
代碼實(shí)現(xiàn):
首先是我們的布局文件:activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#DDE2E3"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/start_ctrl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical"
android:visibility="gone">
<Button
android:id="@+id/start_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#F26968"
android:gravity="center"
android:paddingBottom="15dp"
android:paddingTop="15dp"
android:text="登陸"
android:textColor="#FFFFFF"
android:textSize="18sp" />
<Button
android:id="@+id/start_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#323339"
android:gravity="center"
android:paddingBottom="15dp"
android:paddingTop="15dp"
android:text="注冊(cè)"
android:textColor="#FFFFFF"
android:textSize="18sp" />
</LinearLayout>
</RelativeLayout>
接著是MainActivity.java:
public class MainActivity extends AppCompatActivity {
private LinearLayout start_ctrl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start_ctrl = (LinearLayout) findViewById(R.id.start_ctrl);
//設(shè)置動(dòng)畫(huà),從自身位置的最下端向上滑動(dòng)了自身的高度,持續(xù)時(shí)間為500ms
final TranslateAnimation ctrlAnimation = new TranslateAnimation(
TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0,
TranslateAnimation.RELATIVE_TO_SELF, 1, TranslateAnimation.RELATIVE_TO_SELF, 0);
ctrlAnimation.setDuration(500l); //設(shè)置動(dòng)畫(huà)的過(guò)渡時(shí)間
start_ctrl.postDelayed(new Runnable() {
@Override
public void run() {
start_ctrl.setVisibility(View.VISIBLE);
start_ctrl.startAnimation(ctrlAnimation);
}
}, 2000);
}
}
注釋寫(xiě)得很清楚了,這里就不BB解釋了,如果你對(duì)TranslateAnimation.RELATIVE_TO_SELF這個(gè)有疑惑, 請(qǐng)自己谷歌或者百度,限于篇幅(我懶),這里就不寫(xiě)了,蠻簡(jiǎn)單的~
本節(jié)給大家細(xì)細(xì)地講解了下Android中的第二種動(dòng)畫(huà)(漸變動(dòng)畫(huà)),四種動(dòng)畫(huà)的詳解,以及 設(shè)置動(dòng)畫(huà)監(jiān)聽(tīng)器,還有如何為View,F(xiàn)ragment和Activity設(shè)置動(dòng)畫(huà),最后還寫(xiě)了一個(gè)進(jìn)入后 從APP底部彈出登陸按鈕和注冊(cè)按鈕的例子,篇幅可能有點(diǎn)長(zhǎng),不過(guò)都非常容易理解,相信 大家看完都能夠收獲滿滿~!好的,本節(jié)就到這里,謝謝~
更多建議: