動(dòng)畫:
一、幀動(dòng)畫
res\drawable
Drawable動(dòng)畫
使用的類:AnimationDrawable
常用方法:
start:啟動(dòng)
stop:停止
isRunning:是否正在運(yùn)行
將指定的一系列圖片婿滓,按照順序進(jìn)行切換蚊伞,類似Gif
使用步驟:
1映跟、準(zhǔn)備資源圖片
2畅卓、在res\drawable\下創(chuàng)建<animation-list>
3尉咕、使用<item>進(jìn)行圖片顯示順序的設(shè)置和停留時(shí)間的設(shè)置
4绊袋、在對應(yīng)的ImageView中使用background引用剛剛的<animation-list>
5毕匀、在對應(yīng)的Activity中使用AnimationDrawable的start啟動(dòng)動(dòng)畫
二、補(bǔ)間動(dòng)畫
View動(dòng)畫
res\anim
使用的類:Animation和AnimationUtils
通過設(shè)置圖片的透明度癌别、平移皂岔、縮放展姐、旋轉(zhuǎn)等實(shí)現(xiàn)動(dòng)畫的效果
使用方式:
1躁垛、XML方式
使用步驟:
1、在res文件夾下創(chuàng)建anim文件夾圾笨,用于存儲(chǔ)動(dòng)畫效果
2教馆、在anim下創(chuàng)建對應(yīng)的動(dòng)畫效果
1、alpha:透明度
常用屬性:
fromAlpha:起始的透明度
toAlpha:終止的透明度
duration:動(dòng)畫的持續(xù)時(shí)間擂达,單位毫秒
注:透明度范圍:0.0-1.0土铺,其中0.0完全透明、1.0完全顯示
2板鬓、rotate旋轉(zhuǎn)
常用屬性:
fromDegrees:起始角度
toDegrees:終止角度
pivotX:中心點(diǎn)的x坐標(biāo)
pivotY:中心點(diǎn)的y坐標(biāo)
repeatCount:重復(fù)的次數(shù)
repeatMode:重復(fù)的模式
interpolator:加速效果
3悲敷、translate:平移
常用屬性:
fromXDelta:設(shè)置起始的x坐標(biāo)
toXDelta:終止的x坐標(biāo)
fromYDelta:起始的y坐標(biāo)
toYDelta:終止的y坐標(biāo)
repeatCount:重復(fù)的次數(shù)
repeatMode:重復(fù)的模式
interpolator:加速效果
4、scale:設(shè)置縮放效果
常用屬性:
fromXScale:起始的x方向的縮放
toXScale:結(jié)束的x方向的縮放
fromYScale:起始的y方向的縮放
toYScale:終止的y方向的縮放
repeatCount:重復(fù)的次數(shù)
repeatMode:重復(fù)的模式
interpolator:加速效果
5俭令、set:組合動(dòng)畫
常用屬性:
repeatCount:重復(fù)的次數(shù)
repeatMode:重復(fù)的模式
3后德、在對應(yīng)的Activity中通過AnimationUtils.loadAnimation獲取對應(yīng)的動(dòng)畫對象
4、找到對應(yīng)的ImageView設(shè)置startAnimation對應(yīng)的動(dòng)畫效果并啟動(dòng)
注意:interpolator動(dòng)畫的加速效果
常用的值:
@android:anim/accelerate_decelerate_interpolator" 先加速后減速
@android:anim/accelerate_interpolator 開始慢抄腔,結(jié)束快
@android:anim/decelerate_interpolator 開始快瓢湃,結(jié)束慢
@android:anim/anticipate_interpolator 開始時(shí)會(huì)超出起始位置,再執(zhí)行動(dòng)畫
@android:anim/overshoot_interpolator 結(jié)束時(shí)會(huì)超出終止位置妓柜,再結(jié)束動(dòng)畫
@android:anim/anticipate_overshoot_interpolator 開始和結(jié)束時(shí)都會(huì)超出一定距離
@android:anim/bounce_interpolator 結(jié)束時(shí)箱季,有反彈效果
@android:anim/cycle_interpolator 動(dòng)畫按特定的次數(shù)執(zhí)行多次,有振動(dòng)效果
@android:anim/linear_interpolator 均勻的速度執(zhí)行動(dòng)畫
2棍掐、JAVA方式
常用的動(dòng)畫類:
1藏雏、AlphaAnimation:透明度動(dòng)畫類
構(gòu)造方法:
AlphaAnimation(float fromAlpha, float toAlpha)
參數(shù)說明:
fromAlpha:起始的透明度
toAlpha:終止的透明度
2、RotateAnimation:旋轉(zhuǎn)動(dòng)畫類
構(gòu)造方法:
RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY)
參數(shù)說明:
fromDegrees:起始的角度
toDegrees:終止的角度
pivotX:中心點(diǎn)的x坐標(biāo)
pivotY:中心點(diǎn)的y坐標(biāo)
3、TranslateAnimation:平移動(dòng)畫類
構(gòu)造方法:
TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
參數(shù)說明:
fromXDelta:x方向起始位置
toXDelta:x方向終止位置
fromYDelta:y方向起始位置
toYDelta:y方向終止位置
4掘殴、ScaleAnimation:縮放動(dòng)畫類
構(gòu)造方法:
ScaleAnimation(float fromX, float toX, float fromY, float toY)
參數(shù)說明:
fromX:x方向的起始縮放
toX:x方向終止縮放
fromY:y方向起始縮放
toY:y方向終止縮放
5赚瘦、AnimationSet:組合動(dòng)畫類
常用方法:
addAnimation:添加動(dòng)畫效果
三、屬性動(dòng)畫
res\animator
2種方式:
1奏寨、xml文件
使用步驟:
1起意、在res\animator文件夾
2、創(chuàng)建對應(yīng)的<objectAnimator>
使用android:propretyName:用來標(biāo)記哪種動(dòng)畫
常用的值:
alpha:透明度
backgroundColor:背景色
rotateX/Y:旋轉(zhuǎn)
scaleX/Y:縮放
translationX/Y:移動(dòng)
注意:組合動(dòng)畫使用的<set>
3病瞳、在對應(yīng)的Activity中獲取對應(yīng)的動(dòng)畫并綁定控件且啟動(dòng)
AnimatorInflater.loadAnimator:將指定的xml文件轉(zhuǎn)換為動(dòng)畫對象
setTarget:為指定的控件設(shè)置動(dòng)畫效果
start:啟動(dòng)動(dòng)畫
end:結(jié)束動(dòng)畫
cancel:取消動(dòng)畫
2揽咕、java方式
1、ObjectAnimator:屬性動(dòng)畫
包括常用的:透明度套菜、旋轉(zhuǎn)亲善、移動(dòng)、縮放
2逗柴、AnimatorSet:屬性動(dòng)畫的組合動(dòng)畫
playTogether:同時(shí)播放
playSequentially:順序播放
play(動(dòng)畫).with/before/after:自定義播放
addUpdateListener:監(jiān)聽屬性動(dòng)畫的變化過程
顏色資源:
路徑:res\values\colors
<color >
補(bǔ)間動(dòng)畫
public class MainActivity extends Activity {
private ImageView iv;
//聲明對應(yīng)的補(bǔ)間動(dòng)畫對象
private Animation animation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv=(ImageView)findViewById(R.id.iv);
}
public void click(View view) {
switch (view.getId()) {
case R.id.tv_1://透明度動(dòng)畫
//加載對應(yīng)的透明度動(dòng)畫效果
animation=AnimationUtils.loadAnimation(this, R.anim.my_alpha);
break;
case R.id.tv_2://旋轉(zhuǎn)
animation=AnimationUtils.loadAnimation(this, R.anim.my_rotate);
break;
case R.id.tv_3://移動(dòng)
animation=AnimationUtils.loadAnimation(this, R.anim.my_transalte);
break;
case R.id.tv_4://縮放
animation=AnimationUtils.loadAnimation(this, R.anim.my_scale);
break;
case R.id.iv://組合動(dòng)畫
//animation=AnimationUtils.loadAnimation(this, R.anim.my_set);
AnimationSet as=new AnimationSet(true);
as.addAnimation(animation);
as.setDuration(3000);
as.setRepeatCount(10);
as.setRepeatMode(Animation.RESTART);
as.setFillAfter(true);//設(shè)置動(dòng)畫結(jié)束時(shí)蛹头,是否
//設(shè)置是否有動(dòng)畫效果
as.setInterpolator(this, android.R.anim.fade_in);
break;
}
//為動(dòng)畫設(shè)置監(jiān)聽事件
animation.setAnimationListener(new AnimationListener() {
//啟動(dòng)
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
Log.e("Animation", "onAnimationStart……");
}
//重復(fù)
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
Log.e("Animation", "onAnimationRepeat……");
}
//結(jié)束
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
Log.e("Animation", "onAnimationEnd……");
}
});
//開始執(zhí)行動(dòng)畫
iv.startAnimation(animation);
}
}
xml
<?xml version="1.0" encoding="UTF-8"?>
<alpha android:duration="4000" android:toAlpha="1.0" android:fromAlpha="0.1" xmlns:android="http://schemas.android.com/apk/res/android"> </alpha>
2
<?xml version="1.0" encoding="UTF-8"?>
<rotate android:repeatMode="reverse" android:repeatCount="10" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:pivotY="50%" android:pivotX="50%" android:toDegrees="360" android:fromDegrees="0" android:duration="2000" xmlns:android="http://schemas.android.com/apk/res/android"> </rotate>
3
<?xml version="1.0" encoding="UTF-8"?>
<scale android:interpolator="@android:anim/cycle_interpolator" android:repeatMode="restart" android:repeatCount="10" android:pivotY="50%" android:pivotX="50%" android:toYScale="6" android:fromYScale="0.1" android:toXScale="3" android:fromXScale="0.1" android:duration="3000" xmlns:android="http://schemas.android.com/apk/res/android"> </scale>
4
<?xml version="1.0" encoding="UTF-8"?>
-<set android:repeatMode="reverse" android:repeatCount="10" xmlns:android="http://schemas.android.com/apk/res/android"> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:toAlpha="1.0" android:fromAlpha="0.1" android:duration="2000"> </alpha> <rotate android:repeatMode="reverse" android:repeatCount="10" xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000" android:toDegrees="360" android:pivotY="50%" android:pivotX="50%" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromDegrees="0"> </rotate> <scale android:repeatMode="restart" android:repeatCount="10" xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:pivotY="50%" android:pivotX="50%" android:interpolator="@android:anim/cycle_interpolator" android:toYScale="6" android:toXScale="3" android:fromYScale="0.1" android:fromXScale="0.1"> </scale> <translate android:repeatMode="reverse" android:repeatCount="10" xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:interpolator="@android:anim/bounce_interpolator" android:toYDelta="0" android:toXDelta="200" android:fromYDelta="0" android:fromXDelta="0"> </translate> </set>
6
<?xml version="1.0" encoding="UTF-8"?>
<translate android:interpolator="@android:anim/bounce_interpolator" android:repeatMode="reverse" android:repeatCount="10" android:toYDelta="400" android:fromYDelta="0" android:toXDelta="200" android:fromXDelta="0" android:duration="4000" xmlns:android="http://schemas.android.com/apk/res/android"> </translate>
補(bǔ)間動(dòng)畫有一定的缺陷,按鈕移動(dòng)后點(diǎn)擊事件依然在原來的位置戏溺,動(dòng)畫效果是通過一些列算法來實(shí)現(xiàn)的渣蜗,所以后來出現(xiàn)屬性動(dòng)畫,效果是真實(shí)控件的移動(dòng)變幻旷祸。