0

我正在为 android 上的照片编辑器创建调整大小功能。每次触摸图像时,我都想在 imageView 的角上放一个圆圈/点。这样我就可以拖动那个圆圈来调整图像的大小。

我的问题是如何获得图像视图的 4 个角的 xy 位置。现在我正在为左上角创建:

  ImageView edgePoints = null;

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);        

    findViewById(R.id.imageBlue).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.imgstage).setOnDragListener(new MyDragListener()); 
    edgePoints = (ImageView)findViewById(R.id.edgePoint); // the dot/circle
    edgePoints.setVisibility(View.INVISIBLE);

}   


Private final class MyTouchListener implements OnTouchListener {
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN ) {
          ClipData data = ClipData.newPlainText("", "");
          DragShadowBuilder shadowBuilder = new DragShadowBuilder(view);
          view.startDrag(data, shadowBuilder, view, 0);
          view.setVisibility(View.VISIBLE);


          ViewGroup owner = (ViewGroup) view.getParent();
          if(owner.getId() == R.id.imgstage)
          {
              lastImageSelected = (ImageView) view;
              RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
              params1.leftMargin = view.getLeft();
              params1.topMargin = view.getTop();
              edgePoints.setLayoutParams(params1);
               edgePoints.setVisibility(View.Visible);
          }

          return true;
        } else {
        return false;
        }
      }

    }

这是我的拖动监听器

     class MyDragListener implements OnDragListener {
    @Override
      public boolean onDrag(View v, DragEvent event) {
        int action = event.getAction();
        final int X = (int) event.getX();
        final int Y = (int) event.getY();

        switch (event.getAction()) {
        case DragEvent.ACTION_DRAG_STARTED:
          break;
        case DragEvent.ACTION_DRAG_ENTERED:
          break;
        case DragEvent.ACTION_DRAG_EXITED:        
          break;
        case DragEvent.ACTION_DROP:
          View view = (View) event.getLocalState();
          ViewGroup owner = (ViewGroup) view.getParent();
          ImageView imageView = (ImageView) findViewById(R.id.imgView);
          RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(imageView.getWidth()/9, imageView.getWidth()/9);
          params1.leftMargin = (int) event.getX() - ((imageView.getWidth()/9)/2);
          params1.topMargin = (int) event.getY() -((imageView.getWidth()/9)/2);


          if(owner.getId() != R.id.imgstage)
          {
              owner.removeView(view);
              RelativeLayout container = (RelativeLayout) v;
              container.addView(view,params1);
          }
          else
              view.setLayoutParams(params1);

          view.setVisibility(View.VISIBLE);


          if(owner.getId() != R.id.imgstage)
          {
              FrameLayout layout = (FrameLayout) owner;
              Context context = getApplicationContext();


              ImageView imv=new ImageView(context);
              FrameLayout.LayoutParams marginParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
              marginParams.setMargins(4, 4, 4, 4);
              imv.setLayoutParams(marginParams);


              imv.setOnTouchListener(new MyTouchListener());

              if(owner.getId() == R.id.blueLayout)   
                  imv.setImageResource(R.drawable.blueblock);
              if(owner.getId() == R.id.orageLayout) 
                  imv.setImageResource(R.drawable.orangeblock);
              if(owner.getId() == R.id.redLayout) 
                  imv.setImageResource(R.drawable.redblock);
              if(owner.getId() == R.id.greenLayout) 
                  imv.setImageResource(R.drawable.greenblock);

              layout.addView(imv);//to be included
          }
          break;
        case DragEvent.ACTION_DRAG_ENDED:
          default:
          break;
        }
        return true;
      }
  }
4

2 回答 2

1

您的应用程序崩溃的原因是您尝试edgePoints多次添加到布局中。如果已添加视图,则无法添加视图。

解决方案是在调用之前添加视图,onTouch并保留它INVISIBLE。然后,onTouch,您应该执行以下操作:

public boolean onTouch(View view, MotionEvent motionEvent) {
    if (motionEvent.getAction() == MotionEvent.ACTION_DOWN ) {

        RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        params1.leftMargin = lastImageSelected.getLeft();
        params1.topMargin = lastImageSelected.getTop();

        edgePoints.setVisibility(View.VISIBLE);
        edgePoints.setLayoutParams(params1);
    }
}

只需设置layout params不要调用addView()

于 2013-11-05T08:00:29.923 回答
0

如果你想让照片编辑器更好地在你自己的视图上做,关于在那里重新调整大小很容易,你只需绘制一个包含图像的矩形和边缘上的四个小矩形,使用自己的视图的优点是获取坐标和操作坐标要容易得多,而且不会让布局参数头疼。

看看这里

于 2013-11-05T08:44:04.820 回答