How to get Coordinates on touch Event on bitmap no

2019-01-26 00:52发布

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.

2条回答
一夜七次
2楼-- · 2019-01-26 01:37

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;
}
查看更多
唯我独甜
3楼-- · 2019-01-26 01:43

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.

查看更多
登录 后发表回答