Hanon
Hanon

Reputation: 3927

Android drag imageview in RelativeLayout Resized

I am currently making an android application which allow user to add some image from a list to the screen, and user is allowed to drag the image on it.

I can drag the image by using onTouch method. But I faced a problem that when I drag the image view to the left or bottom size

The imageview will auto resize and cannot drag it out to the layout.

How can I drag it outside the layout without resizing the image view?

@Override
public boolean onTouch(View view, MotionEvent event)  {
    ImageView imageView = (ImageView)view;
    imageView.bringToFront();


    //  Handle event
    switch (event.getAction() & MotionEvent.ACTION_MASK)  {
        case MotionEvent.ACTION_DOWN:  {
            startPoint.set(event.getX(), event.getY());
            currentMode = EVENT_DRAG;
            break;
        }  

        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_POINTER_UP:  {
            currentMode = EVENT_NONE;
            break;
        }  

        case MotionEvent.ACTION_MOVE:  {
            RelativeLayout.LayoutParams layoutPrarms = (RelativeLayout.LayoutParams)imageView.getLayoutParams();
            int left = layoutPrarms.leftMargin + (int)(event.getX() - startPoint.x);
            int top = layoutPrarms.topMargin + (int)(event.getY() - startPoint.y);
            layoutPrarms.leftMargin = left;
            layoutPrarms.topMargin = top;
            imageView.setLayoutParams(layoutPrarms);     
            break;
        }  
    }

    //  Indicate event was handled
    return true;
}

Upvotes: 0

Views: 1693

Answers (1)

coder
coder

Reputation: 10520

Try having your parent layout be a LinearLayout. I have only run into this problem using RelativeLayouts.

Upvotes: 1

Related Questions