我有一个程序,我想放置图像的触摸事件的坐标。我有坐标,现在我只需要帮助放置的图像那里。我将使用一个绘制。
编辑** 我也希望它覆盖了另一幅图像。我找不到任何文件,在这个什么那么。我想这应该很容易。
谁能帮我?
编辑*的 * 的 心动不如行动,现在我只需要弄清楚如何获取图像的中间在触摸点,而不是左上角:
最后查看touchView2 = findViewById(R.id.ImageView02);
touchView.setOnTouchListener(新View.OnTouchListener(){
@覆盖
公共布尔onTouch(视图V,MotionEvent事件){
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);
返回true;
}
});
解决方案
把你想在上面的应用程序的XML形象。 设置不可见或消失了......
替换:
最后查看touchView2 = findViewById(R.id.ImageView02);
在类的构造函数之前:
ImageView的touchView2;
和在构造函数(的onCreate)方法
touchView2 =(ImageView的)findViewById(R.id.ImageView02);
现在成立了一个onTouchEventListener通过让所有触摸屏上。 如果这些坐标是在一个位置,你喜欢叫placeImage方法与pressed X和Y坐标。这种方法被放置在类的构造函数(的onCreate),所以下面的第一个方法应该是正确的外:
@覆盖
公共布尔的onTouchEvent(MotionEvent事件){
super.onTouchEvent(事件);
INT eventAction = event.getAction();
开关(eventAction){
案例MotionEvent.ACTION_DOWN:
浮TouchX = event.getX();
浮敏感= event.getY();
placeImage(TouchX,敏感);
打破;
}
返回true;
}
现在的placeImage方式:
私人无效placeImage(浮X,浮Y){
INT touchX =(INT)X;
INT敏感=(INT)Y;
//放置在触摸右下角
touchView2.layout(touchX,敏感,touchX + 48,+敏感48);
//放置在触摸中心
INT viewWidth = touchView2.getWidth();
INT viewHeight = touchView2.getHeight();
viewWidth = viewWidth / 2;
viewHeight = viewHeight / 2;
touchView2.layout(touchX - viewWidth,敏感 - viewHeight,touchX + viewWidth,敏感+ viewHeight);
}
这应该是你的答案......现在你只需要做的TouchView可见的:
touchView2.setVisibility(0);
I have a program in which I would like to place an image at the coordinates of a touch event. I have the coordinates now I just need help with placing an image there. I will be using a drawable.
Edit** I also want to overlay it over another image. I cant find any documentation on this what so ever. I would think it should be easy.
Can anyone help me?
EDIT** Got it, now I just have to figure out how to get the middle of the image at the touch spot and not the top left corner:
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;
}
});
解决方案
place the image you want on top in the xml of your application. set it invisible or gone...
replace:
final View touchView2 = findViewById(R.id.ImageView02);
before the classes constructor:
ImageView touchView2;
and in the constructor (onCreate) method
touchView2 = (ImageView) findViewById(R.id.ImageView02);
Now set up a onTouchEventListener by getting all touches on the screen. If those coordinates are in a position you like call the placeImage method with the pressed X and Y coordinate. this method is placed outside the class constructor (onCreate) so first method below should be right:
@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;
}
now the placeImage method:
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);
}
This should be your answer... now you only have to make touchView visible:
touchView2.setVisibility(0);
相关推荐
最新文章