android控件拽不动,ANDROID控件拖拽

import android.annotation.SuppressLint;

import android.content.Context;

import android.support.annotation.NonNull;

import android.support.annotation.Nullable;

import android.util.AttributeSet;

import android.view.MotionEvent;

import android.view.ViewGroup;

import android.widget.FrameLayout;

import com.longmen.teacher.R;

import com.longmen.teacher.common.SimpleCallback;

import com.longmen.teacher.utils.DPUtils;

public class DragView extends FrameLayout {

private int mLastX;

private int mLastY;

private int mParentWidth;

private int mParentHeight;

private DragTextView.TextBean mTextBean;

private SimpleCallback mCallback;

public DragView(@NonNull Context context) {

super(context);

}

public DragView(@NonNull Context context, @Nullable AttributeSet attrs) {

super(context, attrs);

}

public DragView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

}

@SuppressLint("ClickableViewAccessibility")

@Override

public boolean onTouchEvent(MotionEvent event) {

ViewGroup mViewGroup = (ViewGroup) getParent();

if (null != mViewGroup && mParentWidth == 0) {

mParentWidth = mViewGroup.getWidth() - (int) getResources().getDimension(R.dimen.note_width);

mParentHeight = mViewGroup.getHeight();

}

int ea = event.getAction();

switch (ea) {

case MotionEvent.ACTION_DOWN:

mLastX = (int) event.getRawX();//获取触摸事件触摸位置的原始X坐标

mLastY = (int) event.getRawY();

case MotionEvent.ACTION_MOVE:

//event.getRawX();获得移动的位置

int dx = (int) event.getRawX() - mLastX;

int dy = (int) event.getRawY() - mLastY;

updateLayout(dx, dy);

mLastX = (int) event.getRawX();

mLastY = (int) event.getRawY();

postInvalidate();

break;

case MotionEvent.ACTION_UP:

if (mCallback != null) {

DragTextView.TextBean textBean = mTextBean;

textBean.x = getLeft();

textBean.y = getBottom();

textBean.t = getTop();

textBean.r = getRight();

mCallback.onResult(textBean);

setVisibility(GONE);

}

mTextBean = null;

break;

}

return true;

}

private void updateLayout(int dx, int dy) {

int l = getLeft() + dx;

int b = getBottom() + dy;

int r = getRight() + dx;

int t = getTop() + dy;

//下面判断移动是否超出屏幕

if (l < 0) {

l = 0;

r = l + getWidth();

}

if (t < 0) {

t = 0;

b = t + getHeight();

}

if (r > mParentWidth) {

r = mParentWidth;

l = r - getWidth();

}

if (b > mParentHeight) {

b = mParentHeight;

t = b - getHeight();

}

layout(l, t, r, b);

}

public void addDragView(String text, float size, SimpleCallback callback) {

removeAllViews();

setVisibility(VISIBLE);

postInvalidate();

mCallback = callback;

DragTextView mTextView = new DragTextView(getContext());

mTextBean = mTextView.getTextBean(text.trim(), size);

addView(mTextView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

int w = DPUtils.getScreenWidth(getContext()) - (int) getResources().getDimension(R.dimen.note_width);

int vW = (int) mTextView.getPaint().measureText(text);

if (vW > w) {

lp.rightMargin = (int) getResources().getDimension(R.dimen.note_width);

}

lp.topMargin = 200;

lp.leftMargin = 200;

setLayoutParams(lp);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值