main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Main" >
<ViewFlipper
android:id="@+id/viewFlipper1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<!-- 第一个页面 -->
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="center">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/a3" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="start1" />
</LinearLayout>
<!-- 第二个页面 -->
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="center">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/a4"
android:gravity="center" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="start2" />
</LinearLayout>
<!-- 第三个页面 -->
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="center">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/a5"
android:gravity="center" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="start3" />
</LinearLayout>
<!-- 第四个页面 -->
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="center">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/a6"
android:gravity="center" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="start4" />
</LinearLayout>
</ViewFlipper>
</RelativeLayout>
Main.java
package com.example.myflipper;
import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ViewFlipper;
public class Main extends Activity {
NotificationManager mManager;
Notification mNotification;
private ViewFlipper viewFlipper;
private GestureDetector detector; // 手势检测
Animation leftInAnimation;
Animation leftOutAnimation;
Animation rightInAnimation;
Animation rightOutAnimation;
Button btn1, btn2, btn3, btn4;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
btn1 = (Button) findViewById(R.id.button1);
btn2 = (Button) findViewById(R.id.button2);
btn3 = (Button) findViewById(R.id.button3);
btn4 = (Button) findViewById(R.id.button4);
mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotification = new Notification();
mNotification.icon = R.drawable.a2;
mNotification.when = System.currentTimeMillis();
OnClickListener myClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
String btext = "";
switch (v.getId()) {
case R.id.button1:
btext = "你点了Button1";
break;
case R.id.button2:
btext = "你点了Button2";
break;
case R.id.button3:
btext = "你点了Button3";
break;
case R.id.button4:
btext = "你点了Button4";
break;
}
// TODO Auto-generated method stub
mNotification.tickerText = btext;
mNotification.flags = Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(Main.this,Notice.class);
PendingIntent pIntent=PendingIntent.getActivity(Main.this, 0, intent, 0);
mNotification.setLatestEventInfo(getApplicationContext(), "小心正文", btext,pIntent );
mManager.notify(1, mNotification);
}
};
btn1.setOnClickListener(myClick);
btn2.setOnClickListener(myClick);
btn4.setOnClickListener(myClick);
btn3.setOnClickListener(myClick);
viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper1);
detector = new GestureDetector(this,
new GestureDetector.OnGestureListener() {
@Override
public boolean onSingleTapUp(
MotionEvent e) {
// TODO Auto-generated method
// stub
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method
// stub
}
@Override
public boolean onScroll(MotionEvent e1,
MotionEvent e2,
float distanceX,
float distanceY) {
// TODO Auto-generated method
// stub
return false;
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method
// stub
}
@Override
public boolean onFling(MotionEvent e1,
MotionEvent e2,
float velocityX,
float velocityY) {
if (e1.getX() - e2.getX() > 120) {
viewFlipper.setInAnimation(leftInAnimation);
viewFlipper.setOutAnimation(leftOutAnimation);
viewFlipper.showNext();// 向右滑动
return true;
} else if (e1.getX()
- e2.getY() < -120) {
viewFlipper.setInAnimation(rightInAnimation);
viewFlipper.setOutAnimation(rightOutAnimation);
viewFlipper.showPrevious();// 向左滑动
return true;
}
return false;
}
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method
// stub
return false;
}
});
// 动画效果
leftInAnimation = AnimationUtils.loadAnimation(this,
R.layout.left_in);
leftOutAnimation = AnimationUtils.loadAnimation(this,
R.layout.left_out);
rightInAnimation = AnimationUtils.loadAnimation(this,
R.layout.right_in);
rightOutAnimation = AnimationUtils.loadAnimation(this,
R.layout.right_out);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return this.detector.onTouchEvent(event); // touch事件交给手势处理。
}
}
notice.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Notice" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
Notice.java
package com.example.myflipper;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class Notice extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notice);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
// present.
getMenuInflater().inflate(R.menu.notice, menu);
return true;
}
}
动画配置文件
left_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="100%p"
android:toXDelta="0"
android:duration="600"
/>
<alpha
android:fromAlpha="0.1"
android:toAlpha="1.0"
android:duration="600"
/>
</set>
left_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0"
android:toXDelta="-100%p"
android:duration="600"
/>
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.1"
android:duration="600"
/>
</set>
right_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="-100%p"
android:toXDelta="0"
android:duration="600"
/>
<alpha
android:fromAlpha="0.1"
android:toAlpha="1.0"
android:duration="600"
/>
</set>
right_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0"
android:toXDelta="100%p"
android:duration="600"
/>
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.1"
android:duration="600"
/>
</set>