效果圖
//將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() {@Overridepublic 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() {@Overridepublic 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; //表示還可以監聽其他事件 }}); }
?