开发者

Problems with touch event

开发者 https://www.devze.com 2023-04-05 15:10 出处:网络
Speaking of events we know that the touch screen method onTouchEvent (MotionEvent events) allows you to capture touch events on the screen and event.getX () and

Speaking of events we know that the touch screen method onTouchEvent (MotionEvent events) allows you to capture touch events on the screen and event.getX () and event.getY () give me the coordinates of the touch as float values ​​(ie values ​​with a comma).

Specifically, I realized that taking logcat using the fixed point the finger in a mobile phone screen and not moving it, the event is not only perceived but MotionEvent.ACTION_DOWN MotionEvent.ACTION_MOVE and returned coordinates are manifold. This i can understand because the surface of the finger touches more points and more by reading the device coordinates believe that the finger moves while holding it.

My problem is that I would read the color of one pixel of a color image when I hold your finger still. In particular I need the central area occupied by the finger. First, assuming you have a Bitmap object, I have to round to integer coordinates of the touch as the getPixel (intX, int y) coordinates of the entire Bitmap wants to return the pixel color.

Then how do I get the central point of touch? Be that there is a function that can help me? Keep in mind that while I stopped and read 开发者_如何学Gothe value of a pixel on the image then I start to move slowly and even though I always record every move the center pixel. I need this because every move, if you change the color of the image in pixels, I want to vibrate or not the device.

If I could stop thinking about themselves in a dynamic array to store the coordinates of different pixels and search for the center of gravity, but moving I do not know what to do. I think however, that the research center of gravity, if not done very efficiently, can be slow during the move and then give the wrong results. If the CG is not quite enough for me to another point belonging to the environment.

I hope you can help me maybe with a few lines of code sketched. Thanks in advance.


To get the touch position of finger, just cast the getX() and getY() to int and perform your desired tasks.

public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN ) {
      getPixelAt((int)event.getX(), (int)event.getY());
    }
}


you can put you on main activity class when you can want to touch event occurs. let see you can put following code or not ? and return is most important as well.

@Override
     public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
    mDistanceX = 0;
  }
  return super.onTouchEvent(event);
     }
0

精彩评论

暂无评论...
验证码 换一张
取 消