2011-07-28 29 views
29

我有一個帶重寫getView方法的listview來填充它。 現在,我想讓列表中的每個項目都動起來或從屏幕的右側 移動到項目通常應該出現的左側。如何爲每個列表視圖項目製作翻譯動畫

每個項目的動畫不應該在同一時間啓動,它必須延遲對於其他項目,移動前幾毫秒...

好,這是我的適配器類:

public class MyAdapter extends ArrayAdapter<String>{ 

    private Context context; 
    private String[] info; 

    public MyAdapter(Context context, int resource, 
      String[] objects) { 
     super(context, resource, objects); 
     // TODO Auto-generated constructor stub 
     this.context = context; 
     this.info = objects; 

    } 

    protected class RowViewHolder { 
     public TextView text1; 
     public CheckBox cb; 
     public String ss; 
    } 

    @Override 
    public View getView(int pos, View inView, ViewGroup parent) { 
      View vix = inView; 

      RowViewHolder holder; 

      if (vix == null) { 
       LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       vix = inflater.inflate(R.layout.check_list, null); 
      }  
       holder = new RowViewHolder(); 

       holder.text1 = (TextView) vix.findViewById(R.id.info_group); 
       holder.text1.setText(info[pos]); 

       holder.ss = info[pos]; 

       holder.cb = (CheckBox) vix.findViewById(R.id.check); 
       holder.cb.setTag(holder.ss); 
       holder.cb.setOnCheckedChangeListener(CbListen); 

       vix.setTag(holder); 

      return vix; 
    } 

    private OnCheckedChangeListener CbListen = new OnCheckedChangeListener(){ 
     @Override 
     public void onCheckedChanged(CompoundButton com, boolean pool) { 
      // TODO Auto-generated method stub 
      String state = (com.getTag()).toString(); 

      if(com.isChecked()){ 
       System.out.println(state+" CHECKED"); 
      }else{ 
       System.out.println(state+" UNCHECKED"); 
      } 
     } 
    }; 

} 

任何想法? :)

UPDATE

嘛,肯定是! LOL:p

只是下載那些ApiDemos「像Farhan說的那樣」 ,你們會在包視圖中找到一些像LayoutAnimation2樣本。

那裏,列表中的每個項目都被動畫爲向下填充 翻譯動畫,而Alpha分別在變化。

這就是我對我的情況做:

AnimationSet set = new AnimationSet(true); 

    Animation animation = new AlphaAnimation(0.0f, 1.0f); 
    animation.setDuration(500); 
    set.addAnimation(animation); 

    animation = new TranslateAnimation(
     Animation.RELATIVE_TO_SELF, 50.0f,Animation.RELATIVE_TO_SELF, 0.0f, 
     Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f 
    ); 
    animation.setDuration(1000); 
    set.addAnimation(animation); 

    LayoutAnimationController controller = new LayoutAnimationController(set, 0.5f); 

    group_list.setLayoutAnimation(controller); 

我把這個我下面的setAdapter()函數, 你們可以讓它看起來與加速,減速等效果更加舒適。

:p

+1

noop不知道,但你可能想檢查api演示的源代碼....真的有一些關於動畫的好東西太.. – Farhan

回答

1

getView()填充活動中的每個項目。嘗試使用Timer將您的動畫創建爲getView。

1
package com.Animation1; 

import android.app.ListActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.animation.Animation; 
import android.view.animation.AnimationUtils; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.AdapterView; 
import java.util.ArrayList; 

public class Animation1Activity extends ListActivity implements 
           AdapterView.OnItemSelectedListener { 
    Animation anim = null; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 
     anim = AnimationUtils.loadAnimation(this, R.anim.magnify); 
     setContentView(R.layout.main); 
     ListView v = getListView();  // set up item selection listener 
     v.setOnItemSelectedListener(this); // see OnItemSelectedListener methods below 
     ArrayList<String> items = new ArrayList<String>(); 
     items.add("Red"); 
     items.add("Grey"); 
     items.add("Cyan"); 
     items.add("Blue"); 
     items.add("Green"); 
     items.add("Yellow"); 
     items.add("Black"); 
     items.add("White"); 
     ArrayAdapter itemsAdapter = new ArrayAdapter(this, R.layout.row, items); 
     setListAdapter(itemsAdapter); 
    } 

    protected void onListItemClick(ListView l, 
            View v, 
            int position, 
            long id) { 
     v.startAnimation(anim); 
    } 

// --- AdapterView.OnItemSelectedListener methods --- 
    public void onItemSelected(AdapterView parent, 
      View v, 
      int position, 
      long id) { 
     v.startAnimation(anim); 
    } 

    public void onNothingSelected(AdapterView parent) {} 
} 
0

您也可以使用XML屬性您的ListView 佈局動畫設置動畫設置。 您可以在res/anim文件夾下使用xml文件創建任何類型的動畫。如果您希望將其重用於其他任何列表視圖,並且還會很好地管理您的代碼,這將會很有幫助。

請讓我知道你是否需要關於在xml中創建動畫的幫助。

5

@user724861已經給出了完美的答案! 我怎麼會發現它的混淆在哪裏把他建議的代碼...我把該代碼放在我的列表片段活動如下..

public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState);   

    AnimationSet set = new AnimationSet(true); 
    Animation animation = new AlphaAnimation(0.0f, 1.0f); 
    animation.setDuration(300); 
    set.addAnimation(animation); 

    /*animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 50.0f, 
      Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 
      0.0f, Animation.RELATIVE_TO_SELF, 0.0f); 
    animation.setDuration(10); 
    set.addAnimation(animation); just comment if you don't want :) */ 
    LayoutAnimationController controller = new LayoutAnimationController(
      set, 0.5f); 

    lv.setLayoutAnimation(controller); 

    adapter = new LazyAdapter(getActivity(), numResults, nodes, tabType); 
    setListAdapter(adapter); 
} 
+1

我已經在我的基本活動中做了這個方法,我只是打電話哇真棒謝謝 –

2

每個項目的動畫不應該開始在同一時間

如果你想要的話,你可以這樣做:

layout_controller.xml:

<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android" 
android:delay="30%" 
android:animation="@anim/scale" /> 

scale.xml:

<set xmlns:android="http://schemas.android.com/apk/res/android" 
android:interpolator="@android:anim/accelerate_interpolator"> 
<scale 
    android:fromXScale="0.1" 
    android:toXScale="1" 
    android:fromYScale="0.1" 
    android:toYScale="1.0" 
    android:duration="800" 
    android:pivotX="10%" 
    android:pivotY="10%" 
    android:startOffset="100" /> 
</set> 

然後在SetListAdapter後您的Java()粘貼以下代碼:

LayoutAnimationController controller = AnimationUtils.loadLayoutAnimation(
this, R.anim.layout_controller); 
getListView().setLayoutAnimation(controller); 

需要注意的是「機器人:延遲」,使動畫開始前一後的延遲。

0

在你的列表視圖活動父佈局只需添加

安卓animatelayoutchanges = 「真」

享受!