How to get Coordinates on touch Event on bitmap not of Screen

坚强是说给别人听的谎言 提交于 2019-11-28 01:59:23

问题


Greets ,

How can I get the Coordinates of the Bitmap not of the screen. Screen Coordinates got by event.getX(),event.getY() methods but not getting coordinates of the bitmap. Please help anyone.


回答1:


You can't get the coordinates of the bitmap directly. You need to calculate to yourself. Use the position of the ImageView, and with that you can handle it.

Of course, when you are after Activity onCreate you can acces to any inflated (active) views parameter. Exemple. ImageView a; a.getPaddingBottom(); Like all coordinats (left right etc...) After this you need the Height and Width of the ImageView. Nah, when you know the views position, hegiht and width you can calculate.

Example:

final ImageView iv_YourImage = (ImageView) findViewById(R.id.iv_imageview);
public boolean onTouch(View v, MotionEvent event){
int topParam =  iv_YourImage.getPaddingTop();
 int rightParam =  iv_YourImage.getPaddingRight();
int maxTopParam = topParam+iv_YourImage.getMaxHeight();
int maxRightParam = rightParam + iv_YourImage.getMaxWidth();
 if(event.getX>topParam&&event.getX<maxTopParam){
    //the x coordinate is in your image... do the same to Y
 }
return true;
}



回答2:


look at the following code if the answer is too short:

Android: Detect touch on actual image in ImageView

the code replaces the padding and max height coding of this answer and replaces it with the actual width and height of the displayed image using getWidth() and getHeight(). And uses the screen width and height to calculate its position. If you want more detailed answers for your problem you should give more information such as where did you draw the bitmap... did you define it at the activity XML or did you draw it at a random location such as at the X and Y coordinates of a touch event, the answer depends on that information.

package com.example.img;

imports....

public Image extends Activity {

ImageView imgYourImage;
// int imgWidth;
// etc...

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image);

    imgYourImage = (ImageView) findViewById(R.id.yourImage);
    imgDimension();
}

private void imgDimension() {
    imgWidth = imgYourImage.getWidth();
    // at newer api's you can use getLeft() and getTop() of imageView
    // if getLeft() works you have the leftX coordinate of image already.
    // do same with height
    // also get the screen Width and Height by display.getWidth() (depreciated)
    // Or the newer code display.getSize()
    leftX = (screenwidth - imgWidth) / 2;
    rightx = leftx + imgWidth;
}

Notice that this code only works when the image is displayed at the horizontal center of the screen. Or use top and bottom if the image is displayed at the vertical center of the screen.



来源:https://stackoverflow.com/questions/12674941/how-to-get-coordinates-on-touch-event-on-bitmap-not-of-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!