我正在为 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;
}
}