Selasa, 04 Februari 2014

Android ViewFlipper Tutorial

kali ini saya mauberbagi bagai mana cara membuat android View Flipper bagi temen2 yang mau belajar ketik atau copas script berikut

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:


  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent"
  5.     android:orientation="vertical" >

  6.     <TextView
  7.         android:layout_width="fill_parent"
  8.         android:layout_height="wrap_content"
  9.         android:layout_marginTop="10dp"
  10.         android:gravity="center"
  11.         android:text="View Flipper gambar 1"
  12.         android:textColor="#000099"
  13.         android:textSize="30dp" />

  14.     <ViewFlipper
  15.         android:id="@+id/view_flipper"
  16.         android:layout_width="fill_parent"
  17.         android:layout_height="fill_parent"
  18.         android:layout_margin="6dip" >

  19.         <!-- The child Views/Layout to flip -->


  20.         <!-- Layout 1 for 1st Screen -->

  21.         <LinearLayout
  22.             android:layout_width="fill_parent"
  23.             android:layout_height="fill_parent"
  24.             android:gravity="center"
  25.             android:orientation="vertical" >

  26.             <TextView
  27.                 android:layout_width="wrap_content"
  28.                 android:layout_height="wrap_content"
  29.                 android:layout_marginTop="15dp"
  30.                 android:text="This Is Screen 1"
  31.                 android:textColor="#191975"
  32.                 android:textSize="25dp"
  33.                 android:textStyle="bold" >
  34.             </TextView>

  35.             <ImageView
  36.                 android:id="@+id/imageView1"
  37.                 android:layout_width="442dp"
  38.                 android:layout_height="450dp"
  39.                 android:layout_marginTop="15dp"
  40.                 android:src="@drawable/image1" />
  41.         </LinearLayout>

  42.         <!-- Layout 2 for 2nd Screen -->

  43.         <LinearLayout
  44.             android:layout_width="fill_parent"
  45.             android:layout_height="fill_parent"
  46.             android:gravity="center"
  47.             android:orientation="vertical" >

  48.             <TextView
  49.                 android:layout_width="wrap_content"
  50.                 android:layout_height="wrap_content"
  51.                 android:layout_marginTop="15dp"
  52.                 android:text="This Is Screen 2"
  53.                 android:textColor="#191975"
  54.                 android:textSize="25dp"
  55.                 android:textStyle="bold" >
  56.             </TextView>

  57.             <ImageView
  58.                 android:id="@+id/imageView1"
  59.                 android:layout_width="450dp"
  60.                 android:layout_height="450dp"
  61.                 android:layout_marginTop="15dp"
  62.                 android:src="@drawable/image2" />
  63.         </LinearLayout>
  64.         <!-- Layout 3 for 2nd Screen -->

  65.         <LinearLayout
  66.             android:layout_width="fill_parent"
  67.             android:layout_height="fill_parent"
  68.             android:gravity="center"
  69.             android:orientation="vertical" >

  70.             <TextView
  71.                 android:layout_width="wrap_content"
  72.                 android:layout_height="wrap_content"
  73.                 android:layout_marginTop="15dp"
  74.                 android:text="This Is Screen 2"
  75.                 android:textColor="#191975"
  76.                 android:textSize="25dp"
  77.                 android:textStyle="bold" >
  78.             </TextView>

  79.             <ImageView
  80.                 android:id="@+id/imageView1"
  81.                 android:layout_width="450dp"
  82.                 android:layout_height="450dp"
  83.                 android:layout_marginTop="15dp"
  84.                 android:src="@drawable/image3" />
  85.         </LinearLayout>
  86.     </ViewFlipper>

  87. </LinearLayout>


selanjutnya buat folder bernama anim didalam res dan buat xml baru bernama
 
   res/anim/

  1. in_from_left.xml
  2. in_from_right.xml
  3. out_to_left.xml
  4. out_to_right.xml

dan berikut script dari

res/anim/ in_from_left.xml


  1. <set xmlns:android="http://schemas.android.com/apk/res/android"
  2.     android:shareInterpolator="false">
  3.     <translate
  4.         android:fromXDelta="-100%" android:toXDelta="0%"
  5.            android:fromYDelta="0%" android:toYDelta="0%"
  6.            android:duration="1400" />
  7. </set>

res/anim/  in_from_right.xml

  1. <set xmlns:android="http://schemas.android.com/apk/res/android"
  2.     android:shareInterpolator="false">
  3.     <translate
  4.         android:fromXDelta="100%" android:toXDelta="0%"
  5.            android:fromYDelta="0%" android:toYDelta="0%"
  6.            android:duration="1400" />
  7. </set>

res/anim/ out_to_left.xml


  1. <set xmlns:android="http://schemas.android.com/apk/res/android"
  2.     android:shareInterpolator="false">
  3.       <translate android:fromXDelta="0%" android:toXDelta="-100%"
  4.         android:fromYDelta="0%" android:toYDelta="0%"
  5.         android:duration="1400"/>
  6. </set>

res/anim/ out_to_right.xml


  1. <set xmlns:android="http://schemas.android.com/apk/res/android"
  2.     android:shareInterpolator="false">
  3.       <translate android:fromXDelta="0%" android:toXDelta="100%"
  4.         android:fromYDelta="0%" android:toYDelta="0%"
  5.         android:duration="1400"/>
  6. </set>

kemudian buat activity java nya

src/ ViewFlipperMainActivity

  1. public class ViewFlipperMainActivity extends Activity {
  2. private ViewFlipper viewFlipper;
  3. private float lastX;

  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.view_flipper_main);
  8. viewFlipper = (ViewFlipper) findViewById(R.id.view_flipper);
  9. }


  10. public boolean onTouchEvent(MotionEvent touchevent) {
  11. switch (touchevent.getAction()) {

  12. case MotionEvent.ACTION_DOWN: {
  13. lastX = touchevent.getX();
  14. break;
  15. }
  16. case MotionEvent.ACTION_UP: {
  17. float currentX = touchevent.getX();

  18. if (lastX < currentX) {
  19. if (viewFlipper.getDisplayedChild() == 0)
  20. break;

  21. viewFlipper.setInAnimation(this, R.anim.in_from_left);
  22. viewFlipper.setOutAnimation(this, R.anim.out_to_right);
  23. viewFlipper.showNext();
  24. }

  25. if (lastX > currentX) {
  26. if (viewFlipper.getDisplayedChild() == 1)
  27. break;
  28. viewFlipper.setInAnimation(this, R.anim.in_from_right);
  29. viewFlipper.setOutAnimation(this, R.anim.out_to_left);
  30. viewFlipper.showPrevious();
  31. }
  32. break;
  33. }
  34. }
  35. return false;
  36. }

  37. }
bila sudah hasilnya seperti berikut
                                   




Bila anda ingin mendownload Source Codenya click link di bawah
ViewFlipper Source Code

Terimakasih telah membaca


Tidak ada komentar:

Posting Komentar

Designed ByOra responsive ora popo