Java 如何为每个listview项目制作翻译动画

Java 如何为每个listview项目制作翻译动画,java,android,listview,animation,translation,Java,Android,Listview,Animation,Translation,我有一个带有override getView方法的listview来填充它。 现在,我想让列表中的每一项都从右侧设置动画或移动 屏幕左侧项目正常显示的位置 每个项目的动画不应同时启动,它必须在其他项目移动之前延迟几毫秒 这是我的适配器类: public class MyAdapter extends ArrayAdapter<String>{ private Context context; private String[] info; public My

我有一个带有override 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");
            }
        }
    };

}
我把它放在setAdapter()函数下面, 你们可以通过加速减速等效果让它看起来更舒适


:p

getView()填充活动中的每个项目。尝试使用布局动画在getView中创建动画。

在文档中,您可以通过xml属性进行设置

package com.Animation1;
导入android.app.ListActivity;
导入android.os.Bundle;
导入android.view.view;
导入android.view.animation.animation;
导入android.view.animation.AnimationUtils;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
导入android.widget.AdapterView;
导入java.util.ArrayList;
公共类Animation1Activity扩展了ListActivity实现
AdapterView.OnItemSelectedListener{
动画动画=空;
/**在首次创建活动时调用*/
@凌驾
创建公共空间(捆绑冰柱){
超级冰柱;
anim=AnimationUtils.loadAnimation(this,R.anim.放大);
setContentView(R.layout.main);
ListView v=getListView();//设置项目选择侦听器
v、 setOnItemSelectedListener(this);//请参阅下面的OnItemSelectedListener方法
ArrayList items=新建ArrayList();
项目。添加(“红色”);
项目。添加(“灰色”);
项目。添加(“青色”);
项目。添加(“蓝色”);
项目。添加(“绿色”);
项目。添加(“黄色”);
项目。添加(“黑色”);
项目。添加(“白色”);
ArrayAdapter itemsAdapter=新的ArrayAdapter(此,R.layout.row,items);
setListAdapter(itemsAdapter);
}
仅受保护的空列表项单击(列表视图l,
观点五,
int位置,
长id){
v、 startAnimation(anim);
}
//---AdapterView.OnItemSelectedListener方法--
已选择公共无效项(AdapterView父项,
观点五,
int位置,
长id){
v、 startAnimation(anim);
}
未选择的公共无效(AdapterView父项){}
}

您还可以使用ListView的xml属性布局动画设置动画。 您可以在res/anim文件夹下的xml文件中创建任何类型的动画。如果您想将其用于任何其他listview,它将非常有用,并且还可以很好地管理您的代码


如果您需要有关xml动画创建的帮助,请告诉我。

@用户724861给出了完美的答案!! 然而,我发现将他建议的代码放在何处令人困惑……我将该代码放在我的列表片段活动中,如下所示

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);
}
每个项目的动画不应同时启动

如果您需要,可以这样做:

layout_controller.xml:


scale.xml:


然后在Java after SetListAdapter()中粘贴以下代码:

layoutaimationcontroller=AnimationUtils.loadlayoutaimation(
这是R.anim.布局(控制器);
getListView().SetLayoutImation(控制器);

请注意,“android:delay”使动画在上一个动画之后以delay开始。

只需添加到listview活动父布局中即可

Android:animatelayoutchanges=“true”


享受吧

不知道,但您可能想检查api演示的源代码。。。。真的,关于动画也有一些很棒的东西。我在我的基本活动中使用了这种方法,我称之为“哇,太棒了,谢谢”,我用它来制作arrayList的动画。谢谢
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) {}
}
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);
}