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