java 如何为每个列表视图项制作翻译动画

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6853918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 17:36:19  来源:igfitidea点击:

how to make translation animation for each listview items

javaandroidlistviewanimationtranslation

提问by user724861

i have a listview with override getView method to populate it. now, i want to make each item of the list to animate or moving from the right side of the screen to the left side where the item should normally appear.

我有一个带有覆盖 getView 方法的列表视图来填充它。现在,我想让列表中的每个项目都具有动画效果或从屏幕右侧移动到项目通常应该出现的左侧。

the animation of each item should not start at the same time, it must delay for couple ms before the other items moving...

每个项目的动画不应该同时开始,它必须在其他项目移动之前延迟几毫秒......

well, this is my adapter class:

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

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

}

any idea? :)

任何的想法?:)

UPDATE

更新

Well, surely it is! LOL :p

嗯,肯定是!大声笑:p

just download those ApiDemos "like what have Farhan said" and you guys will find some kind like LayoutAnimation2 sample at the package view.

只需下载那些“就像 Farhan 所说的那样”的 ApiDemos,你们就会在包视图中找到类似 LayoutAnimation2 的示例。

there, each items of the list is being animated to populate downward by translate-animation while the alpha is changing respectively.

在那里,当 alpha 分别改变时,列表中的每个项目都被动画化以通过平移动画向下填充。

here's what i do for my case:

这是我为我的案例所做的:

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

i put this below my setAdapter() function, you guys can make it look more cozy with accelerate-decelerate etc effects.

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

:p

:p

回答by Batta

@user724861has given the perfect answer!! how ever i found it's confusing where to put the code he has suggested...i put that code in my ListFragment activityas follow..

@ user724861给出了完美的答案!!我怎么发现把他建议的代码放在哪里很令人困惑……我把那个代码放在我的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);
}

回答by Dori

Using LayoutAnimations!

使用布局动画!

In the docs you can set via android:layoutAnimationxml attr

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

see here

看这里

回答by Milad Faridnia

the animation of each item should not start at the same time

每个项目的动画不应同时开始

If you want it, you can do something like this:

如果你愿意,你可以做这样的事情:

layout_controller.xml:

layout_controller.xml:

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

scale.xml:

规模.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>

And then in your Java after SetListAdapter() paste the following code:

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

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

note that "android:delay" makes animations start with delay after previous one.

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

回答by Yasir

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

回答by KhairiB

getView() populate each items in your activity. try to create your animation into getView with Timer.

getView() 填充您活动中的每个项目。尝试使用Timer将动画创建到 getView 中。

回答by Umair

You can also set the animation using xml attribute Layout animationof your ListView. You can create any type of animation using in xml file under res/animfolder. It will be helpful if you want to reuse it for any other listview and will also manage your code well.

您还可以使用ListView 的xml 属性布局动画设置动画。您可以使用res/anim文件夹下的 xml 文件创建任何类型的动画。如果您想将它重用于任何其他列表视图并且还可以很好地管理您的代码,这将很有帮助。

Please let me know if you need help regarding animation creation in xml.

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

回答by Himanshu Goyal

Simply add in your listview activity parent layout

只需在您的列表视图活动父布局中添加

Android:animatelayoutchanges="true"

安卓:animatelayoutchanges="true"

Enjoy!

享受!