android 自定义可拖动框

这阵子在做一个东西 ,  要求定位一个动态摄像头中一个东西的位置(这个位置必须由人 手动圈住),查的好多都是调用系统 intent 相机切图,发现不可行,所以有了这篇博客。

此代码实现了,方形框拉伸,拖动。

刚看到这个要求时,觉得很简单,结果....           android 里面还是坑太多了,这次就是掉了android 中获取控件位置的一个坑,setleft、setright、settop、setbottom  不稳定,所以弄得很乱啊。

废话不多说上代码,main.java中只有一个跳转 ,有用的只有一个java、一个xml、一个style    文件列表如下:


1、CameraRemoveView.java

public class CameraRemoveView extends AppCompatActivity implements GestureDetector.OnGestureListener{

    private int Left = 300;
    private int Top =300 ;
    private int Bottom = 600;
    private int Right = 600;
    private int r=70;
    private int winTop ; //状态栏高度

    private GestureDetector gestureDetector;

    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera_remove_layout);

        /**
         * 获取状态栏高度——方法1
         * */
        int statusBarHeight1 = -1;
        //获取status_bar_height资源的ID
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight1 = getResources().getDimensionPixelSize(resourceId);
        }
        winTop = statusBarHeight1;

        textView=(TextView) findViewById(view);

        //设置初始位置
        textView.setX(Left);
        textView.setY(Top);
        textView.setHeight( Bottom - Top );
        textView.setWidth( Right - Left );
        gestureDetector=new GestureDetector(this);   //监听郑哥屏幕触摸
    }


    /**
     * 计算触摸点是否在给定正放心内部  R为边长1/2
     * @param x 中心点x坐标
     * @param y 中心点y坐标
     * @param event_x 触摸点x坐标
     * @param event_y 触摸点y坐标
     * @return
     */
    private boolean isInSquare(int x,int y,int event_x,int event_y){
        return  Math.abs( event_x -x ) < r && Math.abs( event_y - y ) < r;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return  gestureDetector.onTouchEvent(event);
    }

    @Override
    public boolean onDown(MotionEvent e) {
        //按下时记录控件位置
        Left = (int) textView.getX();
        Right = textView.getWidth()+Left;
        Top = (int) textView.getY();
        Bottom = textView.getHeight()+Top;
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {

        if (isInSquare( Left, Top , (int)e1.getRawX() , (int)e1.getRawY() )){   //获取左上角
            textView.setX(e2.getRawX());
            textView.setY(e2.getRawY() - winTop);
            textView.setHeight((int) (Bottom - e2.getRawY() + winTop));
            textView.setWidth((int) (Right - e2.getRawX()));
        }else if ( isInSquare( Right, Bottom , (int)e1.getRawX() , (int)e1.getRawY() )){   //获取右下角
            textView.setHeight((int) e2.getRawY() - Top - winTop);
            textView.setWidth((int) e2.getRawX() - Left);
        }else if( isInSquare( Right, Top , (int)e1.getRawX() , (int)e1.getRawY() )){    //获取右上角
            textView.setY(e2.getRawY() - winTop);
            textView.setWidth((int) e2.getRawX() - Left);
            textView.setHeight((int) (Bottom - e2.getRawY() + winTop));
        }else if( isInSquare( Left, Bottom , (int)e1.getRawX() , (int)e1.getRawY() )){     //获取左下角
            textView.setX(e2.getRawX());
            textView.setHeight((int) ( e2.getRawY() - winTop - Top ));
            textView.setWidth((int) (Right - e2.getRawX()));
        }else if( Math.abs(e1.getRawX() - Left) < r ){    //            textView.setX((int) e2.getRawX());
            textView.setWidth((int) (Right - e2.getRawX()));
        }else if ( Math.abs(e1.getRawY() - winTop - Top) < r ){  //            textView.setY((int) e2.getRawY() - winTop );
            textView.setHeight((int) (Bottom - e2.getRawY() + winTop));
        }else if ( Math.abs(e1.getRawX() - Right ) < r ){    //            textView.setWidth((int) e2.getRawX() - Left);
        }else if (Math.abs(e1.getRawY() - winTop - Bottom ) < r ){    //            textView.setHeight((int) e2.getY() - winTop - Top );
        }else if ((e1.getRawX() - r > Left ) && (e1.getRawY() - r  > Top + winTop )    //中间
                && (e1.getRawX() + r < Right ) && (e1.getRawY() + r < Bottom + winTop )){
            textView.setX(Left - e1.getRawX() + e2.getRawX() );
            textView.setY( Top - e1.getRawY() + e2.getRawY() );
        }else {
            Log.i("haha","触摸了框以外部分" );
        }

        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

        return false;
    }
}
2、xml文件

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </LinearLayout>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/view"
                android:background="@drawable/view_style"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <Space
                android:layout_weight="0.5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <Button
                android:id="@+id/view_ok"
                android:text="记录位置"
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <Space
                android:layout_weight="0.5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <Button
                android:id="@+id/view_cancel"
                android:text="取消"
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <Space
                android:layout_weight="0.5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </LinearLayout>


</FrameLayout>
3、style.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false">
        <shape >
            <stroke android:color="#ff0000" android:width="1dp"/>
            <solid android:color="#00000000"/>
        </shape>
    </item>

</selector>


最后来一张效果图吧








猜你喜欢

转载自blog.csdn.net/guang_liang_/article/details/60142565