1.苦逼的需求又來了,需要實作一些動畫效果,第一個想到的是播放gif圖檔,但是這樣會占包的資源,并且清晰度不高,于是想着程式實作,自己用幀動畫+縮放+移動+透明度 實作了一些想要的效果,這裡跟大家分享一下
2.效果圖如下:
3.幀動畫實作代碼
1).首先擷取每幀顯示的imageview控件,然後把所有幀放到animationdrawable對象裡面去,開啟動畫,通過handle延時2秒關閉動畫
imageview ivframe = (imageview) findviewbyid(r.id.iv_frame);
// 通過逐幀動畫的資源檔案獲得animationdrawable示例
final animationdrawable frameanim = (animationdrawable) getresources().getdrawable(r.drawable.thank_you_boss_anim);
ivframe.setbackgrounddrawable(frameanim);// 把animationdrawable設定為imageview的背景
frameanim.start();
new handler().postdelayed(new runnable() {
@override
public void run(){
if (frameanim != null && frameanim.isrunning()) {//2秒之後結束動畫
frameanim.stop();
}
}
},2000);
2).thank_you_boss_anim.xml 所有幀的布局檔案 設定每一幀的圖檔,每幀時間
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >
<!-- 定義一個動畫幀,drawable為img0,持續時間300毫秒 -->
<item android:drawable="@drawable/icon_think_boss_one" android:duration="100"/>
<item android:drawable="@drawable/icon_think_boss_two" android:duration="100"/>
<item android:drawable="@drawable/icon_think_boss_three" android:duration="100"/>
<item android:drawable="@drawable/icon_think_boss_four" android:duration="300"/>
</animation-list>
4.幀動畫加上移動 放大 透明度效果
開啟一個dialog,背景設定透明,得到幀動畫跟文字圖檔顯示imageview,然後開啟兩個動畫。
/**
* 謝謝土豪打賞
*/
private void thanktuhaoreward(){
view view=layoutinflater.from(this).inflate(r.layout.activity_thank_tuhao_reward,null);
final dialog tyrantdialog = new dialog(this,r.style.selectordialog);
tyrantdialog.findviewbyid(r.id.iv_frame);
tyrantdialog.setcontentview(view);
tyrantdialog.setcanceledontouchoutside(true);
imageview ivframe=(imageview) tyrantdialog.findviewbyid(r.id.iv_frame);
// 通過逐幀動畫的資源檔案獲得animationdrawable示例
final animationdrawable frameanim=(animationdrawable) getresources().getdrawable(r.drawable.thank_you_tyrant_anim);
ivframe.setbackgrounddrawable(frameanim);//把animationdrawable設定為imageview的背景
frameanim.start();
imageview playthinkonetext=(imageview) tyrantdialog.findviewbyid(r.id.play_think_one_text);
animation anim = animationutils.loadanimation(this,r.anim.translate_thank_you_tyrant);
playthinkonetext.startanimation(anim);
anim.setfillafter(true);
tyrantdialog.show();
view.setonclicklistener(new onclicklistener() {
@override
public void onclick(view arg0) {
stop(frameanim);
tyrantdialog.cancel();
});
anim.setanimationlistener(new animationlistener() {
public void onanimationstart(animation animation) {}
public void onanimationrepeat(animation animation) {}
public void onanimationend(animation animation){
}
* 停止播放
protected void stop(animationdrawable frameanim) {
if (frameanim != null && frameanim.isrunning()) {
frameanim.stop();
幀動畫的布局檔案我就不貼出來了,貼一下移動,縮放,改變透明度的布局檔案 translate_thank_you_tyrant.xml
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="1000"
android:fromxdelta="30"
android:fromydelta="30"
android:toxdelta="30"
android:toydelta="-120" />
<alpha
android:duration="2000"
android:fromalpha="0.4"
android:toalpha="1.0" />
<scale
android:fillafter="false"
android:fromxscale="0.4"
android:fromyscale="0.4"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotx="50%"
android:pivoty="50%"
android:toxscale="1.0"
android:toyscale="1.0" />
<!--
translate 位置轉移動畫效果
整型值:
fromxdelta 屬性為動畫起始時 x坐标上的位置
toxdelta 屬性為動畫結束時 x坐标上的位置
fromydelta 屬性為動畫起始時 y坐标上的位置
toydelta 屬性為動畫結束時 y坐标上的位置
注意:沒有指定fromxtype toxtype fromytype toytype 時候,預設是以自己為相對參照物
長整型值:
duration 屬性為動畫持續時間
說明: 時間以毫秒為機關
-->
</set>
<a target="_blank" href="http://download.csdn.net/detail/lowprofile_coding/9023781">點選下載下傳源碼</a>