Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 自定義:輕鬆實現全屏任意拖動的控件

編輯:關於 Android 程式設計

這個效果讓我想起之前做過的「模擬小火箭」,原理頗有相似之處,正好拿來做個知識補充,於是轉載過來一起學習,也推薦大家關注原作者的文章。

效果很直觀:用戶可以在屏幕範圍內隨心所欲地拖動一個控件。布局文件非常簡單,就是一個 Button:

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/id_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
</RelativeLayout>

核心邏輯在 MainActivity 中,通過實現 OnTouchListener 接口來跟蹤觸摸事件:

public class MainActivity extends Activity implements View.OnTouchListener {
    private Button mButton;
    private ViewGroup mViewGroup;
    private int xDelta;
    private int yDelta;
    public static final String TAG = "YAYUN";

    @Override public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mViewGroup = (ViewGroup) findViewById(R.id.root);
        mButton = (Button) findViewById(R.id.id_text);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.leftMargin = 50;
        layoutParams.topMargin = 50;
        mButton.setLayoutParams(layoutParams);
        mButton.setOnTouchListener(this);
    }

    @Override public boolean onTouch(View view, MotionEvent event) {
        final int x = (int) event.getRawX();
        final int y = (int) event.getRawY();
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view.getLayoutParams();
                xDelta = x - params.leftMargin;
                yDelta = y - params.topMargin;
                break;
            case MotionEvent.ACTION_MOVE:
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
                int xDistance = x - xDelta;
                int yDistance = y - yDelta;
                layoutParams.leftMargin = xDistance;
                layoutParams.topMargin = yDistance;
                view.setLayoutParams(layoutParams);
                break;
        }
        mViewGroup.invalidate();
        return true;
    }
}

下面拆解一下這段代碼的關鍵:

MainActivity 實現了 OnTouchListener 接口並覆寫 onTouch 方法,每次回調都會通過 x、y 記錄當前觸摸坐標。

在 ACTION_DOWN(按下時觸發,僅一次)中,用 xDelta、yDelta 記錄下按點相對於控件左上角的距離,也就是「相對偏移量」。

在 ACTION_MOVE(移動時不斷觸發)中,用 xDistance、yDistance 計算出新的相對位置,把它們賦值給 leftMargin 和 topMargin,再動態設置給控件。

最後調用 invalidate 方法刷新控件位置。

光看文字不太好理解,我們可以結合 Log 來體會:

假設按下時坐標為 (131, 75),由於 onCreate 中初始的 leftMargin、topMargin 都是 50,所以此時 xDelta = 131 - 50 = 81,yDelta = 75 - 50 = 25。第一個 ACTION_MOVE 的 xDistance = 132 - 81 = 51,yDistance = 80 - 25 = 55,之後便循環往復。這裡最關鍵的就是「相對距離」這個概念——計算的始終是相對於初始偏移的距離。

這個效果直接的落地場景不算多,但其中的處理思路在實際開發中卻非常常用,onTouch 更是控件交互裡的高頻方法,值得爛熟於心。希望這個小例子能給你帶來一點思維上的碰撞。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved