1. 程式人生 > >啟動動畫問題startAnimation

啟動動畫問題startAnimation

private void startSetConversationBgAnimation() {
        String path = getThumbPath(bean);
        if (TextUtils.isEmpty(path)) {
            // 預設背景
            int color = getResources().getColor(R.color.conversation_bg);
            mIvAnimation.setImageDrawable(new ColorDrawable(color));
        } else {
            mIvAnimation.setImageBitmap(BitmapFactory.decodeFile(path));
        }
        Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.activity_conversation_bg_translate_animation);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                mIvCurrentBgAnimation.setVisibility(View.VISIBLE);
            }


            @Override
            public void onAnimationEnd(Animation animation) {
                Drawable d = mIvCurrentBgAnimation.getDrawable();
                mIvCurrentBgAnimation.setImageBitmap(null);
                mAnimationView.setImageDrawable(d);
                mIvCurrentBgAnimation.setVisibility(View.GONE);
            }


            @Override
            public void onAnimationRepeat(Animation animation) {


            }
        });
        mIvCurrentBgAnimation.startAnimation(animation);

}
如果以上動畫連續執行多次的話 動畫效果只會出現一次接著設定的圖片馬上會消失  原因onAnimationEnd會連續執行多次   當執行完一次後mIvAnimation裡的圖片為null 
在次執行時  因為mIvAnimation裡的圖片為空所有mIv也會為空 兒出現以上問題
解決以上為題的方法:

方法1.onAnimationEnd方法不獲取mIvAnimation的圖片  而是呼叫getThumbPath方法重新獲取圖片

 private void startSetConversationBgAnimation() {
        String path = getThumbPath(bean);
        if (TextUtils.isEmpty(path)) {
            // 預設背景 
            int color = getResources().getColor(R.color.conversation_bg);
            mIvAnimation.setImageDrawable(new ColorDrawable(color));
        } else {
            mIvAnimation.setImageBitmap(BitmapFactory.decodeFile(path));
        }
        Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.activity_conversation_bg_translate_animation);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                mIvCurrentBgAnimation.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                String path = getThumbPath(bean);
                if (TextUtils.isEmpty(path)) {
                    // 預設背景 
                    int color = getResources().getColor(R.color.conversation_bg);
                    mIv.setImageDrawable(new ColorDrawable(color));
                } else {
                    mIv.setImageBitmap(BitmapFactory.decodeFile(path));
                }
                mIvCurrentBgAnimation.setImageBitmap(null);
                mIvCurrentBgAnimation.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        mIvCurrentBgAnimation.startAnimation(animation);
    }

方法2.記住最後一次啟動動畫時的animationListener 每次執行動畫裡的回撥時判斷當前物件是否為最後一個animationListener 時的話繼續往下執行否則return
 private void startSetConversationBgAnimation() {
        String path = getThumbPath(bean);
        if (TextUtils.isEmpty(path)) {
            // 預設背景 
            int color = getResources().getColor(R.color.conversation_bg);
            mIvAnimation.setImageDrawable(new ColorDrawable(color));
        } else {
            mIvAnimation.setImageBitmap(BitmapFactory.decodeFile(path));
        }
        Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.activity_conversation_bg_translate_animation);
        mAnimationListener = new Animation.AnimationListener() {
            private boolean checkInstance() {
                return this == mAnimationListener;
            }

            @Override
            public void onAnimationStart(Animation animation) {
                if (checkInstance()) {
                    mIvAnimation.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                if (!checkInstance()) {
                    return;
                }
                Drawable d = mIvAnimation.getDrawable();
                mIvAnimation.setImageBitmap(null);
                mIv.setImageDrawable(d);
                mIvAnimation.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        };
        animation.setAnimationListener(mAnimationListener);
        mIvCurrentBgAnimation.clearAnimation();
        mIvCurrentBgAnimation.startAnimation(animation);
    }

當連續執行2次時 方法1第2次看起來沒有動畫效果直接更換   方法2第2次看起來有動畫效果
如使用動畫2  當第一次更換到一般時  第2次動畫啟動時地圖還是第一次時候的地圖  如果想底圖為第2次時的地圖 那麼要加入preBm變數 在程式碼開始處如果preBm不為空把mIv的圖片設定成preBm