0

I want to anim part of items in listView. Animation is described in code.

Whats the best way to do this without overloading mainThread?

 @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        StockExchangeModel stockExchangeModel = mStockExchangeModels.get(position);
        ViewHolder holder;

        if (convertView == null){
            convertView = ((LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.item_stock_exchange, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (stockExchangeModel.isNeedAnim()){
            // blink color RED
            // wait 0.2s
            // return to start color
            // wait 0.2s
            // blink color RED
            // wait 0.2s
            // return to start color
        }


        holder.value.setText(String.format("%.2f",stockExchangeModel.getValue()));
        holder.change.setText(stockExchangeModel.getChange());
        holder.name.setText(stockExchangeModel.getName());


        return convertView;
    }
Esperanz0
  • 1,524
  • 13
  • 35

1 Answers1

1

You can use a ValueAnimator to Animate between your Color.

ValueAnimator animator = ValueAnimator.ofInt(0, 255, 0);
animator.setDuration(200).setStartDelay(200L);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator valueAnimator) {
        int color = Color.argb((int) valueAnimator.getAnimatedValue(), 255, 0, 0);
        stockExchangeModel.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
       }
    });
animator.start();

That would animate your item from Original Color, to Red, to Original Color.

If you want to have multiple iterations of this you might consider using a AnimationSet instead and choreograph your animations.

Linxy
  • 2,525
  • 3
  • 22
  • 37
  • Can't getBackground() on Java Class Model ( stockExchangeModel ). I know how to create anim, handlers, asynctasks but wanted to know what is the bes way : ) anyway thx for your answer – Esperanz0 Aug 18 '16 at 19:23
  • Oh, I thought you were trying to animate a View. – Linxy Aug 18 '16 at 19:26
  • yes im trying. but stockExchangeModel is java class model. Anyway I changeg it to my view in list. so its working – Esperanz0 Aug 18 '16 at 19:26
  • thats exacly what I did. but last question is. ->> holder.container.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_ATOP); not working .. I tried with holder.container.setBackgroundColor(color); and its changing color to Red but then changing to White instead of orignal. what is wrong – Esperanz0 Aug 18 '16 at 19:31
  • hmm.. Maybe you can try to animator.addListener() and override onAnimationEnd and inside that use "view".getBackground().setColorFilter(null); ..... Or, inside onAnimationUpdate, check if (int) valueAnimator.getAnimatedValue() is 0 and if it is, then do the same thing as before. I am not too sure tho :) – Linxy Aug 18 '16 at 19:34
  • but getBackground().setColorFilter(color, PorterDuff.Mode.SRC_ATOP) not changing color even to Red. – Esperanz0 Aug 18 '16 at 19:36