3

作者: JokerLiang | 来源:发表于2016-08-19 21:18 被阅读0次

结构

1:定义一个半透明的Activity
2:使控件可以拖拽,并记录下拖拽后控件的位置

1:定义一个半透明的Activity

        <activity android:name=".Show_Toast_Location"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"/>
直接在清单文件中加属性,
因为是原来的AC是白色底,
所以会出现全透明的样子,还需将原来的AC背景颜色换成灰色,
这样才能显出半透明的感觉。
      android:background="#6a0a0a0a"

2:使控件可以拖拽,并记录下拖拽后控件的位置

1:找到控件并设置一个触摸监听器
  iv_show_toast_location.setOnTouchListener(new View.OnTouchListener()
2:实现触摸监听器接口中的方法 (MotionEvent 这个参数指手势动作)
  public boolean onTouch(View view, MotionEvent motionEvent)
3:拿到手势动作与用户手势动作进行匹配
  motionEvent.getAction()
        MotionEvent.ACTION_DOWN://(按下去的参数)
        MotionEvent.ACTION_MOVE: //(移动时候的参数)
        MotionEvent.ACTION_UP
4:第一次按住屏幕(MotionEvent.ACTION_DOWN)拿到按下去的X 和 Y
  case    MotionEvent.ACTION_DOWN://(按下去的参数)
                        startX = (int) motionEvent.getRawX();
                        startY = (int) motionEvent.getRawY();
                        break;
5:按住控件进行移动的时候进行计算
case    MotionEvent.ACTION_MOVE: //(移动时候的参数)
                        int MoveX = (int) motionEvent.getRawX();  //拿到移动时X的坐标
                        int MoveY = (int) motionEvent.getRawY();  ////拿到移动时X的坐标
                        
                        //拿到偏移量
                        int desX = MoveX - startX;
                        int desY = MoveY - startY;

                        //下面是移动前的位置

                        //确定一个矩形只需要两个点
                        //拿到当前控件,确定原来左,上角位置
                        int left = iv_show_toast_location.getLeft();
                        int top = iv_show_toast_location.getTop();
                        //拿到当前控件,确定原来右,下角位置
                        int right = iv_show_toast_location.getRight();
                        int bottom = iv_show_toast_location.getBottom();

                       //下面是移动后的位置
                       //移动后的左 , 上 ,右 , 下 就是原来的加上偏移的
                        int Mleft = left + desX;
                        int Mtop = top + desY;
                        int Mright = right + desX;
                        int Mbotton = bottom + desY;


                        //容错处理,不能让控件超出屏幕位置
                        //以原点为坐标系  左  和 上 不能小于0
                        if (Mleft<0){
                            return true;
                        }
                        if(Mtop<0){
                            return true;
                        }

                        //以原点为坐标系  右  和 下 不能小于屏幕宽度
                        if (Mright>WindowsWidth){
                            return true;
                        }
                        if (Mbotton>windowsHeight-22){
                            return true;
                        }

                        //是手机按照移动的过程去做展示

                        iv_show_toast_location.layout(Mleft,Mtop,Mright,Mbotton);

                        //重置开始坐标
                        startX = (int) motionEvent.getRawX();
                        startY = (int) motionEvent.getRawY();
                        break;
6:抬手的时候记录下当前的X和Y值
                    case    MotionEvent.ACTION_UP: //(抬手的参数)
                        //抬手 就记录位置 在SP中
                        SpUtil.PutInt(Show_Toast_Location.this, FianlMath.TOAST_LEFT,iv_show_toast_location.getLeft());
                        SpUtil.PutInt(Show_Toast_Location.this, FianlMath.TOAST_TOP,iv_show_toast_location.getTop());
                        break;
7:这个方法返回值为Boolean值
                       //这里要改成返回true 才能移动
                        //如果既要相应点击 又要相应拖动 则要改成false
                        return true;
全部代码
public class Show_Toast_Location extends Activity {

    private ImageView iv_show_toast_location;
    private Button bt_show_toast_location_belowp;
    private Button bt_show_toast_location_top;
    private WindowManager windowManager;
    private int windowsHeight;
    private int WindowsWidth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_toast_location);
        initUi();
        initData();
    }


    private void initUi() {
        iv_show_toast_location = (ImageView) findViewById(R.id.iv_show_Toast_Location);
        bt_show_toast_location_top = (Button) findViewById(R.id.bt_show_Toast_Location_top);
        bt_show_toast_location_belowp = (Button) findViewById(R.id.bt_show_Toast_Location_below);

        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        //拿到屏幕宽度和高度
        WindowsWidth = windowManager.getDefaultDisplay().getWidth();
        windowsHeight = windowManager.getDefaultDisplay().getHeight();
        //通过下面拿到的左上角坐标,设置及到控件上做回显操作
        int MaginLeft = SpUtil.getInt(this, FianlMath.TOAST_LEFT, 0);
        int MaginTop = SpUtil.getInt(this, FianlMath.TOAST_TOP, 0);

        //设置控件位置,应该拿到控件的布局来提供
        //下面的参数作用是 在指定当前布局内的控件的大小
        //定义一个规则
        RelativeLayout.LayoutParams layoutParams =
                new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        //指定控件位置
        //将左上角的坐标作用到控件上
        layoutParams.leftMargin = MaginLeft;
        layoutParams.topMargin = MaginTop;
        //将这个规则作用在想要这样做的控件上
        iv_show_toast_location.setLayoutParams(layoutParams);

        //上下两段文字的显示
        if (MaginTop>windowsHeight/2){
            bt_show_toast_location_top.setVisibility(View.VISIBLE);
            bt_show_toast_location_belowp.setVisibility(View.INVISIBLE);
        }else {
            bt_show_toast_location_top.setVisibility(View.INVISIBLE);
            bt_show_toast_location_belowp.setVisibility(View.VISIBLE);
        }
    }
    private void initData() {
        //设置触摸的监听器
        iv_show_toast_location.setOnTouchListener(new View.OnTouchListener() {

            private int startX;
            private int startY;

            //(motionEvent)这个参数是移动的数据(行为)
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch(motionEvent.getAction()){
                    case    MotionEvent.ACTION_DOWN://(按下去的参数)
                        startX = (int) motionEvent.getRawX();
                        startY = (int) motionEvent.getRawY();
                        break;
                    case    MotionEvent.ACTION_MOVE: //(移动时候的参数)
                        int MoveX = (int) motionEvent.getRawX();  //拿到移动时X的坐标
                        int MoveY = (int) motionEvent.getRawY();  ////拿到移动时X的坐标
                        
                        //拿到偏移量
                        int desX = MoveX - startX;
                        int desY = MoveY - startY;

                        //下面是移动前的位置

                        //确定一个矩形只需要两个点
                        //拿到当前控件,确定原来左,上角位置
                        int left = iv_show_toast_location.getLeft();
                        int top = iv_show_toast_location.getTop();
                        //拿到当前控件,确定原来右,下角位置
                        int right = iv_show_toast_location.getRight();
                        int bottom = iv_show_toast_location.getBottom();

                       //下面是移动后的位置
                       //移动后的左 , 上 ,右 , 下 就是原来的加上偏移的
                        int Mleft = left + desX;
                        int Mtop = top + desY;
                        int Mright = right + desX;
                        int Mbotton = bottom + desY;


                        //容错处理,不能让控件超出屏幕位置
                        //以原点为坐标系  左  和 上 不能小于0
                        if (Mleft<0){
                            return true;
                        }
                        if(Mtop<0){
                            return true;
                        }

                        //以原点为坐标系  右  和 下 不能小于屏幕宽度
                        if (Mright>WindowsWidth){
                            return true;
                        }
                        if (Mbotton>windowsHeight-22){
                            return true;
                        }


                        //上下两个文字的显示
                        if (Mtop>windowsHeight/2){
                            bt_show_toast_location_top.setVisibility(View.VISIBLE);
                            bt_show_toast_location_belowp.setVisibility(View.INVISIBLE);
                        }else {
                            bt_show_toast_location_top.setVisibility(View.INVISIBLE);
                            bt_show_toast_location_belowp.setVisibility(View.VISIBLE);
                        }

                        //是手机按照移动的过程去做展示

                        iv_show_toast_location.layout(Mleft,Mtop,Mright,Mbotton);

                        //重置开始坐标
                        startX = (int) motionEvent.getRawX();
                        startY = (int) motionEvent.getRawY();
                        break;
                    case    MotionEvent.ACTION_UP: //(抬手的参数)
                        //抬手 就记录位置 在SP中
                        SpUtil.PutInt(Show_Toast_Location.this, FianlMath.TOAST_LEFT,iv_show_toast_location.getLeft());
                        SpUtil.PutInt(Show_Toast_Location.this, FianlMath.TOAST_TOP,iv_show_toast_location.getTop());
                        break;
                }
                //这里要改成返回true 才能移动
                //如果既要相应点击 又要相应拖动 则要改成false
                return true;
            }
        });

    }
}
图片

![getRowX和getX差异.png](https://img.haomeiwen.com/i2743980/27e0d81aa8878ca0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![多击事件图片.png](https://img.haomeiwen.com/i2743980/2c09e552c64038d1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![下边缘显示范围.png](https://img.haomeiwen.com/i2743980/76a6994f63da028e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![移动过程.png](https://img.haomeiwen.com/i2743980/714ae42d1e52c0ca.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![左上右下坐标获取.png](https://img.haomeiwen.com/i2743980/57bf86961d8fd615.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![getRowX和getX差异.jpg](https://img.haomeiwen.com/i2743980/2fb27b2af79e84aa.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

相关文章

  • 恶意文件夹

    【%你的iapp在这里哦/恭喜你找到了/3/3/3/3/3/3/3/3/3/3/3/3/3/3/3/3/3/3/3...

  • 3+3+3

    九年了,不曾去过,马路那边的刘家村。唱戏,小路~抓蝌蚪,洗衣服,捞水草,漩涡~种满菜的田地,养着奶牛的茅草屋,充满...

  • 3/3

    郭一博 刘佐千 李文浩 王天聪 柳絮 刘全利 李明东

  • 3/3

  • if(a==3) or if(3==a)

    记得刚写程序那会儿,遇到 if else 的条件判断逻辑,基本都会这样写:if(a==3) 为什么呢? 因为自然...

  • 3/3

    原先我是为了他留长头发,现在他的女朋友剪了短发,他说随她去,都好。 原先她卑微付出真心为他,现在她是个被宠溺的幸福...

  • 3/3

    夜月再至,只剩着静谧和四寂与我作伴, 呼啸而过,耳畔又闻过车马还川流不息, 旧亿渐入,也始终囚于泯然其细枝末节。 ​​​

  • 3:3

    今天是个晴朗的天气。染俄我只想呆在寝室。

  • 美惠教练3  3  3  3

  • 做乘法的意义练习时的问题

    4×3 4+3 4×4×4 4+4+4 3+3+3+3 3×3×3×3 2×3 3×3 第一单元要学完了,通过测评

网友评论

      本文标题:3

      本文链接:https://www.haomeiwen.com/subject/ftobsttx.html