本示例是用ViewFliper實現屏幕切換圖片的效果,手指滑動就實現圖片的切換,主要實現效果如下:
程序結構圖:
ViewFliper中的代碼
- package com.scnu.mc.myviewfliper;
-
- import Android.app.Activity;
- import android.os.Bundle;
- import android.view.MotionEvent;
- import android.widget.ViewFlipper;
-
- public class ViewFliper extends Activity {
-
- ViewFlipper viewFlipper = null;
- float startX;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- init();
- }
-
- private void init() {
- viewFlipper = (ViewFlipper) this.findViewById(R.id.viewFlipper);
- }
-
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN:
- startX = event.getX();
- break;
- case MotionEvent.ACTION_UP:
-
- if (event.getX() > startX) { // 向右滑動
- viewFlipper.setInAnimation(this, R.anim.in_left2right);
- viewFlipper.setOutAnimation(this, R.anim.out_left2right);
- viewFlipper.showNext();
- } else if (event.getX() < startX) { // 向左滑動
- viewFlipper.setInAnimation(this, R.anim.in_right2left);
- viewFlipper.setOutAnimation(this, R.anim.out_right2left);
- viewFlipper.showPrevious();
- }
- break;
- }
-
- return super.onTouchEvent(event);
- }
-
- }