简体   繁体   English

如何在Android中获取图像的(x,y)坐标?

[英]How to get (x,y) cordinates of image in android?

I have drawn a image on ONTOUCH with (X,Y)cordinates while moving the image it's cordinates should move along with the image when it reaches it end point the image should be drawn there or else the image should go back to it's starting position. 我已经用(X,Y)坐标在ONTOUCH上绘制了图像,同时移动图像的坐标应在到达终点时与图像一起移动,否则应在此处绘制图像,否则图像应回到其起始位置。

For eg:(in our mobile if kepad lock we will drag it to open if it reaches it end point the lock will open or else the image will reaches it's starting position). 例如:(在我们的手机中,如果kepad锁到达终点,它将拖动它以将其打开,否则锁将打开,否则图像将到达其起始位置)。

Reffered link: http://www.vogella.com/tutorials/AndroidTouch/article.html 引用的链接: http ://www.vogella.com/tutorials/AndroidTouch/article.html

Here is my code: 这是我的代码:

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (mLocked) {
return false;
}

final int action = event.getAction();

float x = event.getX();
float y = event.getY();
if (mOnDrawerScrollListener != null) {
mOnDrawerScrollListener.onScrollStarted();
}

final int pt = getSide();
mTouchDelta = (int)(y - pt);
prepareTracking(pt);
mVelocityTracker.addMovement(event);
final Rect frame = mFrame;
final View handle = mHandle;

If anyone have idea about this please help me friends. 如果有人对此有任何想法,请帮助我的朋友。

If you want to get(x, y) cordinates of view (ImageView), just use view.getX() , view.getY() . 如果要获取视图(ImageView)的(x,y)坐标,只需使用view.getX()view.getY()即可 But when you get it in onCreate() , the results will be (0, 0) , because it is not created yet. 但是,当您在onCreate()中获得它时,结果将是(0,0) ,因为尚未创建。 You should use view.getX(), view.getY() in onGlobalLayoutListener() . 您应该在onGlobalLayoutListener()中使用view.getX(), view.getY() For eg: 例如:

rlRoot.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            // TODO Auto-generated method stub
            rlRoot.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            Log.d("X", view.getX()+"");
            Log.d("Y", view.getY()+"");
        }
    });

With rlRoot is the layout includes your view and view may be ImageView or any View. 使用rlRoot是包含您的视图的布局,该视图可以是ImageView或任何View。 Try it, hope it help. 试试看,希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM