圆圈内的可移动图像

时间:2014-09-20 03:38:55

标签: android android-layout crop

enter image description here

在此图片中,圆圈是静态的。矩形图像位于此圆圈的后面。我需要移动,缩小,放大和平移此圆圈内的矩形图像。怎么做到这一点?

给我建议如何做到这一点。

提前致谢

3 个答案:

答案 0 :(得分:2)

遵循指南:

  1. 采取相对布局
  2. 在此布局中添加ImageView。
  3. 现在添加圆形图像。
  4. 示例:

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

    然后在代码下方应用以使用Imageview的移动。

    使用OnTouchListener实现活动,然后

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

    希望这会对你有所帮助,这很容易。

答案 1 :(得分:1)

看看

TouchImageView

您可以使用类

缩放和平移图像视图

答案 2 :(得分:0)

此库正是ProfileView的对象。

它具有一个可定制的圆形视图,其中带有imageview,该视图是可缩放的,可移动的,并且这些事件是自定义的。