dragView
因項目新需求需要添加一個屏幕拖拽按鈕可以彈出菜單的控件,因爲不是我做的閒來無事寫一個demo吧 可能存在一些小bug(畢竟就寫了幾個小時)兄弟姐妹們理解思路就行 具體的可以自己調試一下 廢話不多說先來一個gif走一走(調了幀數 可能看着比較快 不要介意)
GitHub 傳送車https://github.com/guanhaoran/dragView
- gif有點小 對付看吧 (太大了傳github的時候 會出錯 放圖片還看不出來效果 不要介意)
廢話不多話直接看代碼
1.寫一個類繼承 Button 重寫構造方法&onTouchEvent(MotionEvent event)
public DragView(Context context) { this(context, null); } public DragView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public DragView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.context = context; //獲取屏幕寬高,用於控制控件在屏幕內移動 DisplayMetrics dm = getResources().getDisplayMetrics(); mScreenWidth = dm.widthPixels; mScreenHeight = dm.heightPixels - 100;//這裏減去的100是下邊的back鍵和menu鍵那一欄的高度,看情況而定 showPopup = new ShowPopup(context); }
2.onTouchEvent(MotionEvent event)
大家可能看的不太懂 先大體看下結構 稍後我會在代碼里加上註釋 確保每條代碼都有註釋一些沒有的 我就不會加註釋 也浪費大家時間
public boolean onTouchEvent(MotionEvent event) { super.onTouchEvent(event); int lastMoveX = 0; int lastMoveY = 0; switch (event.getAction()) { case MotionEvent.ACTION_DOWN: falg = true; startDownX = (int) event.getRawX(); startDownY = (int) event.getRawY(); break; case MotionEvent.ACTION_MOVE: if (showPopup.isShowing()) { showPopup.dissPopup(); } lastMoveX = (int) event.getRawX(); lastMoveY = (int) event.getRawY(); int dx = lastMoveX - startDownX; int dy = lastMoveY - startDownY; if (Math.abs(dx) > 2 || Math.abs(dy) > 2) { falg = false; } left = getLeft() + dx; top = getTop() + dy; right = getRight() + dx; bottom = getBottom() + dy; if (left < 0) { left = 0; right = left + getWidth(); } if (right > mScreenWidth) { right = mScreenWidth; left = right - getWidth(); } if (top < 0) { top = 0; bottom = top + getHeight(); } if (bottom > mScreenHeight) { bottom = mScreenHeight; top = bottom - getHeight(); } layout(left, top, right, bottom); Log.i("____________________", left + "___" + top + "___" + right + "___" + bottom); invalidate(); startDownX = (int) event.getRawX(); startDownY = (int) event.getRawY(); break; case MotionEvent.ACTION_UP: int lastMoveDx = Math.abs((int) event.getRawX() - startDownX); int lastMoveDy = Math.abs((int) event.getRawY() - startDownY); if (falg) { ToastUtils.showToast(context, "我點擊了"); showContent(360, true); } falg = false; break; } return true;
3.點擊按鈕出現的一個小圓點 這個使用popupWindow做的包括背景顏色變暗也是在popupWindow中實現的 這個稍後也會有註釋 我只是寫完了 不喜歡寫註釋
(這個毛病很壞 - -)private void showContent(int angles, boolean isShow) { if (showPopup.isShowing()) { showPopup.dissPopup(); } else { int excursionX = -(getWidth() / 2) - 5; int excursionY = 0; if (left == 0 && right == 0 && top == 0 && bottom == 0) { excursionX = -(getWidth() / 2)-5; excursionY = 0; } else if (left < 50 && top < 50) { excursionX = getWidth(); excursionY = 0; } else if (mScreenWidth - right < 50 && top < 50) { excursionX = -getWidth() * 2 - 20; excursionY = 0; } else if (mScreenWidth - right < 50 && mScreenHeight - bottom < getHeight()*2) { excursionX = -getWidth() * 2 - 20; excursionY = -getHeight() * 3 - 20; } else if (mScreenHeight - bottom < getHeight()*2 && left < 50) { excursionX = getWidth(); excursionY = -getHeight() * 3 - 20; } else if (left < 50) { excursionX = getWidth(); excursionY = -getHeight() - getHeight() / 2; } else if (top < 50) { excursionX = -(getWidth() / 2)-10; excursionY = -10; } else if (mScreenWidth - right < 50) { excursionX = -getWidth() * 2 - 10; excursionY = -getHeight() - getHeight() / 2 - 10; } else if (mScreenHeight - bottom < getHeight() * 2.5) { excursionX = -getWidth() / 2 -10; excursionY = -getHeight() * 3 - 10; } else { } showPopup.showPopup(this, excursionX, excursionY); } }
4.還有一個popupWindow 我就不貼在這裏了 大家可以看代碼就好了,可能代碼包結構很亂大家不要介意,我也是爲了效果別的我也沒注意
#
- 最後我在上傳GitHub的時候 哇!!!很痛苦 全是英文(奈何我的英文還不好)傳了好久才傳上去 下一篇文章我會把GitHub的使用給兄弟們詳詳細細的說一遍
QQ:765307272
- GitHub 傳送車https://github.com/guanhaoran/dragView