android 不能播放animation-list動畫


經測試,應該是3.0一下的機型在播放animation-lis時,動畫沒有播放;然后網絡搜索,找到一篇文章,講的非常詳細http://www.cnblogs.com/firecode/archive/2012/11/01/2749774.html

里面給出了幾種執行動畫的方法,我這邊就再重復一下:

首先提一點需要注意:

1》ImageView的src與background有什么區別呢?
用src的時候,應是原圖顯示,不該變圖片的大小;用background的時候,按照組件的大小來放大或者縮小圖片。

2》代碼中使用setBackgroundResource方法設置imageView資源背景,相當於布局文件中的android:background屬性,這個屬性是view類的屬性,必須通過getBackground()方法來獲取;而getdrawable()是imageview類的方法,必須通過在代碼中setImageResource(int)(對應布局文件的android:src)或setImageDrawable(Drawable drawable)方法設置才可以使用getdrawable()方法;否則程序中代碼雖然不會出錯,但是運行后會報空指針異常!!!

方法 1 

<ImageView
        android:id="@+id/splash_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="55dp"
        android:src="@drawable/explosiontext" />

iv = (ImageView) findViewById(R.id.splash_iv);
        anim = (AnimationDrawable) iv.getDrawable();
        iv.post(new Runnable() {
            @Override
            public void run() {
                anim.start();
            }
        });

方法 2

// @Override
    // public void onWindowFocusChanged(boolean hasFocus) {
    // anim.start();
    // super.onWindowFocusChanged(hasFocus);
    // }

方法 3 

iv = (ImageView) findViewById(R.id.splash_iv);
        anim = (AnimationDrawable) iv.getDrawable();
        iv.getViewTreeObserver().addOnPreDrawListener(preDrawListener);

OnPreDrawListener preDrawListener = new OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            anim.start();
            return true; // 必須要有這個true返回
        }
    };

方法 4 

imageView.setBackgroundResource(R.anim.xxxxx);
AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
RunAnim runAnim=new RunAnim();
runAnim.execute("");

class RunAnim extends AsyncTask<String, String, String> {
        @Override
        protected String doInBackground(String... params) {
            if (!animationDrawable.isRunning()) {
                animationDrawable.stop();
                animationDrawable.start();
            }
            return "";
        }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM