1. กำหนด layout หลัก main.xml และ layout ของ item ที่จะใช้แสดงข้อความ รวมทั้งกำหนดข้อความที่ส่วนหัวของหน้าจอไว้ที่ strings.xml
mainl.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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="มงคลถัดไป"
android:id="@+id/btn"
android:onClick="ClickHandler" />
<ViewFlipper
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/flip" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#b0daff"
android:text="มงคลชีวิตข้อที่ ๑ ไม่คบคนพาล: คนพาลหมายถึงผู้ที่คิดชั่ว พูดชั่ว ทำชั่ว ..." />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#b0daff"
android:text="มงคลชีวิตข้อที่ ๒ คบบัณฑิต: บัณฑิต หมายถึงผู้ทรงความรู้ มีปัญญา มีจิตใจที่งาม และมีการดำเนินชีวิตที่ถูกต้อง รู้ดีรู้ชั่ว (ไม่ใช่คนที่จบปริญญาโดยนัย) ..." />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#b0daff"
android:text="มงคลชีวิตข้อที่ ๓: การบูชา คือการแสดงความเคารพบุคคลที่เรานับถือ ยกย่อง เลื่อมใสในบุคคลคนนั้น ..." />
</ViewFlipper>
</LinearLayout>
newslayout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="มงคลถัดไป"
android:id="@+id/btn"
android:onClick="ClickHandler" />
<ViewFlipper
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/flip" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#b0daff"
android:text="มงคลชีวิตข้อที่ ๑ ไม่คบคนพาล: คนพาลหมายถึงผู้ที่คิดชั่ว พูดชั่ว ทำชั่ว ..." />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#b0daff"
android:text="มงคลชีวิตข้อที่ ๒ คบบัณฑิต: บัณฑิต หมายถึงผู้ทรงความรู้ มีปัญญา มีจิตใจที่งาม และมีการดำเนินชีวิตที่ถูกต้อง รู้ดีรู้ชั่ว (ไม่ใช่คนที่จบปริญญาโดยนัย) ..." />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#b0daff"
android:text="มงคลชีวิตข้อที่ ๓: การบูชา คือการแสดงความเคารพบุคคลที่เรานับถือ ยกย่อง เลื่อมใสในบุคคลคนนั้น ..." />
</ViewFlipper>
</LinearLayout>
newslayout.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" >
<ViewFlipper
android:layout_margin="6dip"
android:id="@+id/layoutswitcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_height="wrap_content"
android:id="@+id/firstpanel"
android:paddingTop="10dip"
android:text="my first panel"
android:layout_width="fill_parent"
android:layout_weight="1"
android:textStyle="bold" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_height="wrap_content"
android:id="@+id/secondpanel"
android:paddingTop="10dip"
android:text="my second panel"
android:layout_width="fill_parent"
android:layout_weight="1"
android:textStyle="bold" >
</TextView>
</LinearLayout>
</ViewFlipper>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ViewFlipper
android:layout_margin="6dip"
android:id="@+id/layoutswitcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_height="wrap_content"
android:id="@+id/firstpanel"
android:paddingTop="10dip"
android:text="my first panel"
android:layout_width="fill_parent"
android:layout_weight="1"
android:textStyle="bold" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_height="wrap_content"
android:id="@+id/secondpanel"
android:paddingTop="10dip"
android:text="my second panel"
android:layout_width="fill_parent"
android:layout_weight="1"
android:textStyle="bold" >
</TextView>
</LinearLayout>
</ViewFlipper>
</LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">มงคลชีวิต ๓๘ ประการ</string>
<string name="app_name">มงคลชีวิต ๓๘ ประการ</string>
</resources>
<resources>
<string name="hello">มงคลชีวิต ๓๘ ประการ</string>
<string name="app_name">มงคลชีวิต ๓๘ ประการ</string>
</resources>
2. เขียนโปรแกรม MyViewFlipper.java และ NewsLayout.java
MyViewFlipper.java
package ubu.example.MyViewFlipper;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ViewFlipper;
public class MyViewFlipper extends Activity {
Button btn;
ViewFlipper flip;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.btn);
flip = (ViewFlipper) findViewById(R.id.flip);
flip.setInAnimation(this, android.R.anim.slide_out_right); // when a view is displayed
//flip.setInAnimation(this, android.R.anim.fade_out); // when a view disappears
flip.setOutAnimation(this, android.R.anim.slide_in_left);
}
public void ClickHandler(View v){
flip.showNext();
}
}
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ViewFlipper;
public class MyViewFlipper extends Activity {
Button btn;
ViewFlipper flip;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.btn);
flip = (ViewFlipper) findViewById(R.id.flip);
flip.setInAnimation(this, android.R.anim.slide_out_right); // when a view is displayed
//flip.setInAnimation(this, android.R.anim.fade_out); // when a view disappears
flip.setOutAnimation(this, android.R.anim.slide_in_left);
}
public void ClickHandler(View v){
flip.showNext();
}
}
NewsLayout.java
package ubu.example.MyViewFlipper;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ViewFlipper;
public class NewsLayout extends Activity {
private ViewFlipper vf;
private float oldTouchValue;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newslayout);
vf = (ViewFlipper) findViewById(R.id.layoutswitcher);
}
@Override
public boolean onTouchEvent(MotionEvent touchevent) {
switch (touchevent.getAction()) {
case MotionEvent.ACTION_DOWN: {
oldTouchValue = touchevent.getX();
break;
}
case MotionEvent.ACTION_UP: {
float currentX = touchevent.getX();
if (oldTouchValue < currentX) {
vf.setInAnimation(AnimationHelper.inFromLeftAnimation());
vf.setOutAnimation(AnimationHelper.outToRightAnimation());
vf.showNext();
}
if (oldTouchValue > currentX) {
vf.setInAnimation(AnimationHelper.inFromRightAnimation());
vf.setOutAnimation(AnimationHelper.outToLeftAnimation());
vf.showPrevious();
}
break;
}
}
return false;
}
}
class AnimationHelper{
// for the previous movement
public static Animation inFromRightAnimation() {
Animation inFromRight = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, +1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
inFromRight.setDuration(350);
inFromRight.setInterpolator(new AccelerateInterpolator());
return inFromRight;
}
public static Animation outToLeftAnimation() {
Animation outtoLeft = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, -1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
outtoLeft.setDuration(350);
outtoLeft.setInterpolator(new AccelerateInterpolator());
return outtoLeft;
}
// for the next movement
public static Animation inFromLeftAnimation() {
Animation inFromLeft = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, -1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
inFromLeft.setDuration(350);
inFromLeft.setInterpolator(new AccelerateInterpolator());
return inFromLeft;
}
public static Animation outToRightAnimation() {
Animation outtoRight = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, +1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
outtoRight.setDuration(350);
outtoRight.setInterpolator(new AccelerateInterpolator());
return outtoRight;
}
}
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ViewFlipper;
public class NewsLayout extends Activity {
private ViewFlipper vf;
private float oldTouchValue;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newslayout);
vf = (ViewFlipper) findViewById(R.id.layoutswitcher);
}
@Override
public boolean onTouchEvent(MotionEvent touchevent) {
switch (touchevent.getAction()) {
case MotionEvent.ACTION_DOWN: {
oldTouchValue = touchevent.getX();
break;
}
case MotionEvent.ACTION_UP: {
float currentX = touchevent.getX();
if (oldTouchValue < currentX) {
vf.setInAnimation(AnimationHelper.inFromLeftAnimation());
vf.setOutAnimation(AnimationHelper.outToRightAnimation());
vf.showNext();
}
if (oldTouchValue > currentX) {
vf.setInAnimation(AnimationHelper.inFromRightAnimation());
vf.setOutAnimation(AnimationHelper.outToLeftAnimation());
vf.showPrevious();
}
break;
}
}
return false;
}
}
class AnimationHelper{
// for the previous movement
public static Animation inFromRightAnimation() {
Animation inFromRight = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, +1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
inFromRight.setDuration(350);
inFromRight.setInterpolator(new AccelerateInterpolator());
return inFromRight;
}
public static Animation outToLeftAnimation() {
Animation outtoLeft = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, -1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
outtoLeft.setDuration(350);
outtoLeft.setInterpolator(new AccelerateInterpolator());
return outtoLeft;
}
// for the next movement
public static Animation inFromLeftAnimation() {
Animation inFromLeft = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, -1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
inFromLeft.setDuration(350);
inFromLeft.setInterpolator(new AccelerateInterpolator());
return inFromLeft;
}
public static Animation outToRightAnimation() {
Animation outtoRight = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, +1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
outtoRight.setDuration(350);
outtoRight.setInterpolator(new AccelerateInterpolator());
return outtoRight;
}
}


ไม่มีความคิดเห็น:
แสดงความคิดเห็น