1. 程式人生 > >Android拖動控制元件的實現,自定義可拖動的LinearLayout

Android拖動控制元件的實現,自定義可拖動的LinearLayout

工作內容;

1.拖動控制元件

2.自定義可拖動的LinearLayout

學習分享:

一、拖動控制元件的實現步驟:

【前提:控制元件在RelativeLayout中,或者在GridLayout中】

1.按下圖搞懂幾個座標

檢視寬度 view.getWidth();

檢視高度 view.getHeight() 

橘色線:view.getLeft()

藍色線:view.getRight()

紅色線:view.getTop()

粉色線:view.getBottom()

上下左右的偏移都是相對於(0.0)來說的

.

2. MotionEvent類中 getRowX()和 getX() 

1、event.getRowX():觸控點相對於螢幕原點的x座標

2、event.getX():    觸控點相對於其所在元件原點的x座標 


3.實現控制元件拖動程式碼段:【僅用於view,不適合viewGroup】效果:view跟著手指走

//獲取螢幕寬高,用於控制控制元件在螢幕內移動
DisplayMetrics dm = getResources().getDisplayMetrics();
screenWidth = dm.widthPixels;
screenHeight = dm.heightPixels - 100;//這裡減去的100是下邊的back鍵和menu鍵那一欄的高度,看情況而定

//核心程式碼段【OnTouchListener()的onTouch方法,控制元件去設定它就可以了】
@Override
public boolean onTouch(View v, MotionEvent event) {
    int action = event.getAction();
    switch (action) {
        case MotionEvent.ACTION_DOWN:
            lastX = (int) event.getRawX();//移動
            LogTool.e("發生Touch事件x:y____"":""<span lang="EN-US" style="" font-size:11.5pt;"="">\nrawX:rawY____"
            +event.getRawX()+":"+event.getRawY());
            //event.getRawX()事件點距離螢幕左上角的距離
            int dx = (int) event.getRawX() - lastX;
            int dy = (int) event.getRawY() - lastY;
            int left = v.getLeft() + dx;
            int top = v.getTop() + dy;
            int right = v.getRight() + dx;
            int bottom = v.getBottom() + dy;
            if (left < 0) { //最右邊
                right = screenWidth;
                left = right - v.getWidth();
            }
            if (top < 0) {  //最下邊
                bottom = screenHeight;
                top = bottom - v.getHeight();

            }
            v.layout(left, top, right, bottom);//再次將滑動其實位置定位
            lastY = (int) event.getRawY();
            break;
        case MotionEvent.ACTION_UP:
            break;
    }
    return true;
}
<!--可移動控制元件所在的父佈局需使用GridLayout或RelativeLayout-->
二、自定義可拖動linearLayout【類似於上面的拖動控制元件的程式碼,就直接貼java程式碼了】
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.widget.LinearLayout;

public class DragViewGroup extends LinearLayout {
    private int lastX,lastY,screenWidth,screenHeight;

    public DragViewGroup(Context context) {
        this(context, null);
    }

    public DragViewGroup(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DragViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        screenWidth = dm.widthPixels;
        screenHeight = dm.heightPixels-50;//減去下邊的高度
    }
    //定位
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        //可以在這裡確定這個viewGroup的:寬 = r-l.高 = b - t
    }
    //攔截touch事件
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
//        LogTool.e("onInterceptTouchEvent");
        int action = ev.getAction();
        switch (action){
            case MotionEvent.ACTION_DOWN:
                lastX = (int) ev.getRawX();//設定移動的初始位置相對位置
                lastY = (int) ev.getRawY();
                break;
            case MotionEvent.ACTION_MOVE://移動
                //event.getRawX()事件點距離螢幕左上角的距離
                int dx = (int) ev.getRawX() - lastX;
                int dy = (int) ev.getRawY() - lastY;

                int left = this.getLeft() + dx;
                int top = this.getTop() + dy;
                int right = this.getRight() + dx;
                int bottom = this.getBottom() + dy;
                if (left < 0) { //最左邊
                    left = 0;
                    right = left + this.getWidth();
                }
                if (right > screenWidth) { //最右邊
                    right = screenWidth;
                    left = right - this.getWidth();
                }
                if (top < 0) {  //最上邊
                    top = 0;
                    bottom = top + this.getHeight();
                }
                if (bottom > screenHeight) {//最下邊
                    bottom = screenHeight;
                    top = bottom - this.getHeight();
                }
                this.layout(left, top, right, bottom);//設定控制元件的新位置
//                LogTool.e("position:" + left + ", " + top + ", " + right + ", " + bottom);
                lastX = (int) ev.getRawX();//再次將滑動其實位置定位
                lastY = (int) ev.getRawY();
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        return super.onInterceptTouchEvent(ev);
    }
}

佈局程式碼;

<?xml version="1.0" encoding="utf-8"?>
<retrodemo.liu.com.retrofitdemo.view.DragViewGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/linear_save_share"
>
    <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp"
android:orientation="vertical"
android:background="@drawable/shape_save_share_linear"
>
        <ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/unsaved"
android:id="@+id/iv_save_ss"
/>
        <View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@color/white"
/>
        <ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/share"
android:id="@+id/iv_share_ss"
/>
    </LinearLayout>
</retrodemo.liu.com.retrofitdemo.view.DragViewGroup>
include程式碼:
<include layout="@layout/layout_save_share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="60dp"
/>