Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 拖动ImageView时侦听容器边缘_Android_Android Layout_Position - Fatal编程技术网

Android 拖动ImageView时侦听容器边缘

Android 拖动ImageView时侦听容器边缘,android,android-layout,position,Android,Android Layout,Position,我有一个比它所显示的容器大的图像。图像可在容器内拖动,当前可在容器内的任何位置拖动。我希望在图像顶部与容器顶部匹配时停止拖动图像 我的代码: @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fra

我有一个比它所显示的容器大的图像。图像可在容器内拖动,当前可在容器内的任何位置拖动。我希望在图像顶部与容器顶部匹配时停止拖动图像

我的代码:

@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>
@覆盖
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图根视图=充气机。充气(R.layout.fragment_main,容器,
假);
frame=(ViewGroup)rootView.findviewbyd(R.id.frame1);
imageView=(imageView)rootView.findViewById(R.id.imageView1);
setOnTouchListener(新的OnTouchListener(){
@凌驾
公共布尔onTouch(视图v,运动事件){
final int X=(int)event.getRawX();
final int Y=(int)event.getRawY();
LayoutParams LayoutParams=(LayoutParams)imageView.getLayoutParams();
Log.d(“警报”,imageView.getId()+“按下”);
开关(event.getAction()&MotionEvent.ACTION\u掩码){
case MotionEvent.ACTION\u DOWN:
RelativeLayout.LayoutParams lParams=(RelativeLayout.LayoutParams)imageView.getLayoutParams();
_xDelta=X-lParams.leftMargin;
_yDelta=Y-lParams.topMargin;
打破
case MotionEvent.ACTION\u UP:
//无所事事
打破
case MotionEvent.ACTION\u指针\u向下:
//无所事事
打破
case MotionEvent.ACTION\u指针\u向上:
//无所事事
打破
case MotionEvent.ACTION\u移动:
layoutParams=(RelativeLayout.layoutParams)imageView.getLayoutParams();
layoutParams.leftMargin=X-xDelta;
layoutParams.topMargin=Y-_yDelta;
v、 setLayoutParams(layoutParams);
打破
}
frame.invalidate();
返回true;
}
});
返回rootView;
}
}

我想阻止图像被拖到超出其容器边界的地方。例如,图像顶部应被拖到不超过布局容器顶部的地方。

经过几天的代码修补,我终于找到了解决问题的有效方法。我将在这里发布代码

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;
    }
}
视图框架;
图像视图图像视图;
//在拖动操作期间存储手指的位置
私人国际电信公司;
私人国际酒店;
//存储框架的边界
私有整数帧宽度;
私有整数帧高度;
公共占位符片段(){
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图根视图=充气机。充气(R.layout.fragment_main,容器,
假);
frame=(视图)rootView.findviewbyd(R.id.frame1);
imageView=(imageView)rootView.findViewById(R.id.imageView1);
//绘制帧后,获取帧的宽度和高度。
frame.getViewTreeObserver().addOnGlobalLayoutListener(新ViewTreeObserver.OnGlobalLayoutListener()){
@凌驾
公共图书馆{
//设置全局变量
frameWidth=getView().getWidth();
frameHeight=getView().getHeight();
//Log.e(“Framewidth”,“Framewidth+Framewidth”);
Log.e(“帧高”、“帧高”);
}
});     
//控制键盘拖动功能的ontouch侦听器。
setOnTouchListener(新的OnTouchListener(){
@凌驾
公共布尔onTouch(视图v,运动事件){
//获取用户触摸屏的x线和y线
final int X=(int)event.getRawX();
final int Y=(int)event.getRawY();
LayoutParams LayoutParams=(LayoutParams)imageView.getLayoutParams();
开关(event.getAction()&MotionEvent.ACTION\u掩码){
case MotionEvent.ACTION\u DOWN:
RelativeLayout.LayoutParams lParams=(RelativeLayout.LayoutParams)imageView.getLayoutParams();
_xDelta=X-lParams.leftMargin;
_yDelta=Y-lParams.topMargin;
打破
case MotionEvent.ACTION\u UP:
//无所事事
打破
case MotionEvent.ACTION\u指针\u向下:
//无所事事
打破
case MotionEvent.ACTION\u指针\u向上:
//无所事事
打破
case MotionEvent.ACTION\u移动:
layoutParams=(RelativeLayout.layoutParams)imageView.getLayoutParams();
Log.e(“Test”、“+imageView.getX()+”、“+imageView.getY());
layoutParams.leftMargin=X-xDelta;
layoutParams.topMargin=Y-_yDelta;
//阻止将图像的外边缘拖过
//框架外部边缘。
如果(((int)layoutParams.topMargin)>=1){
layoutParams.topMargin=0;
如果(((int)layoutParams.leftMargin)>=1){
layoutParams.leftMargin=0;
}else if(((int)layoutParams.leftMargin)