当前位置:   article > 正文

可拖拽移动控件的实现方式(两种)_if (math.abs(event.getrawx() - lastx) < 5 && math.

if (math.abs(event.getrawx() - lastx) < 5 && math.abs(event.getrawy() - last

  最近在学习android,忽然思考到一些直播软件有的那种拖拽悬浮播放框的效果是如何做的,所以去网上搜了搜,特意记录下来:

两种方式都需要给控件添加onTouchListenner()监听,并处理其中的方法:

 第一种:         

  1. public class ThirdActivity extends AppCompatActivity {
  2. private Button button;
  3. @Override
  4. protected void onCreate(@Nullable Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.ac_third);
  7. newWindow();
  8. }
  9. private void newWindow() {
  10. final Button button = new Button(this);
  11. button = new Button(this);
  12. button.setBackground(getResources().getDrawable(R.drawable.shape_main_floating));
  13. button.setText("风险\n应急");
  14. button.setPadding(5,5,5,5);
  15. button.setTextColor(Color.parseColor("#FFFFFF"));
  16. button.setVisibility(View.GONE);
  17. button.setOnClickListener(new View.OnClickListener() {
  18. @Override
  19. public void onClick(View v) {
  20. if (riskAccidentId != -1) {
  21. Bundle bundle = new Bundle();
  22. bundle.putString("id", riskAccidentId + "");
  23. RouteHelper.startFragment(MangoRouteURL.RiskAccidentDetailFragment, bundle);
  24. }
  25. }
  26. });
  27. final WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
  28. final WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT
  29. , WindowManager.LayoutParams.WRAP_CONTENT, 0, 0, PixelFormat.TRANSLUCENT);
  30. layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION;
  31. layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
  32. | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
  33. | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
  34. layoutParams.gravity = Gravity.TOP | Gravity.LEFT;
  35. layoutParams.x = getResources().getDisplayMetrics().widthPixels - DensityUtil.dip2px(MainBottomActivity.this, 80);
  36. layoutParams.y = getResources().getDisplayMetrics().heightPixels - DensityUtil.dip2px(MainBottomActivity.this, 160);
  37. layoutParams.width=DensityUtil.dip2px(MainBottomActivity.this, 60);
  38. layoutParams.height=DensityUtil.dip2px(MainBottomActivity.this, 60);
  39. layoutParams.windowAnimations=0;
  40. windowManager.addView(button, layoutParams);
  41. button.setOnTouchListener(new View.OnTouchListener() {
  42. public int lastX;
  43. public int lastY;
  44. public int X;
  45. @Override
  46. public boolean onTouch(View v, MotionEvent event) {
  47. switch (event.getAction()) {
  48. case MotionEvent.ACTION_DOWN:
  49. lastX = (int) event.getRawX();//获取触摸事件触摸位置的原始X坐标
  50. lastY = (int) event.getRawY();
  51. X=(int)event.getRawX();
  52. break;
  53. case MotionEvent.ACTION_MOVE:
  54. layoutParams.x+=(int) event.getRawX()-lastX;
  55. layoutParams.y+=(int) event.getRawY()-lastY;
  56. windowManager.updateViewLayout(v,layoutParams);
  57. lastX= (int) event.getRawX();
  58. lastY = (int) event.getRawY();
  59. break;
  60. case MotionEvent.ACTION_UP:
  61. if (Math.abs(event.getRawX() - X) < 10) {
  62. button.performClick();
  63. }
  64. return true;
  65. }
  66. return true;
  67. }
  68. });
  69. }
  70. }

第二种:

  1. package com.xuganwen.colorfullimage;
  2. import android.content.Context;
  3. import android.graphics.PixelFormat;
  4. import android.os.Bundle;
  5. import android.support.annotation.Nullable;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.view.Gravity;
  8. import android.view.MotionEvent;
  9. import android.view.View;
  10. import android.view.Window;
  11. import android.view.WindowManager;
  12. import android.widget.Button;
  13. import com.xuganwen.colorfullphoto.R;
  14. public class ThirdActivity extends AppCompatActivity {
  15. private Button button;
  16. @Override
  17. protected void onCreate(@Nullable Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.ac_third);
  20. newWindow();
  21. }
  22. private void newWindow() {
  23. button=(Button) findViewById(R.id.btn);
  24. button.setOnTouchListener(new View.OnTouchListener() {
  25. public int lastX;
  26. public int lastY;
  27. public float X;
  28. @Override
  29. public boolean onTouch(View v, MotionEvent event) {
  30. switch (event.getAction()) {
  31. case MotionEvent.ACTION_DOWN:
  32. lastX = (int) event.getRawX();//获取触摸事件触摸位置的原始X坐标
  33. lastY = (int) event.getRawY();
  34. X = event.getRawX();
  35. break;
  36. case MotionEvent.ACTION_MOVE:
  37. //event.getRawX();获得移动的位置
  38. int dx = (int) event.getRawX() - lastX;
  39. int dy = (int) event.getRawY() - lastY;
  40. int l = v.getLeft() + dx;
  41. if (l < 0) {
  42. l = 0;
  43. } else if (l > getResources().getDisplayMetrics().widthPixels - DensityUtil.dip2px(MainBottomActivity.this, 60)) {
  44. l = getResources().getDisplayMetrics().widthPixels - DensityUtil.dip2px(MainBottomActivity.this, 60);
  45. }
  46. int b = v.getBottom() + dy;
  47. if (b < DensityUtil.dip2px(MainBottomActivity.this, 60)) {
  48. b = DensityUtil.dip2px(MainBottomActivity.this, 60);
  49. } else if (b > getResources().getDisplayMetrics().heightPixels) {
  50. b = getResources().getDisplayMetrics().heightPixels;
  51. }
  52. int r = v.getRight() + dx;
  53. if (r < DensityUtil.dip2px(MainBottomActivity.this, 60)) {
  54. r = DensityUtil.dip2px(MainBottomActivity.this, 60);
  55. } else if (r > getResources().getDisplayMetrics().widthPixels) {
  56. r = getResources().getDisplayMetrics().widthPixels;
  57. }
  58. int t = v.getTop() + dy;
  59. if (t < 0) {
  60. t = 0;
  61. } else if (t > getResources().getDisplayMetrics().heightPixels) {
  62. t = getResources().getDisplayMetrics().heightPixels - DensityUtil.dip2px(MainBottomActivity.this, 60);
  63. }
  64. v.layout(l < 0 ? 0 : l, t < 0 ? 0 : t
  65. , r
  66. , b);
  67. lastX = (int) event.getRawX();
  68. lastY = (int) event.getRawY();
  69. v.postInvalidate();
  70. break;
  71. case MotionEvent.ACTION_UP:
  72. if (Math.abs(event.getRawX() - X) < 10) {
  73. button.performClick(); //如果是点击行为,则执行点击事件
  74. }
  75. return true;
  76. }
  77. return true;
  78. }
  79. });
  80. }
  81. }

公用布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <Button
  6. android:layout_width="wrap_content"
  7. android:layout_height="wrap_content"
  8. android:id="@+id/btn"
  9. android:text="xuganwen"
  10. />
  11. </RelativeLayout>

两种方式都能保证不会滑动到屏幕外,效果都还可以,第二种效果比较推荐,欢迎评论。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/261934
推荐阅读
相关标签
  

闽ICP备14008679号