Android編程實現(xiàn)動畫自動播放功能
本文實例講述了Android編程實現(xiàn)動畫自動播放功能。分享給大家供大家參考,具體如下:
private ImageView image;
private AnimationDrawable animDrawable = new AnimationDrawable();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.table_title);
image = (ImageView) this.findViewById(R.id.scrollView);
//代碼實現(xiàn)
// for(int i=0; i<16; i++){
// int id = getResources().getIdentifier("load_"+(i+1), "drawable", getPackageName());
// animDrawable.addFrame(getResources().getDrawable(id), 50);
// }
// animDrawable.setOneShot(false);
// image.setBackgroundDrawable(animDrawable);
animDrawable = (AnimationDrawable) image.getBackground();
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
animDrawable.start();
}
在onCreate中執(zhí)行 animDrawale.start() 動畫并不執(zhí)行。 搞不懂啊
另外以上代碼測試過有缺陷。 例如在TabActivity中 onWindowFocusChanged只會執(zhí)行一次
所以
AnimationDrawable ad;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView iv = (ImageView) findViewById(R.id.animation_view);
iv.setBackgroundResource(R.drawable.animation);
ad = (AnimationDrawable) iv.getBackground();
//關(guān)鍵代碼
iv.getViewTreeObserver().addOnPreDrawListener(opdl);
}
OnPreDrawListener opdl=new OnPreDrawListener(){
@Override
public boolean onPreDraw() {
if(ad.isRunning() == false){
ad.start();
}
return true; //注意必須返回true
}
};
此方法試過 確實可行。 很好。
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)動畫技巧匯總》、《Android開發(fā)入門與進階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
相關(guān)文章
Flutter使用sqflite處理數(shù)據(jù)表變更的方法詳解
了解過數(shù)據(jù)庫的同學應(yīng)該會知道,數(shù)據(jù)表結(jié)構(gòu)是可能發(fā)生改變的。所以本文為大家介紹了Flutter?使用?sqflite?處理數(shù)據(jù)表變更的版本升級處理方法,感興趣的可以了解一下2023-04-04
Flutter開發(fā)之Widget自定義總結(jié)
這篇文章主要給大家介紹了關(guān)于Flutter開發(fā)中Widget自定義的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Flutter具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-04-04
Android 中對于圖片的內(nèi)存優(yōu)化方法
Android 中對于圖片的內(nèi)存優(yōu)化方法,需要的朋友可以參考一下2013-03-03
Android Flutter實現(xiàn)仿閑魚動畫效果
目前正在做的項目,為了增加用戶的體驗度,準備增加一些動畫效果。本文將通過Android Flutter實現(xiàn)仿閑魚動畫效果,感興趣的可以嘗試一下2023-02-02

