Jumat, 13 Juli 2012

Android : View Animation

Pembelajaran selanjutnya adalah membuat tampilan Animasi yang terbagi animasi pada jam analog dan animasi pada Linearlayout.
  • Pertama kita buat project baru dengan kriteria
Nama : viewanimation
package name : tutorial.viewanimation
Activity Name : ViewanimationActivity

  • Selanjutnya kita buka file main.xml dan lakukan perubahan seperti berikut:
<?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 Pada LinearLayout"
  />
 <Button
  android:id="@+id/start2"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="Start Animation Pada AnalogClock"
  />
 <AnalogClock
  android:id="@+id/myClock"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  />
</LinearLayout>

  • Selanjutnya buka file ViewanimationActivity.java dan lakukan perubahan seperti berikut.
package tutorial.viewanimation;

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

  • Terakhir kita jalankan aplikasi. . :)

1 komentar:

  1. hhmmm.. pengen nanya nih lagi pusing-pusingnya ngerjain aplikasi andorid,,,,
    pengen nanya,, apakah file animasi bisa di masukkan ke layout..
    misalnya gif, swf, ....
    mohon admin ad awaktu luang untuk membalas komentar saya ini,,,,
    terima kasih sebelumnya....

    BalasHapus