view animation code

satya - Thursday, February 05, 2009 3:52:19 PM

ViewAnimationActivity


public class LayoutAnimationActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_layout);
        setupListView();
        this.setupButton();
    }
    private void setupListView()
    {
         String[] listItems = new String[] {
               "Item 1", "Item 2", "Item 3",
               "Item 4", "Item 5", "Item 6",
         };
         
         ArrayAdapter<String> listItemAdapter = 
             new ArrayAdapter<String>(this
                     ,android.R.layout.simple_list_item_1
                     ,listItems);
         ListView lv = (ListView)this.findViewById(R.id.list_view_id);
         lv.setAdapter(listItemAdapter);
    }
    private void setupButton()
    {
       Button b = (Button)this.findViewById(R.id.btn_animate);
       b.setOnClickListener(
           new Button.OnClickListener(){
             public void onClick(View v)
             {
                animateListView();
             }
           });
    }
    private void animateListView()
    {
      Log.d("h","animate list view");
       ListView lv = (ListView)this.findViewById(R.id.list_view_id);
       float cx = (float)(lv.getWidth()/2.0);
       float cy = (float)(lv.getHeight()/2.0);
       lv.startAnimation(new ViewAnimation(cx,cy));
    }
}

satya - Thursday, February 05, 2009 3:53:22 PM

ViewAnimation


public class ViewAnimation extends Animation {
   float centerX, centerY;
    public ViewAnimation(float cx, float cy)
    {
       centerX = cx;
       centerY = cy;
    }

    @Override
    public void initialize(int width, int height, int parentWidth, int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
        Log.d("d","width:" + width);
        Log.d("d","height:" + height);
        Log.d("d","pwidth:" + parentWidth);
        Log.d("d","pheight:" + parentHeight);
        setDuration(2500);
        setFillAfter(true);
        setInterpolator(new LinearInterpolator());
        
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
       applyTransformationNew(interpolatedTime,t);
    }
    
    protected void applyTransformationNew(float interpolatedTime, Transformation t) 
    {
       //Log.d("d","transform:" + interpolatedTime);
        final Matrix matrix = t.getMatrix();
        matrix.setScale(interpolatedTime, interpolatedTime);
        matrix.preTranslate(-centerX, -centerY);
        matrix.postTranslate(centerX, centerY);
    }
}

satya - Thursday, February 05, 2009 3:54:17 PM

layout xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    
<Button 
   android:id="@+id/btn_animate"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Start Animation"
/>

    <ListView
        android:id="@+id/list_view_id"
        android:persistentDrawingCache="animation|scrolling"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />
</LinearLayout>