拖动ImageView时侦听Container Edges

时间:2014-04-28 15:55:21

标签: android android-layout position

我的图像大于显示的容器。图像可以在容器内拖动,目前可以在容器内的任何位置拖动。我想在图像顶部与容器顶部匹配时停止拖动图像。

我的代码:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);

        frame = (ViewGroup) rootView.findViewById(R.id.frame1);


        imageView = (ImageView) rootView.findViewById(R.id.imageView1);

        imageView.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                final int X = (int) event.getRawX();
                final int Y = (int) event.getRawY();

                LayoutParams layoutParams = (LayoutParams) imageView.getLayoutParams();

                Log.d("Alert", imageView.getId() + " pressed");

                switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN: 
                    RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) imageView.getLayoutParams(); 
                    _xDelta = X - lParams.leftMargin;
                    _yDelta = Y - lParams.topMargin;
                    break;
                case MotionEvent.ACTION_UP:
                    //Do Nothing
                    break;
                case MotionEvent.ACTION_POINTER_DOWN:
                    //Do Nothing
                    break;
                case MotionEvent.ACTION_POINTER_UP:
                    //Do Nothing
                    break;
                case MotionEvent.ACTION_MOVE:
                    layoutParams = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
                    layoutParams.leftMargin = X - _xDelta;
                    layoutParams.topMargin = Y - _yDelta;
                    v.setLayoutParams(layoutParams);
                    break;
                }
                frame.invalidate();
                return true;
            }
        });


        return rootView;
    }
}


 <RelativeLayout
    android:id="@+id/frame1"
    android:layout_width="wrap_content"
    android:layout_height="130dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:background="#000" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:scaleType="matrix"
        android:src="@drawable/image1" />

</RelativeLayout>

我想阻止图像被拖得比其容器的边框更远,例如图像的顶部应该拖动不超过布局容器的顶部。

1 个答案:

答案 0 :(得分:0)

经过一段时间修改我的代码后,我终于找到了解决我的问题的解决方法,在这里发布代码。

View frame;
    ImageView imageView;

    //stores position of finger during drag operation
    private int _xDelta;
    private int _yDelta;

    //Stores the boundaries of the Frame
    private int frameWidth;
    private int frameHeight;

    public PlaceholderFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);

        frame = (View) rootView.findViewById(R.id.frame1);
        imageView = (ImageView) rootView.findViewById(R.id.imageView1);     

        //Get frame width and height once it has been drawn.
        frame.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                // set global variables
                frameWidth = getView().getWidth();
                frameHeight = getView().getHeight();
                //Log.e("Framewidth", "" + frameWidth);
                Log.e("FrameHeight", "" + frameHeight);



            }
        });     

        //ontouch listener that controls the Drag functionality of the keyboard.
        imageView.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                //get x and y cords of the user touch
                final int X = (int) event.getRawX();
                final int Y = (int) event.getRawY();

                LayoutParams layoutParams = (LayoutParams) imageView.getLayoutParams();

                switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN: 
                    RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) imageView.getLayoutParams(); 
                    _xDelta = X - lParams.leftMargin;
                    _yDelta = Y - lParams.topMargin;
                    break;
                case MotionEvent.ACTION_UP:
                    //Do Nothing
                    break;
                case MotionEvent.ACTION_POINTER_DOWN:
                    //Do Nothing
                    break;
                case MotionEvent.ACTION_POINTER_UP:
                    //Do Nothing
                    break;
                case MotionEvent.ACTION_MOVE:
                    layoutParams = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
                    Log.e("Test", "" + imageView.getX() + " , " + imageView.getY());    

                    layoutParams.leftMargin = X - _xDelta;
                    layoutParams.topMargin = Y - _yDelta;

                    //stops the outer edges of the image from being dragged past the 
                    //frames outer edges.
                    if(((int)layoutParams.topMargin) >= 1) {
                        layoutParams.topMargin = 0;
                        if(((int)layoutParams.leftMargin) >= 1){
                            layoutParams.leftMargin = 0;
                        }else if(((int)layoutParams.leftMargin) <= -291){
                            layoutParams.leftMargin = -290;
                        }
                    } else if(((int)layoutParams.topMargin) <= -70) {
                        layoutParams.topMargin = -68;
                        if(((int)layoutParams.leftMargin) >= 1){
                            layoutParams.leftMargin = 0;
                        }else if(((int)layoutParams.leftMargin) <= -291){
                            layoutParams.leftMargin = -290;
                        }
                    } else if(((int)layoutParams.leftMargin) >= 1){
                        layoutParams.leftMargin = 0;
                    } else if(((int)layoutParams.leftMargin) <= -291) {
                        layoutParams.leftMargin = -290;
                    }

                    v.setLayoutParams(layoutParams);
                    break;
                }
                frame.invalidate();
                return true;
            }
        });


        return rootView;
    }
}

这是为智能手表创建自定义InputMethod的大型项目的第一部分