简体   繁体   English

Android在RelativeLayout中拖动imageview调整大小

[英]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. 我目前正在制作一个android应用程序,允许用户从列表中向屏幕添加一些图像,并允许用户在其上拖动图像。

I can drag the image by using onTouch method. 我可以使用onTouch方法拖动图像。 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. imageview将自动调整大小,无法将其拖到布局中。

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;
}

Try having your parent layout be a LinearLayout. 尝试使您的父布局为LinearLayout。 I have only run into this problem using RelativeLayouts. 我只使用RelativeLayouts遇到了这个问题。

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

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