1、簡介
最近,因爲項目需要,需要製作一個界面上可拖拽的按鈕,網上也有多實例,看了下大部分都是示例不全或講解不清晰,效果圖也不明顯,藉此自己記錄一番自己的實現方案,以備不時之需,同時也爲廣大學者可以直接通過拷貝方式完成項目所需。
2、效果圖
在開始代碼之前,首先看看效果圖,如下:
3、核心代碼實現
3.1 DraggingButton 實現
public class DraggingButton extends android.support.v7.widget.AppCompatButton {
private int lastX = 0;
private int lastY = 0;
private int beginX = 0;
private int beginY = 0;
private int screenWidth = 720;
private int screenHeight = 1280;
public DraggingButton(Context context) {
this(context, null);
}
public DraggingButton(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public DraggingButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initData(context);
}
private void initData(Context context){
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
screenWidth = dm.widthPixels;
screenHeight = dm.heightPixels;
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
lastX = (int) event.getRawX(); // 觸摸點與屏幕左邊的距離
lastY = (int) event.getRawY(); // 觸摸點與屏幕上邊的距離
beginX = lastX;
beginY = lastY;
break;
case MotionEvent.ACTION_MOVE:
int dx =(int)event.getRawX() - lastX; // x軸拖動的絕對距離
int dy =(int)event.getRawY() - lastY; // y軸拖動的絕對距離
// getLeft(): 子View的左邊界到父View的左邊界的距離, getRight():子View的右邊界到父View的左邊界的距離
// 如下幾個數據表示view應該在佈局中的位置
int left = getLeft() + dx;
int top = getTop() + dy;
int right = getRight() + dx;
int bottom = getBottom() + dy;
if(left < 0){
left = 0;
right = left + getWidth();
}
if(right > screenWidth){
right = screenWidth;
left = right - getWidth();
}
if(top < 0){
top = 0;
bottom = top + getHeight();
}
if(bottom>screenHeight){
bottom = screenHeight;
top = bottom - getHeight();
}
layout(left, top, right, bottom);
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
break;
case MotionEvent.ACTION_UP:
// 解決拖拽的時候鬆手點擊事件觸發
if (Math.abs(lastX - beginX) < 10 && Math.abs(lastY - beginY) < 10){
return super.onTouchEvent(event);
}else{
setPressed(false);
return true;
}
default:
break;
}
return super.onTouchEvent(event);
}
}
核心代碼已經奉獻,通過自定義的DraggingButton即可實現可拖拽功能,具體原理主要在於onTouchEvent和layout兩個函數的使用,具體細節不在講述,代碼註釋比較清晰。
4、舉個栗子
4.1 activity中的佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.android.study.example.uidemo.dragging.DraggingButton
android:id="@+id/tv_dragging"
android:layout_width="80dp"
android:layout_height="80dp"
android:gravity="center"
android:background="@drawable/drag_button_bg"
android:layout_margin="20dp"
android:padding="10dp"
android:text="懸浮\n按鈕1"
android:textSize="15sp"
android:layout_gravity="right"
android:textColor="#ffffff"/>
</LinearLayout>
4.2 樣式 drag_button_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="oval">
<!--填充顏色-->
<solid android:color="#bf39b500" />
<!--描邊-->
<stroke android:width="2dp" android:color="#bf39b500" />
</shape>
</item>
<item>
<shape android:shape="oval">
<!--填充顏色-->
<solid android:color="#ff8bc34a" />
<!--描邊-->
<stroke android:width="2dp" android:color="#bf39b500"/>
</shape>
</item>
</selector>
4.3 activity 中的代碼
private DraggingButton mDraggintView;
mDraggintView = (DraggingButton) findViewById(R.id.tv_dragging);
mDraggintView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(FloatingActionBtnTestActivity.this, "click", Toast.LENGTH_SHORT).show();
}
});