In this exercise, animation is applied on a single view (AnalogClock) and on a whole layout (LinearLayout).
The main part is the class ViewAnimation extends Animation; initialize() initialize some setting, applyTransformation() will be called repeatly with difference value of interpolatedTime, between 0 to 1.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/mylayout"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 >
 <Button
  android:id="@+id/start1"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="Start Animation on LinearLayout"
  />
 <Button
  android:id="@+id/start2"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="Start Animation on AnalogClock"
  />
 <AnalogClock
  android:id="@+id/myClock"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  />
</LinearLayout>
AndroidViewAnimation.java
package com.exercise.AndroidViewAnimation;
import android.app.Activity;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.Transformation;
import android.widget.AnalogClock;
import android.widget.Button;
import android.widget.LinearLayout;
public class AndroidViewAnimation extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
     Button myStartButton1 = (Button)this.findViewById(R.id.start1);
     myStartButton1.setOnClickListener(myStartButton1OnClickListener);
     Button myStartButton2 = (Button)this.findViewById(R.id.start2);
     myStartButton2.setOnClickListener(myStartButton2OnClickListener);
    }
    private Button.OnClickListener myStartButton1OnClickListener =
     new Button.OnClickListener(){
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    LinearLayout myAnimation = (LinearLayout)findViewById(R.id.mylayout);
       myAnimation.startAnimation(new ViewAnimation());
   }
    };
    
    private Button.OnClickListener myStartButton2OnClickListener =
     new Button.OnClickListener(){
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    AnalogClock myAnimation = (AnalogClock)findViewById(R.id.myClock);
       myAnimation.startAnimation(new ViewAnimation());
   }
    };
    
    public class ViewAnimation extends Animation
    {
     int centerX, centerY;
     @Override
     public void initialize(int width, int height, int parentWidth,
       int parentHeight)
     {
      super.initialize(width, height, parentWidth, parentHeight);
      setDuration(5000);
      setFillAfter(true);
      setInterpolator(new LinearInterpolator());
      centerX = width/2;
      centerY = height/2;
     }
     
     @Override
  protected void applyTransformation(float interpolatedTime,
    Transformation t) {
   // TODO Auto-generated method stub
      final Matrix matrix = t.getMatrix();
      matrix.setScale(interpolatedTime, interpolatedTime);
  }
    }
}
 Download the files.
Download the files.
No comments:
Post a Comment