安卓控件拖动和点击事件冲突处理----可拖动的FloatingActionButton

  直接上代码,具体为什么自行百度吧。额。。就是这么任性?
/**
 * Created by ${davidMa} on 2017/5/23.
 * com.aochuang.megstar.widget
 * 描述:
 * 公司:aochuang01
 */
public class MyFloatActionButton extends FloatingActionButton {
    int dx, dy;
    private int lastx, lasty;//记录手指按下去的位置
    private int startX, startY, endX, endY;
    private long startTime = 0;
    private long endTime = 0;
    private boolean isclick;

    public MyFloatActionButton(Context context) {

        super(context, null);
    }

    public MyFloatActionButton(Context context, AttributeSet attrs) {
        super(context, attrs, 0);
    }

    public MyFloatActionButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void init() {
        setSaveEnabled(true);
    }

    @Override
    protected Parcelable onSaveInstanceState() {
        LogUtils.d("onSaveInstanceState satrtX=" + startX);
        Parcelable superState = super.onSaveInstanceState();
        SavedState ss = new SavedState(superState);
        ss.state = startX;
        return ss;
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        LogUtils.d("onRestoreInstanceState satrtX" + ((SavedState) state).state);
        super.onRestoreInstanceState(state);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:// 获取手指第一次接触屏幕
                dx = 0;
                dy = 0;
                isclick = false;//当按下的时候设置isclick为false,具体原因看后边的讲解
                startTime = System.currentTimeMillis();
                break;
            case MotionEvent.ACTION_MOVE:// 手指在屏幕上移动对应的事件
                int x = (int) event.getRawX();
                int y = (int) event.getRawY();
                // 获取手指移动的距离
                dx = x - lastx;
                dy = y - lasty;
                setAlpha(0.5f);
                int l = getLeft();
                int r = getRight();
                int t = getTop();
                int b = getBottom();
                LogUtils.d("上面的位置是" + (l + dx) + "右边的坐标是" + (t + dy));
                // 更改在窗体的位置//确保位置显示在屏幕内
                if ((l + dx) >= 0 && (t + dy) > 0 && (r + dx) <= UIUtils.screenWidthPixels() && (b + dy) <= UIUtils.screenHeightPixels())
                    layout(l + dx, t + dy, r + dx, b + dy);
                // 获取移动后的位置
                lastx = (int) event.getRawX();
                lasty = (int) event.getRawY();
                if (Math.abs(dx) > 20 || Math.abs(dy) > 20) {
                    LogUtils.d("Math.abs(dx)>20");
                    event.setAction(MotionEvent.ACTION_CANCEL);
                    return true;
                }
            case MotionEvent.ACTION_UP:// 手指离开屏幕对应事件
                // 记录最后图片在窗体的位置
                endTime = System.currentTimeMillis();
                //当从点击到弹起小于半秒的时候,则判断为点击,如果超过则不响应点击事件
                if ((endTime - startTime) > 0.1 * 1000L) {
                    isclick = true;
                } else {
                    isclick = false;
                }
                lastx = (int) event.getRawX();
                lasty = (int) event.getRawY();
                System.out.println("执行顺序up");
                setAlpha(1.0f);

        }
        if (isclick)
            return true;
        return super.dispatchTouchEvent(event);
    }

    static class SavedState extends BaseSavedState {
        public static final Parcelable.Creator<SavedState> CREATOR
                = new Parcelable.Creator<SavedState>() {

            @Override
            public SavedState createFromParcel(Parcel source) {
                return new SavedState(source);
            }

            public SavedState[] newArray(int size) {
                return new SavedState[size];
            }
        };
        int state;

        SavedState(Parcelable superState) {
            super(superState);
        }

        private SavedState(Parcel in) {
            super(in);
            state = in.readInt();
        }

        @Override
        public void writeToParcel(Parcel out, int flags) {
            super.writeToParcel(out, flags);
            out.writeInt(state);
        }
    }
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值