tweened animation: 漸變動畫; 2>frame by frame: 畫面轉(zhuǎn)換動畫.2.Android的Animation動畫由四種類型組成:X..."/>
您好,登錄后才能下訂單哦!
1.Android動畫模式:
1>tweened animation: 漸變動畫;
2>frame by frame: 畫面轉(zhuǎn)換動畫.
2.Android的Animation動畫由四種類型組成:
XML | |
alpha | 漸變透明度動畫效果 |
scale | 漸變尺寸伸縮動畫效果 |
translate | 畫面轉(zhuǎn)換位置移動動畫效果 |
rotate | 畫面轉(zhuǎn)移旋轉(zhuǎn)動畫效果 |
Java代碼 | |
AlphaAnimation | 漸變透明度動畫效果 |
ScaleAnimation | 漸變尺寸伸縮動畫效果 |
TranslateAnimation | 畫面轉(zhuǎn)換位置移動動畫效果 |
RotateAnimation | 畫面轉(zhuǎn)移旋轉(zhuǎn)動畫效果 |
3.動畫效果示例
在res/下新建anim文件夾,下面動畫文件放入anim下面
1> alpha 淡入效果:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="2000" /> </set> <!-- fromAlpha:開始時透明度 toAlpha: 結(jié)束時透明度 duration:動畫持續(xù)時間 -->
2>alpha 淡出效果:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="2000" /> </set> <!-- fromAlpha:開始時透明度 toAlpha: 結(jié)束時透明度 duration:動畫持續(xù)時間 -->
3> rotate 旋轉(zhuǎn)效果:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="false" android:zAdjustment="bottom" > <rotate android:duration="4000" android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:toDegrees="360" /> <!-- fromDegrees 動畫開始時的角度 toDegrees 動畫結(jié)束時物件的旋轉(zhuǎn)角度,正代表順時針 pivotX 屬性為動畫相對于物件的X坐標(biāo)的開始位置 pivotY 屬性為動畫相對于物件的Y坐標(biāo)的開始位置 --> </set>
4> scale 縮放效果:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <scale android:duration="10000" android:fromXScale="0.0" android:fromYScale="0.0" android:interpolator="@android:anim/decelerate_interpolator" android:pivotX="50%" android:pivotY="50%" android:repeatCount="1" android:repeatMode="reverse" android:startOffset="0" android:toXScale="1.5" android:toYScale="1.5" /> </set> <!-- fromXDelta,fromYDelta 起始時X,Y座標(biāo),屏幕右下角的座標(biāo)是X:320,Y:480 toXDelta, toYDelta 動畫結(jié)束時X,Y的座標(biāo) --> <!-- interpolator 指定動畫插入器 常見的有加速減速插入器 accelerate_decelerate_interpolator 加速插入器 accelerate_interpolator, 減速插入器 decelerate_interpolator。 fromXScale,fromYScale, 動畫開始前X,Y的縮放,0.0為不顯示, 1.0為正常大小 toXScale,toYScale, 動畫最終縮放的倍數(shù), 1.0為正常大小,大于1.0放大 pivotX, pivotY 動畫起始位置,相對于屏幕的百分比,兩個都為50%表示動畫從屏幕中間開始 startOffset, 動畫多次執(zhí)行的間隔時間,如果只執(zhí)行一次,執(zhí)行前會暫停這段時間, 單位毫秒 duration,一次動畫效果消耗的時間,單位毫秒, 值越小動畫速度越快 repeatCount,動畫重復(fù)的計數(shù),動畫將會執(zhí)行該值+1次 repeatMode,動畫重復(fù)的模式,reverse為反向,當(dāng)?shù)谂即螆?zhí)行時,動畫方向會相反。 restart為重新執(zhí)行,方向不變 -->
5> translate 移動效果:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="4000" android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="50" android:toYDelta="0" /> </set> <!-- fromXDelta,fromYDelta 起始時X,Y座標(biāo),屏幕右下角的座標(biāo)是X:320,Y:480 toXDelta, toYDelta 動畫結(jié)束時X,Y的座標(biāo) -->
在代碼中引用動畫文件:
public class MainActivity extends Activity implements android.view.View.OnClickListener { private TextView tvShow; private Button btnRotate,btnFadeOut,btnFadeIn,btnScale,btnTranslate; private Animation animation = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); tvShow = (TextView) findViewById(R.id.tvShow); btnRotate = (Button) findViewById(R.id.btnRotate); btnRotate.setOnClickListener(this); btnFadeOut = (Button) findViewById(R.id.btnFadeOut); btnFadeOut.setOnClickListener(this); btnFadeIn = (Button) findViewById(R.id.btnFadeIn); btnFadeIn.setOnClickListener(this); btnScale = (Button) findViewById(R.id.btnScale); btnScale.setOnClickListener(this); btnTranslate = (Button) findViewById(R.id.btnTranslate); btnTranslate.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { //翻轉(zhuǎn) case R.id.btnRotate: animation = AnimationUtils.loadAnimation(this, R.anim.rotate); break; //淡出 case R.id.btnFadeOut: animation = AnimationUtils.loadAnimation(this, R.anim.alpha_fadeout); break; //淡入 case R.id.btnFadeIn: animation = AnimationUtils.loadAnimation(this, R.anim.alpha_fadein); break; //縮放 case R.id.btnScale: animation = AnimationUtils.loadAnimation(this, R.anim.scale); break; //平移 case R.id.btnTranslate: animation = AnimationUtils.loadAnimation(this, R.anim.translate); break; default: break; } if(animation != null){ tvShow.startAnimation(animation); } } }
效果見下圖:
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。