Vicky
Vicky

Reputation: 941

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.

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).

Reffered link: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.

Upvotes: 0

Views: 95

Answers (1)

sonngaytho
sonngaytho

Reputation: 111

If you want to get(x, y) cordinates of view (ImageView), just use view.getX(), view.getY(). But when you get it in onCreate(), the results will be (0, 0), because it is not created yet. You should use view.getX(), view.getY() in onGlobalLayoutListener(). For e.g:

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. Try it, hope it help.

Upvotes: 1

Related Questions