1. buat android projeck baru dengan nama ViewFlipper
2. untuk activity name nya gunakan nama berikut "ViewFlipperMainActivity.java"
untuk Layout name nya gunakan nama berikut "view_flipper_main.xml"
res/layout/view_flipper_main.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="10dp"
- android:gravity="center"
- android:text="View Flipper gambar 1"
- android:textColor="#000099"
- android:textSize="30dp" />
- <ViewFlipper
- android:id="@+id/view_flipper"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_margin="6dip" >
- <!-- The child Views/Layout to flip -->
- <!-- Layout 1 for 1st Screen -->
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:gravity="center"
- android:orientation="vertical" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="15dp"
- android:text="This Is Screen 1"
- android:textColor="#191975"
- android:textSize="25dp"
- android:textStyle="bold" >
- </TextView>
- <ImageView
- android:id="@+id/imageView1"
- android:layout_width="442dp"
- android:layout_height="450dp"
- android:layout_marginTop="15dp"
- android:src="@drawable/image1" />
- </LinearLayout>
- <!-- Layout 2 for 2nd Screen -->
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:gravity="center"
- android:orientation="vertical" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="15dp"
- android:text="This Is Screen 2"
- android:textColor="#191975"
- android:textSize="25dp"
- android:textStyle="bold" >
- </TextView>
- <ImageView
- android:id="@+id/imageView1"
- android:layout_width="450dp"
- android:layout_height="450dp"
- android:layout_marginTop="15dp"
- android:src="@drawable/image2" />
- </LinearLayout>
- <!-- Layout 3 for 2nd Screen -->
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:gravity="center"
- android:orientation="vertical" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="15dp"
- android:text="This Is Screen 2"
- android:textColor="#191975"
- android:textSize="25dp"
- android:textStyle="bold" >
- </TextView>
- <ImageView
- android:id="@+id/imageView1"
- android:layout_width="450dp"
- android:layout_height="450dp"
- android:layout_marginTop="15dp"
- android:src="@drawable/image3" />
- </LinearLayout>
- </ViewFlipper>
- </LinearLayout>
selanjutnya buat folder bernama anim didalam res dan buat xml baru bernama
res/anim/
- in_from_left.xml
- in_from_right.xml
- out_to_left.xml
- out_to_right.xml
dan berikut script dari
res/anim/ in_from_left.xml
- <set xmlns:android="http://schemas.android.com/apk/res/android"
- android:shareInterpolator="false">
- <translate
- android:fromXDelta="-100%" android:toXDelta="0%"
- android:fromYDelta="0%" android:toYDelta="0%"
- android:duration="1400" />
- </set>
res/anim/ in_from_right.xml
- <set xmlns:android="http://schemas.android.com/apk/res/android"
- android:shareInterpolator="false">
- <translate
- android:fromXDelta="100%" android:toXDelta="0%"
- android:fromYDelta="0%" android:toYDelta="0%"
- android:duration="1400" />
- </set>
res/anim/ out_to_left.xml
- <set xmlns:android="http://schemas.android.com/apk/res/android"
- android:shareInterpolator="false">
- <translate android:fromXDelta="0%" android:toXDelta="-100%"
- android:fromYDelta="0%" android:toYDelta="0%"
- android:duration="1400"/>
- </set>
res/anim/ out_to_right.xml
- <set xmlns:android="http://schemas.android.com/apk/res/android"
- android:shareInterpolator="false">
- <translate android:fromXDelta="0%" android:toXDelta="100%"
- android:fromYDelta="0%" android:toYDelta="0%"
- android:duration="1400"/>
- </set>
kemudian buat activity java nya
src/ ViewFlipperMainActivity
- public class ViewFlipperMainActivity extends Activity {
- private ViewFlipper viewFlipper;
- private float lastX;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.view_flipper_main);
- viewFlipper = (ViewFlipper) findViewById(R.id.view_flipper);
- }
- public boolean onTouchEvent(MotionEvent touchevent) {
- switch (touchevent.getAction()) {
- case MotionEvent.ACTION_DOWN: {
- lastX = touchevent.getX();
- break;
- }
- case MotionEvent.ACTION_UP: {
- float currentX = touchevent.getX();
- if (lastX < currentX) {
- if (viewFlipper.getDisplayedChild() == 0)
- break;
- viewFlipper.setInAnimation(this, R.anim.in_from_left);
- viewFlipper.setOutAnimation(this, R.anim.out_to_right);
- viewFlipper.showNext();
- }
- if (lastX > currentX) {
- if (viewFlipper.getDisplayedChild() == 1)
- break;
- viewFlipper.setInAnimation(this, R.anim.in_from_right);
- viewFlipper.setOutAnimation(this, R.anim.out_to_left);
- viewFlipper.showPrevious();
- }
- break;
- }
- }
- return false;
- }
- }
Bila anda ingin mendownload Source Codenya click link di bawah
ViewFlipper Source Code
Terimakasih telah membaca
Tidak ada komentar:
Posting Komentar