2011-07-28 14 views
29

tengo una vista de lista con reemplazar el método getView para poblarlo. ahora, quiero hacer que cada elemento de la lista se anime o se mueva desde el lado derecho de la pantalla a la izquierda donde normalmente debería aparecer el elemento.cómo hacer animación de traducción para cada elemento listview

la animación de cada artículo no debe comenzar al mismo tiempo, se debe retrasar para el par ms antes de los otros elementos en movimiento ...

bien, esta es mi clase de adaptador:

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

} 

¿alguna idea? :)

ACTUALIZACIÓN

Bueno, sin duda que lo es! LOL: p

acaba de descargar esos ApiDemos "como lo que dijo Farhan" y ustedes encontrarán algún tipo de muestra como LayoutAnimation2 en la vista del paquete.

allí, cada elemento de la lista está siendo animado para poblar hacia abajo por traducir-animación mientras el alfa está cambiando respectivamente.

Esto es lo que hago para mi caso:

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

pongo esto a mi setAdapter función(), ustedes pueden hacer que se vea más acogedor, con efectos de aceleración-deceleración, etc.

: p

+1

noop ninguna idea, pero es posible que desee comprobar el código fuente de las demostraciones de API .... realmente hay algunas cosas geniales sobre la animación también ... – Farhan

Respuesta

1

getView() poblar cada artículos en su actividad. intente crear su animación en getView con Timer.

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

También puede configurar la animación usando el atributo xml animación Disposición de su ListView. Puede crear cualquier tipo de animación utilizando en el archivo xml bajo la carpeta res/anim. Será útil si desea reutilizarlo para cualquier otra vista de lista y también administrará bien su código.

Háganme saber si necesita ayuda con respecto a la creación de animación en xml.

5

@user724861 ha dado la respuesta perfecta !! como sea que encontré es confuso dónde poner el código que ha sugerido ... puse ese código en mi ListFragment activity como sigue ..

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

He hecho este método en mi actividad base y llamo guau increíble gracias –

2

la animación de cada artículo no debe comenzar al mismo tiempo

Si lo desea, puede hacer algo como esto:

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> 

Y luego, en su Java después de SetListAdapter() Pega el siguiente código:

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

nota que "android: retraso" hace animaciones comienzan con retraso después de la anterior.

0

sólo tiene que añadir en su diseño de matriz actividad de vista de lista

Android: animatelayoutchanges = "true"

Enjoy!

Cuestiones relacionadas