如何在android中的viewflipper中控制图像?

时间:2011-06-15 07:25:29

标签: android viewflipper

我已经开发了一个应用程序,其中我实现了ontouchlistener,我用手指触摸交换图像。但问题是当图像被交换并且最后的图像到达时以及当我交换手指拖动时它显示第一张图像。我想在最后一张图片出现时停止我的图像交换,或者它不应该从lat转到第一张应该怎么办?

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);
   }
}

提前致谢

2 个答案:

答案 0 :(得分:2)

在您的代码中创建counter,并分别在showNextshowPrevious处递增和递减。类似下面的内容

                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++;
                     }
                }

答案 1 :(得分:1)

检查当前视图是否是最后一个视图,如果它是最后一个视图,则不要转到上一个视图 你的逻辑中有一点改变,使用下面的代码。

if(vf.getId() != vf.getChildAt(vf.getChildCount()-1).getId())

由于 迪帕克