2016-08-18 69 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