當(dāng)前位置:首頁(yè) > 嵌入式培訓(xùn) > 嵌入式學(xué)習(xí) > 講師博文 > Android動(dòng)畫(huà)
補(bǔ)間動(dòng)畫(huà)(Tween Animation)
補(bǔ)間動(dòng)畫(huà)有以下四種
Alpha透明度動(dòng)畫(huà)
Scale尺寸縮放動(dòng)畫(huà)
Translate位置移動(dòng)動(dòng)畫(huà)
Rotate旋轉(zhuǎn)動(dòng)畫(huà)
補(bǔ)間動(dòng)畫(huà)的共同屬性
Duration:動(dòng)畫(huà)持續(xù)時(shí)間(單位:毫秒)
fillAfter:設(shè)置為true,動(dòng)畫(huà)轉(zhuǎn)化在動(dòng)畫(huà)結(jié)束后被應(yīng)用
interpolator:動(dòng)畫(huà)插入器(加速、減速插入器)
repeatCount:動(dòng)畫(huà)重復(fù)次數(shù)
repeatMode:順序重復(fù)/倒序重復(fù)
startOffset:動(dòng)畫(huà)之間的時(shí)間間隔(應(yīng)用于組合動(dòng)畫(huà))
動(dòng)畫(huà)實(shí)現(xiàn)方式
配置文件(/res/anim)——alpha、scale、translate、rotate (更簡(jiǎn)單)
Java代碼實(shí)現(xiàn)——AlphaAnimation、ScaleAnimation、TranslateAnimation、RotateAnimation
(更靈活)
例如
用Java代碼去創(chuàng)建
//創(chuàng)建Alpha動(dòng)畫(huà)(透明度為10%-100%)
Animation alpha = new AlphaAnimation(0.1f,1.0f);
//設(shè)置動(dòng)畫(huà)時(shí)間為5秒
alpha.setDuration(5000);
//開(kāi)始播放
img.startAnimation(alpha);
加載配置文件
Animation scale = AnimationUtils.loadAnimation(context, R.anim.scale_anim);
//開(kāi)始動(dòng)畫(huà)
img.startAnimation(scale);
AlphaAnimation
fromAlpha動(dòng)畫(huà)起始時(shí)的透明度 (1.0表示完全不透明)
toAlpha動(dòng)畫(huà)終止時(shí)的透明度 (0.0表示完全透明)
ScaleAnimation
fromX,toX分別是起始和結(jié)束時(shí)x坐標(biāo)上的伸縮尺寸
fromY,toY分別是起始和結(jié)束時(shí)y坐標(biāo)上的伸縮尺寸
pivotX,pivotY分別為伸縮動(dòng)畫(huà)相對(duì)于x,y軸開(kāi)始位置
Translate
fromXDelta,fromYDelta分別是起始的X,Y的坐標(biāo)
toXDelta,toYDelta分別是結(jié)束時(shí)X,Y的坐標(biāo)
RotateAnimation
fromDegrees
toDegrees
pivotX,pivotY分別為旋轉(zhuǎn)動(dòng)畫(huà)相對(duì)于x,y的坐標(biāo)開(kāi)始位置
動(dòng)畫(huà)監(jiān)聽(tīng)器
AnimationListener
幀動(dòng)畫(huà)(Frame Animation)
概念
逐幀動(dòng)畫(huà)是一種常見(jiàn)的動(dòng)畫(huà)形式(Frame By Frame),其原理是在“連續(xù)的關(guān)鍵幀”中分解動(dòng)畫(huà)動(dòng)作,也就是在時(shí)間軸的每幀上逐幀繪制不同的內(nèi)容,使其連續(xù)播放而成動(dòng)畫(huà)。
幀動(dòng)畫(huà)實(shí)現(xiàn)
使用加載配置文件
將圖片資源拷貝到drawable資源目錄中
在drawable目錄中創(chuàng)建幀動(dòng)畫(huà)配置文件,在該文件中配置動(dòng)畫(huà)所需圖片,以及每個(gè)圖片停留時(shí)間。
<animation-list xmlns:android="//schemas.android.com/apk/res/android" >
<item
android:drawable="幀圖片的資源id"
android:duration="播放該幀的時(shí)間"/>
android:drawable="幀圖片的資源id"
android:duration="播放該幀的時(shí)間"/>
</animation-list>
//載入xml逐幀動(dòng)畫(huà)資源
imageView.setImageResource(R.drawable.btn_animation);
aniDrawable = (AnimationDrawable) imageView.getDrawable();
//播放動(dòng)畫(huà)
aniDrawable.setOneShot(true);
aniDrawable.start();
//停止動(dòng)畫(huà)
//aniDrawable.stop();
//檢查動(dòng)畫(huà)是否正在播放
//aniDrawable.isRunning();
使用Java代碼創(chuàng)建
//創(chuàng)建動(dòng)畫(huà)對(duì)象
AnimationDrawable aniDrawable = new AnimationDrawable();
//設(shè)置每一幀的幀圖片,每一幀播放時(shí)間
aniDrawable.addFrame(this.getResources().getDrawable(R.drawable.inc_btn_emphasize_normal), 100);
aniDrawable.addFrame(this.getResources().getDrawable(R.drawable.inc_btn_emphasize_pressed), 100);
aniDrawable.addFrame(this.getResources().getDrawable(R.drawable.inc_btn_normal), 100);
aniDrawable.addFrame(this.getResources().getDrawable(R.drawable.inc_btn_pressed), 100);
imageView.setImageDrawable(aniDrawable);
練習(xí)
制作不會(huì)說(shuō)話的湯姆貓
屬性動(dòng)畫(huà)(Property Animation)
概念
andorid3.0引入。屬性動(dòng)畫(huà)改變對(duì)象的一個(gè)field值實(shí)現(xiàn)動(dòng)畫(huà)。指定你想要的屬性,多長(zhǎng)時(shí)間,動(dòng)畫(huà)的值就可以實(shí)現(xiàn)了。
Animation的局限性
一個(gè)屬性動(dòng)畫(huà)的實(shí)現(xiàn)
//參數(shù):
//動(dòng)畫(huà)作用目標(biāo)
//屬性
//屬性值
ObjectAnimator animator = ObjectAnimator.ofFloat(iv, "translationX", 0, 100);
animator.setDuration(2000);
animator.start();
Animation動(dòng)畫(huà)框架僅僅只是讓圖像發(fā)生位移,而監(jiān)聽(tīng)事件依然在原地。而屬性動(dòng)畫(huà)可以移動(dòng)讓監(jiān)聽(tīng)事件也跟著移動(dòng)。
多個(gè)屬性動(dòng)畫(huà)實(shí)現(xiàn)01
PropertyValuesHolder p01 = PropertyValuesHolder.ofFloat("translationX", 0, 100);
PropertyValuesHolder p02 = PropertyValuesHolder.ofFloat("translationY", 0, 100);
PropertyValuesHolder p03 = PropertyValuesHolder.ofFloat("rotation", 0, 360);
ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(iv, p01, p02, p03);
animator.setDuration(2000);
animator.start();
多個(gè)屬性動(dòng)畫(huà)實(shí)現(xiàn)02
ObjectAnimator oa01 = ObjectAnimator.ofFloat(iv, "translationX", 0, 50);
ObjectAnimator oa02 = ObjectAnimator.ofFloat(iv, "translationY", 0, 50);
ObjectAnimator oa03 = ObjectAnimator.ofFloat(iv, "rotation", 0, 360);
AnimatorSet set = new AnimatorSet();
//同時(shí)播放
//set.playTogether(oa01, oa02, oa03);
//按順序播放(先播01,再播02,后播03)
//set.playSequentially(oa01, oa02, oa03);
//按指定順序播放 (01和02同時(shí)播放,后播放03)
set.play(oa01).with(oa02);
set.play(oa03).after(oa01);
set.setDuration(1000);
set.start();
動(dòng)畫(huà)監(jiān)聽(tīng)事件
透明度屬性動(dòng)畫(huà)
ObjectAnimator animator = ObjectAnimator.ofFloat(v, "alpha", 0, 1);
animator.setDuration(1000);
//添加屬性動(dòng)畫(huà)的監(jiān)聽(tīng)
animator.addListener(new AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animator animation) {
// TODO Auto-generated method stub
}
//動(dòng)畫(huà)結(jié)束之后調(diào)用該方法
@Override
public void onAnimationEnd(Animator animation) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "click", Toast.LENGTH_SHORT).show();
}
@Override
public void onAnimationCancel(Animator animation) {
// TODO Auto-generated method stub
}
});
animator.start();
使用ValueAnimator改變文字顏色
ValueAnimator valueAni = ValueAnimator.ofInt(0, 255);
valueAni.setDuration(2000);
valueAni.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
// TODO Auto-generated method stub
tv.setTextColor(Color.rgb((Integer)animation.getAnimatedValue(), 0, 0));
}
});
valueAni.start();