如何为每个listview项目制作翻译animation

我有一个列表视图重写getView方法来填充它。 现在,我想让列表中的每个项目都动起来,或者从屏幕的右侧移动到项目通常应该出现的左侧。

每个项目的animation不应该在同一时间开始,它必须延迟几秒之前其他项目移动…

好吧,这是我的适配器类:

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

那么,当然是! 大声笑:第

只需下载那些ApiDemos,就像Farhan说的那样,你们会在包视图中find一些像LayoutAnimation2样本。

在那里,列表中的每个项目都被animation化,以便在Alpha变化的同时通过翻译animation向下填充。

这是我为我的情况做的:

 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

Solutions Collecting From Web of "如何为每个listview项目制作翻译animation"

@ user724861已经给出了完美的答案! 我怎么会发现它混淆了在哪里把他build议的代码…我把这个代码放在我的ListFragment活动如下..

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

使用LayoutAnimations!

在文档中,您可以通过android:layoutAnimation xml attr进行设置

看这里

每个项目的animation不应该同时开始

如果你想要的话,你可以做这样的事情:

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> 

然后在你的Java之后的SetListAdapter()粘贴下面的代码:

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

请注意,“android:delay”会使animation以前一个延迟开始。

getView()填充您的活动中的每个项目。 尝试使用Timer将您的animation创build为getView。

 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) {} } 

您还可以使用ListView的xml属性布局animation设置animation。 您可以使用res / anim文件夹下的xml文件创build任何types的animation。 如果您想要将其重用于任何其他列表视图,并且也将很好地pipe理您的代码,将会很有帮助。

请让我知道如果你需要帮助在xml中创buildanimation。

只需添加你的列表视图活动父布局

安卓:animatelayoutchanges = “真”

请享用!