Search code examples
androidviewflipper

How can i control images in viewflipper in android?


I have developed an application in which i have implemented ontouchlistener with which i swap images with finger touch.But the problem is that when the images are swapped and last images arrives and when i swap my finger to drag the image it shows the first image.I want to stop my image swapping when the last image comes or it shouldn't go from lat to first what should i do??

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

thanks in advance


Solution

  • In your code create a counter and increment and decrement where you are showNext and showPrevious respectively. something like below

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