2012-02-20 80 views
0

我想創建一個具有頁面翻轉功能的應用程序。它應該看起來像Pape Flipping Demo 在菜單選項有「當前頁面移動」功能,我希望我的應用程序具有該功能,在該視頻00:33詳細信息。任何機構都有關於它的教程或示例源代碼?請幫幫我。如何在Android上創建「頁面翻轉」

回答

0

這個工程100%...

LinearLayout mainLayout = (LinearLayout) findViewById(R.id.main_layout); 
    mainLayout.setOnTouchListener(this); 
    flipper = (ViewFlipper) findViewById(R.id.flipper); 
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    int layouts[] = new int[]{ R.layout.first, R.layout.second, R.layout.third, R.layout.fourth, 
      R.layout.fifth, R.layout.sixth, R.layout.seventh, R.layout.eighth, R.layout.nineth, R.layout.tenth, 
      R.layout.eleventh, R.layout.twelveth, R.layout.thirteen, R.layout.fourteenth }; 
    for (int layout : layouts) 
     flipper.addView(inflater.inflate(layout, null)); 

和翻轉....

public boolean onTouch(View view, MotionEvent event) 
{ 
    switch (event.getAction()) 
    { 
    case MotionEvent.ACTION_DOWN: 
     fromPosition = event.getX(); 
     break; 
    case MotionEvent.ACTION_UP: 
     float toPosition = event.getX(); 
     if (fromPosition > toPosition) 
     { 
      flipper.setInAnimation(AnimationUtils.loadAnimation(this,R.anim.go_next_in)); 
      flipper.setOutAnimation(AnimationUtils.loadAnimation(this,R.anim.go_next_out)); 
      flipper.showNext(); 
     } 
     else if (fromPosition < toPosition) 
     { 
      flipper.setInAnimation(AnimationUtils.loadAnimation(this,R.anim.go_prev_in)); 
      flipper.setOutAnimation(AnimationUtils.loadAnimation(this,R.anim.go_prev_out)); 
      flipper.showPrevious(); 
     } 
    default: 
     break; 
    } 
    return true; 
} 
+0

謝謝你的答案,但它是不完全是我想要的,請再看視頻。我的意思是:在選項菜單中,用戶選擇「當前頁面移動」,並顯示我想要的(在00:33),我希望我的應用程序具有該功能。 – NemoDo 2012-02-20 04:48:40

+0

所以,只需通過按鈕動畫添加到菜單欄,就像在視頻中一樣......沒有問題,我認爲..)加上答案))) – timonvlad 2012-02-20 04:57:36

0

這裏是從視頻的代碼...... 這是確切的代碼

你可以找到這裏的一切http://typea.info/blg/glob/2010/07/androidx06ht-desire-4.html

package info.typea.viewflipperapp; 

import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 
import android.view.animation.AnimationUtils; 
import android.widget.LinearLayout; 
import android.widget.ViewFlipper; 

public class ViewFilpperAppActivity extends Activity implements OnTouchListener { 
    private static final String LOG_TAG = "MyApp"; 

    /* コンテキストメニューID */ 
    private static final int MENU_VIEWFLIP_ANIM = Menu.FIRST; 
    private static final int MENU_MOVE_NEXT  = Menu.FIRST + 1; 
    private static final int MENU_MOVE_CURRENT = Menu.FIRST + 2; 

    /* ページ切り替え動作モード */ 
    private int procMode = MENU_VIEWFLIP_ANIM; 

    /* ページを切り替える移動量閾値 */ 
    private final int SWITCH_THRESHOLD = 10; 

    /* ページ切り替えモード */ 
    private final int FLIPMODE_NOMOVE = 0; 
    private final int FLIPMODE_NEXT = 1; 
    private final int FLIPMODE_PREV = -1; 
    private int flipMode = FLIPMODE_NOMOVE; 

    private ViewFlipper vf = null; 
    private View currentView = null; 
    private View nextView  = null; 
    private View prevView  = null; 

    /* ページのIDと順序を管理 */ 
    private int viewOrder[] = null; 
    private int curIdx = -1; 
    private int preIdx = -1; 
    private int nxtIdx = -1; 

    private int movePageThreshold = 0; 
    private float startX; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     LinearLayout layout = (LinearLayout) findViewById(R.id.layout_main); 
     layout.setOnTouchListener(this); 

     vf = (ViewFlipper) findViewById(R.id.details); 

     /* 
     * レイアウトを実行時にインスタンス化し ViewFlipperに格納 
     * 同時に順序をIDで管理する 
     * addView(v,idx) で管理できると思いきや、実行時に View と Index の対応が変わってしまうようだ 
     */ 
     LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     int layouts[] = new int[] {R.layout.layout_page1, 
            R.layout.layout_page2, 
            R.layout.layout_page3}; 
     viewOrder = new int[layouts.length]; 
     for (int i=0; i<layouts.length; i++) { 
      View vw = vi.inflate(layouts[i], null); 
      // ViewFlipper に格納 
      vf.addView(vw); 
      // ID管理用配列に保持 
      viewOrder[i] = vw.getId(); 
     } 
    } 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // コンテキストメニューで、ページめくりの実裝を切り替える 
     super.onCreateOptionsMenu(menu); 
     menu.add(0, MENU_VIEWFLIP_ANIM, 0, R.string.vewflipper_anim); 
     menu.add(0, MENU_MOVE_CURRENT, 0, R.string.current_page_move); 
     menu.add(0, MENU_MOVE_NEXT,  0, R.string.next_prev_page_move); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     procMode = item.getItemId(); 

     // ViewFlipper に設定したアニメーションを解除 
     vf.setInAnimation(null); 
     vf.setOutAnimation(null); 

     return true; 
    } 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     boolean ret = false; 

     // ページめくりの実裝を切り替える 
     switch(procMode) { 
     case MENU_VIEWFLIP_ANIM: 
      ret = pageFlipWithSimpleAnimation(v, event); 
      break; 
     case MENU_MOVE_CURRENT: 
      ret = pageFlipWithFingerMoveCurrent(v, event); 
      break; 
     case MENU_MOVE_NEXT: 
      ret = pageFlipWithFingerMoveNext(v, event); 
      break; 
     } 
     return ret; 
    } 

    /** 
    * ViewFlipper と アニメーションを利用して単純にページをめくる 
    * @param v 
    * @param event 
    * @return 
    */ 
    public boolean pageFlipWithSimpleAnimation(View v, MotionEvent event) { 
     switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: 
      startX = event.getX(); 
      break; 
     case MotionEvent.ACTION_UP: 
      float currentX = event.getX(); 
      if (this.startX > currentX) { 
       vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in)); 
       vf.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out)); 
       vf.showNext(); 
      } 
      if (this.startX < currentX) { 
       vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_out)); 
       vf.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_in)); 
       vf.showPrevious(); 
      } 
     default: 
      break; 
     } 
     return true; 
    } 

    /** 
    * 前のページおよび次のページを移動することによってページをめくる 
    * @param v 
    * @param event 
    * @return 
    */ 
    public boolean pageFlipWithFingerMoveNext(View v, MotionEvent event) { 
     float currentX = event.getX(); 

     switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: 
      startX = event.getX(); 

      currentView = vf.getCurrentView(); 
      movePageThreshold = (currentView.getWidth()/5); 

      int viewCount = viewOrder.length; 
      for (int i=0; i<viewCount; i++) { 
       //Log.i("MyApp", "ord=id:" + viewOrder[i] + "==" + cv.getId()); 
       if (viewOrder[i] == currentView.getId()) { 
        curIdx = i; 
        break; 
       } 
      } 

      if (curIdx >= 0) { 
       preIdx = curIdx - 1; 
       nxtIdx = curIdx + 1; 
       preIdx = (preIdx < 0  )?viewCount - 1:preIdx; 
       nxtIdx = (nxtIdx >= viewCount)?0   :nxtIdx; 

       prevView = vf.findViewById(viewOrder[preIdx]); 
       nextView = vf.findViewById(viewOrder[nxtIdx]); 
      } 

      Log.i(LOG_TAG, 
        String.format("Pre=%d(%d),Cur=%d(%d),Nxt=%d(%d)" 
            ,preIdx,prevView.getId() 
            ,curIdx,currentView.getId() 
            ,nxtIdx,nextView.getId())); 

      break; 
     case MotionEvent.ACTION_MOVE: 
      int travelDistanceX = (int)(currentX - this.startX); 
      int fingerPosX = (int)currentX; 

      if (flipMode == FLIPMODE_NOMOVE) { 
       if (travelDistanceX > SWITCH_THRESHOLD) { 
        flipMode = FLIPMODE_PREV; 
       } else if 
        (travelDistanceX < (SWITCH_THRESHOLD * -1) ) { 
        flipMode = FLIPMODE_NEXT; 
       } else { 
        flipMode = FLIPMODE_NOMOVE; 
       } 
      } 

      if (flipMode == FLIPMODE_PREV) { 
       prevView.layout(fingerPosX - prevView.getWidth(), 
          prevView.getTop(), 
          fingerPosX , 
          prevView.getBottom()); 

       vf.bringChildToFront(prevView); 
       prevView.setVisibility(View.VISIBLE); 

      } 
      if (flipMode == FLIPMODE_NEXT) { 
       nextView.layout(fingerPosX , 
          nextView.getTop(), 
          fingerPosX + currentView.getWidth() + nextView.getWidth(), 
          nextView.getBottom()); 

       vf.bringChildToFront(nextView); 
       nextView.setVisibility(View.VISIBLE); 
      } 
      break; 

     case MotionEvent.ACTION_UP: 

      int activeIdx = -1; 
      if ((this.startX - currentX) > movePageThreshold) { 
       activeIdx = nxtIdx; 
      }else if 
       ((this.startX - currentX) < (movePageThreshold * -1)) { 
       activeIdx = preIdx; 
      } else { 
       activeIdx = curIdx; 
      } 
      int activeId = viewOrder[activeIdx]; 
      for(int i=0; i<vf.getChildCount(); i++) { 
       // Log.i("MyApp",String.format("vf_id:%d,sel_id:%d",vf.getChildAt(i).getId(),activeId)); 
       if (vf.getChildAt(i).getId() == activeId) { 
        vf.setDisplayedChild(i); 
        break; 
       } 
      } 
      flipMode = 0; 
     default: 
      break; 
     } 
     return true; 
    } 

    /** 
    * 現在のページを移動することによってページをめくる 
    * @param v 
    * @param event 
    * @return 
    */ 
    public boolean pageFlipWithFingerMoveCurrent(View v, MotionEvent event) { 
     float currentX = event.getX(); 

     switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: 
      startX = event.getX(); 

      currentView = vf.getCurrentView(); 
      movePageThreshold = (currentView.getWidth()/5); 

      int viewCount = viewOrder.length; 
      for (int i=0; i<viewCount; i++) { 
       //Log.i("MyApp", "ord=id:" + viewOrder[i] + "==" + cv.getId()); 
       if (viewOrder[i] == currentView.getId()) { 
        curIdx = i; 
        break; 
       } 
      } 

      if (curIdx >= 0) { 
       preIdx = curIdx - 1; 
       nxtIdx = curIdx + 1; 
       preIdx = (preIdx < 0  )?viewCount - 1:preIdx; 
       nxtIdx = (nxtIdx >= viewCount)?0   :nxtIdx; 

       prevView = vf.findViewById(viewOrder[preIdx]); 
       nextView = vf.findViewById(viewOrder[nxtIdx]); 
      } 

      Log.i(LOG_TAG, 
        String.format("Pre=%d(%d),Cur=%d(%d),Nxt=%d(%d)" 
            ,preIdx,prevView.getId() 
            ,curIdx,currentView.getId() 
            ,nxtIdx,nextView.getId())); 

      break; 
     case MotionEvent.ACTION_MOVE: 
      int travelDistanceX = (int)(currentX - this.startX); 
      int fingerPosX = (int)currentX; 

      if (flipMode == FLIPMODE_NOMOVE) { 
       if (travelDistanceX > SWITCH_THRESHOLD) { 
        flipMode = FLIPMODE_PREV; 
       } else if 
        (travelDistanceX < (SWITCH_THRESHOLD * -1) ) { 
        flipMode = FLIPMODE_NEXT; 
       } else { 
        flipMode = FLIPMODE_NOMOVE; 
       } 
      } 

      if (flipMode == FLIPMODE_PREV) { 
       currentView.layout(fingerPosX, 
          currentView.getTop(), 
          fingerPosX + currentView.getWidth() , 
          currentView.getBottom()); 

       vf.bringChildToFront(currentView); 
       prevView.setVisibility(View.VISIBLE); 

      } 
      if (flipMode == FLIPMODE_NEXT) { 
       currentView.layout(fingerPosX - currentView.getWidth() , 
          currentView.getTop(), 
          fingerPosX, 
          currentView.getBottom()); 

       vf.bringChildToFront(currentView); 
       nextView.setVisibility(View.VISIBLE); 
      } 
      break; 

     case MotionEvent.ACTION_UP: 

      int activeIdx = -1; 
      if ((this.startX - currentX) > movePageThreshold) { 
       activeIdx = nxtIdx; 
      }else if 
       ((this.startX - currentX) < (movePageThreshold * -1)) { 
       activeIdx = preIdx; 
      } else { 
       activeIdx = curIdx; 
      } 
      int activeId = viewOrder[activeIdx]; 
      for(int i=0; i<vf.getChildCount(); i++) { 
       // Log.i("MyApp",String.format("vf_id:%d,sel_id:%d",vf.getChildAt(i).getId(),activeId)); 
       if (vf.getChildAt(i).getId() == activeId) { 
        vf.setDisplayedChild(i); 
        break; 
       } 
      } 
      flipMode = 0; 
     default: 
      break; 
     } 
     return true; 
    } 
}