如何在Android的Viewflipper中控制图片?

| 我开发了一个应用程序,我实现了
ontouchlistener
,我可以用手指触摸交换图像,但是问题是当交换图像并到达最后一个图像时,当我交换手指以拖动图像时它会显示第一个图像。我要在最后一张图片出现时停止我的图片交换,否则它不应该从lat转到first,该怎么办?
public class Touch extends Activity implements OnTouchListener {
 float downXValue;
   private static final String TAG = \"Touch\";
   // These matrices will be used to move and zoom image
   Matrix matrix = new Matrix();
   Matrix savedMatrix = new Matrix();

   // We can be in one of these 3 states
   static final int NONE = 0;
   static final int DRAG = 1;
   static final int ZOOM = 2;
   int mode = NONE;                                

   // Remember some things for zooming
   PointF start = new PointF();
   PointF mid = new PointF();
   float oldDist = 1f;
   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Set main.XML as the layout for this Activity
        setContentView(R.layout.main);

        // Add these two lines
        LinearLayout lay = (LinearLayout) findViewById(R.id.lays);
        lay.setOnTouchListener((OnTouchListener) this);
   }

   @Override
   public boolean onTouch(View arg0, MotionEvent arg1) {

        // Get the action that was done on this touch event
        switch (arg1.getAction())
        {
            case MotionEvent.ACTION_DOWN:
            {
                // store the X value when the user\'s finger was pressed down
                downXValue = arg1.getX();
                break;
            }

            case MotionEvent.ACTION_UP:
            {
                // Get the X value when the user released his/her finger
                float currentX = arg1.getX();            

                // going backwards: pushing stuff to the right
                if (downXValue < currentX)
                {
                    // Get a reference to the ViewFlipper
                     ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
                     // Set the animation
                    //  vf.setAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
                      // Flip!
                      vf.showPrevious();
                }

                // going forwards: pushing stuff to the left
                if (downXValue > currentX)
                {
                    // Get a reference to the ViewFlipper
                    ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
                     // Set the animation
               //    vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
                      // Flip!
                     vf.showNext();
                }
                break;
            }

      case MotionEvent.ACTION_MOVE:
         if (mode == DRAG) {
            // ...
            matrix.set(savedMatrix);
            matrix.postTranslate(arg1.getX() - start.x,
                  arg1.getY() - start.y);
         }
         else if (mode == ZOOM) {
            float newDist = spacing(arg1);
            Log.d(TAG, \"newDist=\" + newDist);
            if (newDist > 10f) {
               matrix.set(savedMatrix);
               float scale = newDist / oldDist;
               matrix.postScale(scale, scale, mid.x, mid.y);
            }
         }
         break;
      }

     // view.setImageMatrix(matrix);
      return true; // indicate event was handled
   }



/** Show an event in the LogCat view, for debugging */
   private void dumpEvent(MotionEvent arg1) {
      String names[] = { \"DOWN\", \"UP\", \"MOVE\", \"CANCEL\", \"OUTSIDE\",
            \"POINTER_DOWN\", \"POINTER_UP\", \"7?\", \"8?\", \"9?\" };
      StringBuilder sb = new StringBuilder();
      int action = arg1.getAction();
      int actionCode = action & MotionEvent.ACTION_MASK;
      sb.append(\"event ACTION_\").append(names[actionCode]);
      if (actionCode == MotionEvent.ACTION_POINTER_DOWN
            || actionCode == MotionEvent.ACTION_POINTER_UP) {
         sb.append(\"(pid \").append(
               action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
         sb.append(\")\");
      }
      sb.append(\"[\");
      for (int i = 0; i < arg1.getPointerCount(); i++) {
         sb.append(\"#\").append(i);
         sb.append(\"(pid \").append(arg1.getPointerId(i));
         sb.append(\")=\").append((int) arg1.getX(i));
         sb.append(\",\").append((int) arg1.getY(i));
         if (i + 1 < arg1.getPointerCount())
            sb.append(\";\");
      }
      sb.append(\"]\");
      Log.d(TAG, sb.toString());
   }

   /** Determine the space between the first two fingers */
   private float spacing(MotionEvent event) {
      float x = event.getX(0) - event.getX(1);
      float y = event.getY(0) - event.getY(1);
      return FloatMath.sqrt(x * x + y * y);
   }

   /** Calculate the mid point of the first two fingers */
   private void midPoint(PointF point, MotionEvent event) {
      float x = event.getX(0) + event.getX(1);
      float y = event.getY(0) + event.getY(1);
      point.set(x / 2, y / 2);
   }
}
提前致谢     
已邀请:
        在您的代码中创建一个
counter
,并递增和递减,分别为
showNext
showPrevious
。像下面这样
                if (downXValue < currentX)
                {
                     ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
                     if(counter > 0)
                     {
                          vf.showPrevious();
                          counter--;
                     }
                }

                // going forwards: pushing stuff to the left
                if (downXValue > currentX)
                {
                    // Get a reference to the ViewFlipper
                    ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
                     if(counter < 20)
                     {
                          vf.showNext();
                          counter++;
                     }
                }
    
        检查当前视图是否为最后一个视图,如果当前视图为最后一个视图,则不要转到上一个视图 您使用以下代码在逻辑上进行了一些更改。
if(vf.getId() != vf.getChildAt(vf.getChildCount()-1).getId())
谢谢 迪帕克     

要回复问题请先登录注册