From a9edaa040a2cfe36301ebb2e93c55afbe8075682 Mon Sep 17 00:00:00 2001 From: geduo83 Date: Wed, 16 Oct 2019 13:40:46 +0800 Subject: [PATCH] code perfect --- .idea/codeStyles/Project.xml | 116 +++++++++ app/src/main/AndroidManifest.xml | 26 +- .../com/mxdl/customview/MainActivity.java | 7 + .../customview/RectCaptureViewActivity.java | 13 + .../mxdl/customview/view/RectCaptureView.java | 238 ++++++++++++++++++ app/src/main/res/layout/activity_main.xml | 7 + .../res/layout/activity_rect_capture_view.xml | 9 + 7 files changed, 401 insertions(+), 15 deletions(-) create mode 100644 .idea/codeStyles/Project.xml create mode 100644 app/src/main/java/com/mxdl/customview/RectCaptureViewActivity.java create mode 100644 app/src/main/java/com/mxdl/customview/view/RectCaptureView.java create mode 100644 app/src/main/res/layout/activity_rect_capture_view.xml diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..681f41a --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,116 @@ + + + + + + + +
+ + + + xmlns:android + + ^$ + + + +
+
+ + + + xmlns:.* + + ^$ + + + BY_NAME + +
+
+ + + + .*:id + + http://schemas.android.com/apk/res/android + + + +
+
+ + + + .*:name + + http://schemas.android.com/apk/res/android + + + +
+
+ + + + name + + ^$ + + + +
+
+ + + + style + + ^$ + + + +
+
+ + + + .* + + ^$ + + + BY_NAME + +
+
+ + + + .* + + http://schemas.android.com/apk/res/android + + + ANDROID_ATTRIBUTE_ORDER + +
+
+ + + + .* + + .* + + + BY_NAME + +
+
+
+
+
+
\ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 54253bb..09a2cdb 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,6 +1,6 @@ + package="com.mxdl.customview"> - - - - - - - - - - + + + + + + - + - + - - + + \ No newline at end of file diff --git a/app/src/main/java/com/mxdl/customview/MainActivity.java b/app/src/main/java/com/mxdl/customview/MainActivity.java index 5acfcf1..df84991 100644 --- a/app/src/main/java/com/mxdl/customview/MainActivity.java +++ b/app/src/main/java/com/mxdl/customview/MainActivity.java @@ -25,6 +25,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe private Button mBtnCaptrueView; private Button mBtnCaptrueView1; private Button mBtnCricleCapture; + private Button mBtnRectCapture; @Override protected void onCreate(Bundle savedInstanceState) { @@ -38,6 +39,8 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe mBtnCaptrueView1 = findViewById(R.id.btn_capture_view1); mBtnCricleCapture = findViewById(R.id.btn_cricle_capture); + mBtnRectCapture = findViewById(R.id.btn_rect_capture); + mBtnStickyLayout.setOnClickListener(this); mBtnHorizontalScrollViewEx.setOnClickListener(this); mBtnHorizontalScrollView.setOnClickListener(this); @@ -45,6 +48,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe mBtnCaptrueView.setOnClickListener(this); mBtnCaptrueView1.setOnClickListener(this); mBtnCricleCapture.setOnClickListener(this); + mBtnRectCapture.setOnClickListener(this); } @Override @@ -71,6 +75,9 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe case R.id.btn_cricle_capture: startActivity(new Intent(this, CricleCaptureViewActivity.class)); break; + case R.id.btn_rect_capture: + startActivity(new Intent(this, RectCaptureViewActivity.class)); + break; } } } diff --git a/app/src/main/java/com/mxdl/customview/RectCaptureViewActivity.java b/app/src/main/java/com/mxdl/customview/RectCaptureViewActivity.java new file mode 100644 index 0000000..4150c82 --- /dev/null +++ b/app/src/main/java/com/mxdl/customview/RectCaptureViewActivity.java @@ -0,0 +1,13 @@ +package com.mxdl.customview; + +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; + +public class RectCaptureViewActivity extends AppCompatActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_rect_capture_view); + } +} diff --git a/app/src/main/java/com/mxdl/customview/view/RectCaptureView.java b/app/src/main/java/com/mxdl/customview/view/RectCaptureView.java new file mode 100644 index 0000000..7c5d526 --- /dev/null +++ b/app/src/main/java/com/mxdl/customview/view/RectCaptureView.java @@ -0,0 +1,238 @@ +package com.mxdl.customview.view; + +import android.content.Context; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; +import android.graphics.Path; +import android.graphics.Rect; +import android.graphics.RectF; +import android.graphics.Region; +import android.graphics.drawable.Drawable; +import android.os.Build; +import android.support.annotation.RequiresApi; +import android.util.AttributeSet; +import android.util.Log; +import android.view.MotionEvent; +import android.view.View; + +import com.mxdl.customview.R; + +/** + * Description:
+ * Author: mxdl
+ * Date: 2019/10/12
+ * Version: V1.0.0
+ * Update:
+ */ +public class RectCaptureView extends View { + public static final String TAG = RectCaptureView.class.getSimpleName(); + private int mWidth; + private Rect mScreenRect = new Rect(); + private Rect mCaptureRect = new Rect(); + private Paint mBorderPaint = new Paint(); + private Path mCapturePath = new Path(); + private Paint mAreaPaint = new Paint(); + private Drawable mAnchorDrawable; + private int mHalfAnchorWidth; + private int mLastX; + private int mLastY; + private int mHitCorner; + + public RectCaptureView(Context context) { + super(context); + initView(); + } + + public RectCaptureView(Context context, AttributeSet attrs) { + super(context, attrs); + initView(); + } + + public RectCaptureView(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + initView(); + } + + @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) + public RectCaptureView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { + super(context, attrs, defStyleAttr, defStyleRes); + initView(); + } + + public void initView() { + setLayerType(View.LAYER_TYPE_SOFTWARE, null); + mWidth = (int) (getResources().getDisplayMetrics().density * 216 + 0.5f); + mBorderPaint.setStrokeWidth(getResources().getDisplayMetrics().density * 2); + mBorderPaint.setStyle(Paint.Style.STROKE); + mBorderPaint.setAntiAlias(true); + mBorderPaint.setColor(Color.parseColor("#1E88E5")); + + mAreaPaint.setStyle(Paint.Style.FILL_AND_STROKE); + mAreaPaint.setColor(Color.parseColor("#1E88E5")); + mAreaPaint.setAlpha(20); + + mAnchorDrawable = getResources().getDrawable(R.mipmap.ic_edit_fence_dragger); + mHalfAnchorWidth = mAnchorDrawable.getIntrinsicWidth() / 2; + } + + @Override + protected void onLayout(boolean changed, int left, int top, int right, int bottom) { + super.onLayout(changed, left, top, right, bottom); + Log.v(TAG, "onLayout"); + mScreenRect.set(left, top, right, bottom); + int captureLeft = (right - mWidth) / 2; + int captureTop = (bottom - mWidth) / 2; + int captureRight = captureLeft + mWidth; + int captureBottom = captureTop + mWidth; + mCaptureRect.set(captureLeft, captureTop, captureRight, captureBottom); + } + + @Override + protected void onDraw(Canvas canvas) { + super.onDraw(canvas); + + //Log.v(TAG,"onDraw left:"+mCaptureRect.left+";top:"+mCaptureRect.top+";right:"+mCaptureRect.right+";bottom:"+mCaptureRect.bottom); + canvas.save(); + //路径重新设置 + mCapturePath.reset(); + //添加了一个当前的矩形区域 + mCapturePath.addRect(new RectF(mCaptureRect), Path.Direction.CW); + //当前的画布与指定的路径相交 + canvas.clipPath(mCapturePath, Region.Op.INTERSECT); + //画矩形背景选区 + canvas.drawRect(mScreenRect, mAreaPaint); + //画矩形背景边框 + canvas.drawPath(mCapturePath, mBorderPaint); + canvas.restore(); + + //画左上锚点 + + mAnchorDrawable.setBounds(mCaptureRect.left - mHalfAnchorWidth, mCaptureRect.top - mHalfAnchorWidth, mCaptureRect.left + mHalfAnchorWidth, mCaptureRect.top + mHalfAnchorWidth); + mAnchorDrawable.draw(canvas); + + //画右上锚点 + mAnchorDrawable.setBounds(mCaptureRect.right - mHalfAnchorWidth, mCaptureRect.top - mHalfAnchorWidth, mCaptureRect.right + mHalfAnchorWidth, mCaptureRect.top + mHalfAnchorWidth); + mAnchorDrawable.draw(canvas); + + //画右下锚点 + mAnchorDrawable.setBounds(mCaptureRect.right - mHalfAnchorWidth, mCaptureRect.bottom - mHalfAnchorWidth, mCaptureRect.right + mHalfAnchorWidth, mCaptureRect.bottom + mHalfAnchorWidth); + mAnchorDrawable.draw(canvas); + + //画左下锚点 + mAnchorDrawable.setBounds(mCaptureRect.left - mHalfAnchorWidth, mCaptureRect.bottom - mHalfAnchorWidth, mCaptureRect.left + mHalfAnchorWidth, mCaptureRect.bottom + mHalfAnchorWidth); + mAnchorDrawable.draw(canvas); + + } + + @Override + public boolean onTouchEvent(MotionEvent event) { + int x = (int) event.getX(); + int y = (int) event.getY(); + + switch (event.getAction()) { + case MotionEvent.ACTION_DOWN: + mHitCorner = getHitCorner(x, y); + //Log.v(TAG, "ACTION_DOWN:mHitCorner" + mHitCorner); + //如果没有点击在锚点上,则直接返回false + if (mHitCorner == 0) { + return false; + } + break; + case MotionEvent.ACTION_UP: + mHitCorner = 0; + break; + case MotionEvent.ACTION_MOVE: + if (mHitCorner == 0) { + return false; + } + int dx = x - mLastX; + int dy = y - mLastY; + + if (mHitCorner == 7) { + mCaptureRect.left += dx; + mCaptureRect.top += dy; + //mCaptureRect.right -= dx; + //mCaptureRect.bottom -= dy; + } else if (mHitCorner == 9) { + mCaptureRect.left -= dx; + mCaptureRect.top += dy; + //mCaptureRect.right += dx; + //mCaptureRect.bottom -= dy; + } else if (mHitCorner == 3) { + + //mCaptureRect.left -= dx; + //mCaptureRect.top -= dy; + mCaptureRect.right += dx; + mCaptureRect.bottom += dy; + } else if (mHitCorner == 1) { + + mCaptureRect.left += dx; + //mCaptureRect.top -= dy; + //mCaptureRect.right -= dx; + mCaptureRect.bottom += dy; + } + + //防止选取越界 + int screenWidth = mScreenRect.right - mScreenRect.left; + int screenHeight = mScreenRect.bottom - mScreenRect.top; + int maxWidth = screenWidth - mHalfAnchorWidth * 2; + if (mCaptureRect.left <= mHalfAnchorWidth) { + mCaptureRect.left = mHalfAnchorWidth; + } + if (mCaptureRect.top <= mHalfAnchorWidth) { + mCaptureRect.top = mHalfAnchorWidth; + } + if (mCaptureRect.right >= screenWidth - mHalfAnchorWidth) { + mCaptureRect.right = screenWidth - mHalfAnchorWidth; + } + if (mCaptureRect.bottom >= screenHeight - mHalfAnchorWidth) { + mCaptureRect.bottom = screenHeight - mHalfAnchorWidth; + } + if (mCaptureRect.left >= screenWidth - mHalfAnchorWidth) { + mCaptureRect.left = screenWidth - mHalfAnchorWidth; + } + if (mCaptureRect.top >= screenHeight - mHalfAnchorWidth) { + mCaptureRect.top = screenHeight - mHalfAnchorWidth; + } + if (mCaptureRect.right <= mHalfAnchorWidth) { + mCaptureRect.right = mHalfAnchorWidth; + } + if (mCaptureRect.bottom <= mHalfAnchorWidth) { + mCaptureRect.bottom = mHalfAnchorWidth; + } + boolean square = true; + if (Math.abs(mCaptureRect.right - mCaptureRect.left) != Math.abs(mCaptureRect.bottom - mCaptureRect.top)) { + square = false; + } + Log.v(TAG, "mHitCorner" + mHitCorner + ";dx:" + dx + ";dy:" + dy + ";width:" + (mCaptureRect.right - mCaptureRect.left) + ";height:" + (mCaptureRect.bottom - mCaptureRect.top) + ";square:" + (square ? "ok" : "------------------")); + + + break; + } + + mLastX = x; + mLastY = y; + + invalidate(); + return true; + } + + public int getHitCorner(float x, float y) { + int result = 0; + if (y > mCaptureRect.top - mHalfAnchorWidth * 2 && y < mCaptureRect.top + mHalfAnchorWidth * 2) { + if (x > mCaptureRect.left - mHalfAnchorWidth * 2 && x < mCaptureRect.left + mHalfAnchorWidth * 2) { + result = 7; + } else if (x > mCaptureRect.right - mHalfAnchorWidth * 2 && x < mCaptureRect.right + mHalfAnchorWidth * 2) { + result = 9; + } + } else if (y > mCaptureRect.bottom - mHalfAnchorWidth * 2 && y < mCaptureRect.bottom + mHalfAnchorWidth * 2) { + if (x > mCaptureRect.left - mHalfAnchorWidth * 2 && x < mCaptureRect.left + mHalfAnchorWidth * 2) { + result = 1; + } else if (x > mCaptureRect.right - mHalfAnchorWidth * 2 && x < mCaptureRect.right + mHalfAnchorWidth * 2) { + result = 3; + } + } + return result; + } +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 8140cf7..861f340 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -56,5 +56,12 @@ android:text="CricleCaptureView" android:textAllCaps="false" /> +