2016-08-18 76 views
0

我想要在listView中动画的一部分项目。 动画在代码中描述。动画列表中的部分项目

什么是最好的方式来做到这一点,而不会重载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; 
    } 
+0

只需使用一个asynctask。 –

+0

我不认为这是做这件事的最好方法 – Esperanz0

+0

@MarcoBarbosa这会导致应用崩溃,你不能更新单独线程的视图。 – Linxy

回答

1

您可以使用ValueAnimator在您的颜色之间进行动画处理。

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(); 

这将动画您的项目从原始颜色,红色,原始颜色。

如果你想有多次迭代,你可以考虑使用AnimationSet来代替你的动画。

+0

对Java类模型(stockExchangeModel)不能getBackground()。我知道如何创建anim,处理程序,asynctasks,但想知道什么是bes的方式:)无论如何thx为您的答案 – Esperanz0

+0

哦,我以为你试图动画一个视图。 – Linxy

+0

是的即时通讯尝试。但stockExchangeModel是java类模型。无论如何,我将它转换到列表中的视图。所以它的工作 – Esperanz0