2016-08-30 6 views
0

こんにちは。私は、この半分の通常のオブジェクトアニメーターが何度もヒーティングを起こし、ある時点でメモリの問題を引き起こしているシナリオを持っています。このような虹アニメーションの静的メソッドを作っています。オブジェクトのアニメーターが更新リスナーを削除していないandroid

public static ObjectAnimator startRainbowAnimation(Context context, 
                String textToShow, 
                final TextView textViewToAttach) { 
    AnimatedColorSpan span = new AnimatedColorSpan(context); 
    final SpannableString spannableString = new SpannableString(textToShow); 
    String substring = textToShow; 
    int start = textToShow.indexOf(substring); 
    int end = start + substring.length(); 
    spannableString.setSpan(span, start, end, 0); 

    ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(
      span, ANIMATED_COLOR_SPAN_FLOAT_PROPERTY, 0, 100); 
    objectAnimator.setEvaluator(new FloatEvaluator()); 
    objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
     @Override 
     public void onAnimationUpdate(ValueAnimator valueAnimator) { 
      System.gc(); 
      Log.d("Fafasfasfas", "onAnimationUpdate: inside true"); 
      textViewToAttach.setText(spannableString); 
     } 
    }); 
    objectAnimator.setInterpolator(new LinearInterpolator()); 
    objectAnimator.setDuration(DURATION); 
    objectAnimator.setRepeatCount(ValueAnimator.INFINITE); 
    objectAnimator.start(); 
    return objectAnimator; 
} 

private static final Property<AnimatedColorSpan, Float> ANIMATED_COLOR_SPAN_FLOAT_PROPERTY 
     = new Property<AnimatedColorSpan, Float>(Float.class, "ANIMATED_COLOR_SPAN_FLOAT_PROPERTY") { 
    @Override 
    public void set(AnimatedColorSpan span, Float value) { 
     span.setTranslateXPercentage(value); 
    } 

    @Override 
    public Float get(AnimatedColorSpan span) { 
     return span.getTranslateXPercentage(); 
    } 
}; 

私はティ問題は、あなたが気づいた場合Log.d()はcancleと端がobjectAnimatorに呼ばれた後でさえ、焼成、はい続けるということです

@Override 
    public void onBindViewHolder(final ViewHolder holder, int position) { 
     ChatModel chatModel = chatModelList.get(position); 

     System.gc(); 

     String messageBody = chatModel.getMessage().replaceAll("userid=" + chatModel.getUserId() + ":" + Constants.TYPE_MESSAGE_ATTACHMENT, "").replaceAll("userid=" + chatModel.getOpponentId() + ":" + Constants.TYPE_MESSAGE_ATTACHMENT, ""); 
     holder.message.setText(messageBody); 
     if (showAsRainbow) { 
       if (holder.message.getTag() == null) { 
        objectAnimator = RainbowAnimation.startRainbowAnimation(mContext, messageBody, holder.message); 
        holder.message.setTag(ANIMATED); 
       } 
      } else { 
       objectAnimator.removeAllUpdateListeners(); 
       objectAnimator.removeAllListeners(); 
       objectAnimator.end(); 
       objectAnimator.cancel(); 
       holder.message.setTag(null); 
      } 

     LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) holder.chatViewBubble.getLayoutParams(); 
     LinearLayout.LayoutParams deliveryStatusParams = (LinearLayout.LayoutParams) holder.deliveryStatus.getLayoutParams(); 
     LinearLayout.LayoutParams gifChatViewLayoutParams = (LinearLayout.LayoutParams) holder.imageViewWrapper.getLayoutParams(); 

     checkForGif(chatModel, holder); 

     if (mCurrentUserId.equals(chatModel.getUserId())) { 
      layoutParams.gravity = Gravity.RIGHT; 
      layoutParams.rightMargin = Dp.toDps(mContext, 16); 
      deliveryStatusParams.gravity = Gravity.RIGHT; 
      gifChatViewLayoutParams.gravity = Gravity.RIGHT; 

      holder.chatViewBubble.setLayoutParams(layoutParams); 
      holder.imageView.setLayoutParams(gifChatViewLayoutParams); 

      holder.chatViewBubble.setBackground(ContextCompat.getDrawable(mContext, R.drawable.outgoing_message_bg)); 
      if (chatModel.getDeliveryStatus().equals(Constants.STATUS_DELIVERED)) { 
       if (position >= chatModelList.size() - 1) { 
        holder.deliveryStatus.setVisibility(View.VISIBLE); 
       } else { 
        holder.deliveryStatus.setVisibility(View.INVISIBLE); 
       } 
       holder.deliveryStatus.setText(mContext.getString(R.string.sentText)); 
      } else if (chatModel.getDeliveryStatus().equals(Constants.STATUS_NOT_DELIVERED)) { 
       holder.deliveryStatus.setVisibility(View.VISIBLE); 
       if (updating) { 
        holder.deliveryStatus.setText(mContext.getString(R.string.sendingNowText) + percentage + " %"); 
       } else { 
        holder.deliveryStatus.setText(mContext.getString(R.string.sendingNowText)); 
       } 

      } 
     } else { 
      holder.chatViewBubble.setBackground(ContextCompat.getDrawable(mContext, R.drawable.incoming_message_bg)); 
      layoutParams.gravity = Gravity.LEFT; 
      gifChatViewLayoutParams.gravity = Gravity.LEFT; 

      holder.chatViewBubble.setLayoutParams(layoutParams); 
      holder.imageView.setLayoutParams(gifChatViewLayoutParams); 
      holder.deliveryStatus.setVisibility(View.INVISIBLE); 
     } 

    } 

リサイクル・ビュー・アダプタの内部で、このように、このメソッドを呼び出しています私はキャンセルを確認して終了が呼び出されているので、私は何が間違っていたのか分かりません。誰も助けてくれますか?

答えて

1

私はたくさんの人がポストを見ていることを知っています。つまり、この問題も解決しています。問題は固定されたメソッドでもループ内で実行されたときです。オブジェクトのアニメーターの配列リストを作成し、各呼び出しでアイテムを配列リストに追加します。停止するときは、単に配列をループしてすべてのオブジェクトアニメーターを停止するだけです。ここでは、コード

private ArrayList<ObjectAnimator> objectAnimators = new ArrayList<>(); 
    public void startRainbowAnimation(Context context, 
            final String textToShow, 
            final TextView textViewToAttach) { 
    stopCalled = false; 
    AnimatedColorSpan span = new AnimatedColorSpan(context); 
    final SpannableString spannableString = new SpannableString(textToShow); 
    String substring = textToShow; 
    int start = textToShow.indexOf(substring); 
    int end = start + substring.length(); 
    spannableString.setSpan(span, start, end, 0); 

    ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(
      span, animatedColorSpanFloatProperty, 0, 100); 
    objectAnimator.setEvaluator(new FloatEvaluator()); 

    objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
     @Override 
     public void onAnimationUpdate(ValueAnimator valueAnimator) { 
      System.gc(); 
      if (!stopCalled) { 
       textViewToAttach.setText(spannableString); 
      } 
     } 
    }); 
    objectAnimator.setInterpolator(new LinearInterpolator()); 
    objectAnimator.setDuration(DateUtils.MINUTE_IN_MILLIS * 3); 
    objectAnimator.setRepeatCount(ValueAnimator.INFINITE); 
    objectAnimator.start(); 
    objectAnimators.add(objectAnimator); 
} 
objectAnimators here is an array list of object animators like this 

であり、ここであなたは無限の更新リスナーコール発火の乗り心地を得るためにそれらのすべてを停止する方法です。

public void stopRainbowAnimation() { 
    System.gc(); 
    stopCalled = true; 
    if (!objectAnimators.isEmpty()) { 
     for (int i = 0; i < objectAnimators.size(); i++) { 
      ObjectAnimator eachAnimator = objectAnimators.get(i); 
      eachAnimator.setRepeatCount(0); 
      eachAnimator.end(); 
      eachAnimator.cancel(); 
      eachAnimator.removeAllListeners(); 
      eachAnimator.removeAllUpdateListeners(); 
     } 
     objectAnimators.clear(); 
    } 
} 

これは誰か

+0

をObjectAnimator eachAnimator = objectAnimators.get(I)を助けることを願っています。 eachAnimator.setRepeatCount(0); eachAnimator.end(); eachAnimator.cancel(); eachAnimator.removeAllListeners(); eachAnimator.removeAllUpdateListeners(); は問題が解決します。 –

関連する問題