启动动画问题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 


阅读更多
个人分类: 问题汇总 android
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭