限制FrameLayout中的图像,以便在Android中不能将其拖到它之外

时间:2013-02-22 19:26:09

标签: android android-imageview

我正在使用FrameLayout在布局中设置阴影。在FrameLayout之上,我添加了ImageView,它可以在屏幕上拖放。可调整。

我已经FrameLayoutandroid:layout_width& android:layout_heightfill_parent消耗屏幕区域。有必要满足应用程序要求。

问题

如何限制Image不能在FrameLayout之外拖延?

图1和图2:

Image 1 Image 3

图3:

Image 2

1 个答案:

答案 0 :(得分:0)

参考可拖动视图的大小和触摸点为可拖动视图添加限制已经解决了我的问题。

所以我需要做的只是让 触摸点 framelayout边界进行比较,这取决于场景设置对可拖动视图的限制。

代码就像这些对我来说是完美的工作::

public boolean onTouch(View paramView, MotionEvent event)
    {
      ImageView view = (ImageView)paramView;
      switch (event.getAction() & MotionEvent.ACTION_MASK)


case MotionEvent.ACTION_MOVE:

          if (mode == DRAG) {
/*
* Restricting Image not to be draggable outside of FrameLayout
*/
              matrix.set(savedMatrix); //Top
              if(event.getY()==0)
              {
                  matrix.postTranslate(event.getX()+15-start.x, 0+15);
                  Log.d("Top", "Top...");
              }
              else if(event.getX()==0) //Left
              {
                  matrix.postTranslate(0+15, event.getY()-start.y+15);
                  Log.d("LEFT", "Left...");
              }
              else if((event.getY()-ImageHeight)>fm.getHeight()) //Bottom
              {
                  matrix.postTranslate(event.getX()-start.x+15, fm.getHeight()-ImageHeight+15);
                  Log.d("BOTTOM", "Bottom...");
              }
              else if((event.getX()-ImageWidth)>fm.getWidth()) //Right
              {
                  matrix.postTranslate(fm.getWidth()-ImageWidth+15, event.getY()-start.y+15);
                  Log.d("RIGHT", "Right....");
              }
              else if(event.getY()==0 && (event.getX()-ImageWidth)>fm.getWidth()) //Top -Right
              {
                  matrix.postTranslate(fm.getWidth()-ImageWidth-15, 15);
              }
              else if(event.getY()==0 &&event.getX()==0) //Top-Left
              {
                  matrix.postTranslate(15, 15);
              }
              else if((event.getY()-ImageHeight)>fm.getHeight() && event.getX()==0) //Bottom-Left
              {
                  matrix.postTranslate(15, fm.getHeight()-ImageHeight-15);
              }
              else if((event.getX()-ImageWidth)>fm.getWidth() && (event.getY()-ImageHeight)>fm.getHeight())
              {
                  matrix.postTranslate(fm.getWidth()-ImageWidth-15, fm.getHeight()-ImageHeight-15);
              }
              else
              {
                  matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);  
                  Log.d("ACTION_MOVE", "Starting Point::"+start.x+","+start.y+" Ending Point::"+event.getX()+","+event.getY());
                  Log.d("ACTION_DOWN", "FrameLayout::"+fm.getWidth()+" ,"+fm.getHeight()+" ImageView:: "+view.getWidth()+", "+view.getHeight());

              }
          }
          else if (mode == ZOOM) {

              float newDist = spacing(event);
              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;       
    }