android 拖放功能,Android - 可移动/可拖动浮动按钮(FAB)

基于this answer for another SO question这是我的代码创建。这似乎是(有工作点击功能)很好地工作,而不是依赖于FAB的父布局或定位...

package com.example;

import android.content.Context;

import android.support.design.widget.FloatingActionButton;

import android.util.AttributeSet;

import android.view.MotionEvent;

import android.view.View;

public class MovableFloatingActionButton extends FloatingActionButton implements View.OnTouchListener {

private final static float CLICK_DRAG_TOLERANCE = 10; // Often, there will be a slight, unintentional, drag when the user taps the FAB, so we need to account for this.

private float downRawX, downRawY;

private float dX, dY;

public MovableFloatingActionButton(Context context) {

super(context);

init();

}

public MovableFloatingActionButton(Context context, AttributeSet attrs) {

super(context, attrs);

init();

}

public MovableFloatingActionButton(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

init();

}

private void init() {

setOnTouchListener(this);

}

@Override

public boolean onTouch(View view, MotionEvent motionEvent){

int action = motionEvent.getAction();

if (action == MotionEvent.ACTION_DOWN) {

downRawX = motionEvent.getRawX();

downRawY = motionEvent.getRawY();

dX = view.getX() - downRawX;

dY = view.getY() - downRawY;

return true; // Consumed

}

else if (action == MotionEvent.ACTION_MOVE) {

int viewWidth = view.getWidth();

int viewHeight = view.getHeight();

View viewParent = (View)view.getParent();

int parentWidth = viewParent.getWidth();

int parentHeight = viewParent.getHeight();

float newX = motionEvent.getRawX() + dX;

newX = Math.max(0, newX); // Don't allow the FAB past the left hand side of the parent

newX = Math.min(parentWidth - viewWidth, newX); // Don't allow the FAB past the right hand side of the parent

float newY = motionEvent.getRawY() + dY;

newY = Math.max(0, newY); // Don't allow the FAB past the top of the parent

newY = Math.min(parentHeight - viewHeight, newY); // Don't allow the FAB past the bottom of the parent

view.animate()

.x(newX)

.y(newY)

.setDuration(0)

.start();

return true; // Consumed

}

else if (action == MotionEvent.ACTION_UP) {

float upRawX = motionEvent.getRawX();

float upRawY = motionEvent.getRawY();

float upDX = upRawX - downRawX;

float upDY = upRawY - downRawY;

if (Math.abs(upDX) < CLICK_DRAG_TOLERANCE && Math.abs(upDY) < CLICK_DRAG_TOLERANCE) { // A click

return performClick();

}

else { // A drag

return true; // Consumed

}

}

else {

return super.onTouchEvent(motionEvent);

}

}

}

这里是XML ...

android:id="@+id/fab"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="bottom|end"

android:layout_margin="@dimen/fab_margin"

android:src="@drawable/ic_navigate_next_white_24dp"/>

基本上,您只需在您的XML中将android.support.design.widget.FloatingActionButton替换为com.example.MovableFloatingActionButton即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值