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);
}
}