/**
* Created by ylWang on 2018/7/23 18:22.
*/
public class MarkView extends RelativeLayout {
private ImageView ivTouch;
private RelativeLayout rlRect, rlRoot;
private LinearLayout llSummit;
private TextView tvCancel, tvSure;
private MarkView markView;
public MarkView(Context context) {
super(context);
initViews(context);
}
public MarkView(Context context, AttributeSet attrs) {
super(context, attrs);
initViews(context);
}
public MarkView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initViews(context);
}
private <T extends View> T f(int resId) {
return (T) findViewById(resId);
}
private void initViews(Context context) {
markView = this;
LayoutInflater.from(context).inflate(R.layout.view_mark, this, true);
ivTouch = f(R.id._iv_touch);
rlRect = f(R.id._rl_rect);
llSummit = f(R.id._ll_summit);
tvSure = f(R.id._tv_sure);
tvCancel = f(R.id._tv_cancel);
rlRoot = f(R.id._rl_root);
setListener();
}
private static final String TAG = "@@@";
private int lastX, lastY, offsetX, offsetY;
private final static int MIN_WH = 50;
private int toRight, toBottom;
private void setListener() {
ivTouch.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
llSummit.setVisibility(INVISIBLE);
lastX = x;
lastY = y;
break;
case MotionEvent.ACTION_MOVE:
offsetX = x - lastX;
offsetY = y - lastY;
toRight = rlRect.getRight() + offsetX;
toBottom = rlRect.getBottom() + offsetY;
if ((rlRect.getLeft() + MIN_WH > toRight) || (rlRect.getTop() + MIN_WH > toBottom)) {//控制最小区域
break;
} else if (toRight > rlRoot.getRight() || toBottom > rlRoot.getBottom()) {//不许出界
break;
} else {
ivTouch.offsetLeftAndRight(offsetX);
ivTouch.offsetTopAndBottom(offsetY);
llSummit.offsetLeftAndRight(offsetX);
llSummit.offsetTopAndBottom(offsetY);
rlRect.layout(rlRect.getLeft(), rlRect.getTop(), toRight, toBottom);
areaListener.changePoint(getPosition(rlRect, rlRoot));
}
break;
case MotionEvent.ACTION_UP:
llSummit.setVisibility(VISIBLE);
break;
default:
break;
}
return true;
}
});
rlRect.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
llSummit.setVisibility(INVISIBLE);
lastX = x;
lastY = y;
break;
case MotionEvent.ACTION_MOVE:
offsetX = x - lastX;
offsetY = y - lastY;
if (-offsetX > rlRect.getLeft()
|| rlRect.getRight() + offsetX > rlRoot.getRight()
|| -offsetY > rlRect.getTop()
|| rlRect.getBottom() + offsetY > rlRoot.getBottom()
) {
break;
} else {
ivTouch.offsetLeftAndRight(offsetX);
ivTouch.offsetTopAndBottom(offsetY);
rlRect.offsetLeftAndRight(offsetX);
rlRect.offsetTopAndBottom(offsetY);
llSummit.offsetLeftAndRight(offsetX);
llSummit.offsetTopAndBottom(offsetY);
areaListener.changePoint(getPosition(rlRect, rlRoot));
}
break;
case MotionEvent.ACTION_UP:
llSummit.setVisibility(VISIBLE);
break;
default:
break;
}
return true;
}
});
ClickListener listener = new ClickListener();
tvSure.setOnClickListener(listener);
tvCancel.setOnClickListener(listener);
}
private final class ClickListener implements OnClickListener {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id._tv_sure:
Log.d(TAG, "Sure_onTouch: " + Arrays.toString(getPosition(rlRect, rlRoot)));
areaListener.setPoints(getPosition(rlRect, rlRoot));
((ViewGroup) markView.getParent()).removeView(markView);
break;
case R.id._tv_cancel:
areaListener.cancel();
((ViewGroup) markView.getParent()).removeView(markView);
break;
default:
break;
}
}
}
private int[] getPosition(View view, ViewGroup viewRoot) {
return new int[]{view.getLeft(), view.getTop(), view.getRight(), view.getBottom(), viewRoot.getRight(), viewRoot.getBottom()};
}
public interface AreaListener {
void changePoint(int[] pos);
void setPoints(int[] pos);
void cancel();
}
private AreaListener areaListener;
public void setAreaListener(AreaListener areaListener) {
this.areaListener = areaListener;
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/_rl_root"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/_rl_rect"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:background="@drawable/red_rect"/>
<ImageView
android:id="@+id/_iv_touch"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_below="@id/_rl_rect"
android:layout_marginLeft="-20dp"
android:layout_marginTop="-20dp"
android:layout_toRightOf="@id/_rl_rect"
android:background="@drawable/red_point" />
<LinearLayout
android:id="@+id/_ll_summit"
android:layout_width="65dp"
android:layout_height="30dp"
android:layout_above="@id/_iv_touch"
android:layout_marginRight="30dp"
android:layout_toLeftOf="@id/_iv_touch"
android:background="@android:color/holo_orange_dark"
android:orientation="horizontal">
<TextView
android:id="@+id/_tv_cancel"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@android:color/holo_blue_bright"
android:gravity="center"
android:text="No" />
<TextView
android:id="@+id/_tv_sure"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="5dp"
android:background="@android:color/holo_green_light"
android:gravity="center"
android:text="Yes" />
</LinearLayout>
</RelativeLayout>