Android 将图像放置在坐标处
我有一个程序,我想在触摸事件的坐标处放置一个图像。我现在有了坐标,我只需要帮我把图像放在那里。我将使用一个可拖动的 编辑** 我还想把它覆盖在另一张图片上。我找不到任何关于这方面的文件。我认为这应该很容易 有人能帮我吗 编辑**** 明白了,现在我只需要找出如何在触摸点而不是左上角获得图像的中间部分:Android 将图像放置在坐标处,android,image,Android,Image,我有一个程序,我想在触摸事件的坐标处放置一个图像。我现在有了坐标,我只需要帮我把图像放在那里。我将使用一个可拖动的 编辑** 我还想把它覆盖在另一张图片上。我找不到任何关于这方面的文件。我认为这应该很容易 有人能帮我吗 编辑**** 明白了,现在我只需要找出如何在触摸点而不是左上角获得图像的中间部分: final View touchView2 = findViewById(R.id.ImageView02); touchView.setOnTouchListener(new View
final View touchView2 = findViewById(R.id.ImageView02);
touchView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.v("x,y",event.getY()+","+event.getX());
touchView2.bringToFront();
int y = (int)event.getY();
int x = (int)event.getX();
touchView2.layout(x, y, x+48, y+48);
return true;
}
});
这就是我在底部中心绘制回收站图标的方式要将图像放置在特定坐标处,您必须在画布上绘制图像。要获取触摸事件的坐标,请使用以下代码:
@Override
public void onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
mTouchX = event.getX();
mTouchY = event.getY();//stores touch event
} else {
mTouchX = -1;
mTouchY = -1;
}
super.onTouchEvent(event);
}
将所需图像放在应用程序xml的顶部。 将其设置为不可见或消失 替换:
final View touchView2 = findViewById(R.id.ImageView02);
在类构造函数之前:
ImageView touchView2;
在构造函数(onCreate)方法中
现在通过在屏幕上获得所有触摸设置onTouchEventListener。
如果这些坐标位于您喜欢的位置,请使用按下的X和Y坐标调用placeImage方法。此方法位于类构造函数(onCreate)之外,因此下面的第一个方法应该是正确的:
@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
int eventAction = event.getAction();
switch(eventAction) {
case MotionEvent.ACTION_DOWN:
float TouchX = event.getX();
float TouchY = event.getY();
placeImage(TouchX, TouchY);
break;
}
return true;
}
现在,placeImage方法:
private void placeImage(float X, float Y) {
int touchX = (int) X;
int touchY = (int) Y;
// placing at bottom right of touch
touchView2.layout(touchX, touchY, touchX+48, touchY+48);
//placing at center of touch
int viewWidth = touchView2.getWidth();
int viewHeight = touchView2.getHeight();
viewWidth = viewWidth / 2;
viewHeight = viewHeight / 2;
touchView2.layout(touchX - viewWidth, touchY - viewHeight, touchX + viewWidth, touchY + viewHeight);
}
这应该是你的答案。。。现在,您只需使touchView可见:
touchView2.setVisibility(0);
我有点击的坐标,如何在点击的位置放置图像?我不能使用setX()和setY(),因为我的应用程序支持API-10。
private void placeImage(float X, float Y) {
int touchX = (int) X;
int touchY = (int) Y;
// placing at bottom right of touch
touchView2.layout(touchX, touchY, touchX+48, touchY+48);
//placing at center of touch
int viewWidth = touchView2.getWidth();
int viewHeight = touchView2.getHeight();
viewWidth = viewWidth / 2;
viewHeight = viewHeight / 2;
touchView2.layout(touchX - viewWidth, touchY - viewHeight, touchX + viewWidth, touchY + viewHeight);
}
touchView2.setVisibility(0);