How to get correct coords(event.getX() / event.getY()) of Object with OnTouchListener

前端 未结 4 1162
庸人自扰
庸人自扰 2020-12-12 06:29

I have this :

and what i want exactly is that to show portion of Bitmap via coords(X,Y) of object with OnTouchListener(orange square<

4条回答
  •  青春惊慌失措
    2020-12-12 07:02

    So it was simple to resolve this problem.
    Solution:
    I have an object(orange square) with OnTouchListener(for e.g. he's an quad with size 50dp Height/ 50dp Width).
    So view.getX()/view.getY() takes relative coords(center) of object(that have OnTouchListener).
    So what i needed:

    //imageview parameters ,where i will show this part of bitmap
    int Width = img.getWidth();
    int Height = img.getHeight();
    //after this,i make margin of my coords,via this simple formula -> Current Coord X - Width / 2 (same for Y)
     Bitmap mBitmap = Bitmap.createBitmap(sourceBitmap,view.getX() - Width / 2,view.getY()-Height / 2,Width,Height);  
    

    And that is all.
    Enjoy!

提交回复
热议问题