Search code examples
androidandroid-layoutcrop

Movable image within a circle


enter image description here

In this image, circle is static one. Rectangle image is in behind of this circle. I need to move,zoom out, zoom in and pan the rectangle image within this circle. How to achieve this?

Give me suggestions how to do this.

Thanks in advance


Solution

  • Follow the Guidelines to do so:

    1. Take Relative Layout
    2. Add ImageView in This layout.
    3. Now add Circle Shape Image in it.

    example:

          <RelativeLayout>
             <ImageView /> // image which have to apply movement.
             <ImaeView />  // circle shape
          </RelativeLayout>
    

    Then Apply below code to use movement of Imageview.

    Implements Activity with OnTouchListener, Then

             img.setonTouchListener(this);
    
    
    
             @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
         view= (ImageView) v;
    
    
            // make the image scalable as a matrix
            view.setScaleType(ImageView.ScaleType.MATRIX);
            float scale;
    
            // Handle touch events here...
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
    
            case MotionEvent.ACTION_DOWN: //first finger down only
               savedMatrix.set(matrix);
               start.set(event.getX(), event.getY());
               Log.d(TAG, "mode=DRAG" );
               mode = DRAG;
               break;
            case MotionEvent.ACTION_UP: //first finger lifted
            case MotionEvent.ACTION_POINTER_UP: //second finger lifted
               mode = NONE;
               Log.d(TAG, "mode=NONE" );
               break;
            case MotionEvent.ACTION_POINTER_DOWN: //second finger down
               oldDist = spacing(event); // calculates the distance between two points where user touched.
               Log.d(TAG, "oldDist=" + oldDist);
               // minimal distance between both the fingers
               if (oldDist > 5f) {
                  savedMatrix.set(matrix);
                  midPoint(mid, event); // sets the mid-point of the straight line between two points where user touched. 
                  mode = ZOOM;
                  Log.d(TAG, "mode=ZOOM" );
               }
               break;
    
            case MotionEvent.ACTION_MOVE: 
               if (mode == DRAG) 
               { //movement of first finger
                  matrix.set(savedMatrix);
                  if (view.getLeft() >= -392)
                  {
                     matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);
                  }
               }
               else if (mode == ZOOM) { //pinch zooming
                  float newDist = spacing(event);
                  Log.d(TAG, "newDist=" + newDist);
                  if (newDist > 5f) {
                     matrix.set(savedMatrix);
                     scale = newDist/oldDist; //thinking I need to play around with this value to limit it**
                     matrix.postScale(scale, scale, mid.x, mid.y);
                  }
               }
               break;
            }
    
            // Perform the transformation
            view.setImageMatrix(matrix);
    
            return true; // indicate event was handled
         }
    
         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);
         }
    
         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);
         }
    

    Hope this will help you, Its easy Task.