zoukankan      html  css  js  c++  java
  • 悬浮按钮

    效果图

    //将dp转换为px
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
    
    //浮动图片监听
    private void initListener() {
        //弹出对话框
        jianwen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View view = LayoutInflater.from(getActivity()).inflate(R.layout.quick_layout, null);
                bbs = (TextView) view.findViewById(R.id.bbs);
                dailyattendance = (TextView) view.findViewById(R.id.dailyattendance);
                Jump.jumpToWeb(getActivity(), bbs, bbs.getText().toString(), Contents.MINE_LUNTAN_URL, false);
                Jump.jumpToWeb(getActivity(), dailyattendance, dailyattendance.getText().toString(), Contents.MINE_REGISTRATION_URL, false);
                
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setView(view);
                AlertDialog dialog = builder.create();
                Window window = dialog.getWindow();
                WindowManager.LayoutParams attributes = window.getAttributes();
                attributes.alpha = 0.6f;    //设置对话框透明度
                attributes.gravity = Gravity.BOTTOM;     //设置对话框的位置
                attributes.width = LinearLayout.LayoutParams.MATCH_PARENT;
                window.setAttributes(attributes);
                dialog.show();
            }
        });
    
    
        //移动浮动图片
        jianwen.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int widthPixels = getResources().getDisplayMetrics().widthPixels;   //屏幕宽度
                int heightPixels = getResources().getDisplayMetrics().heightPixels;     //屏幕高度
                int width = v.getWidth();   //图片宽带
                int height = v.getHeight();     //图片高度
                switch (event.getAction()) {
                    case MotionEvent.ACTION_MOVE:
                        //图片的绝对位置
                        float x = event.getRawX();
                        float y = event.getRawY();
                        //避免图片滑出屏幕
                        if (x > widthPixels - width) {
                            x = widthPixels - width;
                        }
                        if (y > heightPixels - height - dip2px(getContext(), 75)) {
                            y = heightPixels - height - dip2px(getContext(), 75);
                        }
                        v.setX(x);
                        v.setY(y);
                        break;
                }
                return false;   //表示还可以监听其他事件
            }
        });
    }
  • 相关阅读:
    记录一次阿里云服务器被攻击的经历
    post字符 特殊字符处理【转】
    forkjoin框架疑问记录
    centos7 安装 maven 和ant git 以及 rocketmq 4.2安装过程(安装成功,调用失败)
    IntelliJ Idea编译报错:javacTask: 源发行版 1.8 需要目标发行版 1.8
    记录一次json转换的经历
    maven项目在idea下右键不出现maven的解决办法
    Mac系统查看端口占用和杀死进程
    委托所想
    win8中的参数传递
  • 原文地址:https://www.cnblogs.com/anni-qianqian/p/5530620.html
Copyright © 2011-2022 走看看