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
方法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); }
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); }