Skip to content

Commit fe585bd

Browse files
committed
放大镜模式下增加全图预览的功能
1 parent 1811916 commit fe585bd

File tree

4 files changed

+75
-7
lines changed

4 files changed

+75
-7
lines changed

changes.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
### V5.4
22

3-
* 优化交互逻辑
4-
* 支持左旋转
3+
* IDoodleItem支持缩放功能
4+
* 放大镜模式下增加全图预览的功能
5+
* 增加新的画笔:马赛克
6+
* 支持左旋转
7+
* 优化交互逻辑

doodle/src/main/java/cn/hzw/doodle/DoodleView.java

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
import android.view.MotionEvent;
1414
import android.view.View;
1515

16-
import junit.framework.Assert;
17-
1816
import java.util.HashMap;
1917
import java.util.List;
2018
import java.util.Map;
@@ -30,6 +28,7 @@
3028
import cn.hzw.doodle.core.IDoodleTouchDetector;
3129

3230
import static cn.hzw.doodle.util.DrawUtil.drawCircle;
31+
import static cn.hzw.doodle.util.DrawUtil.drawRect;
3332
import static cn.hzw.doodle.util.DrawUtil.rotatePoint;
3433

3534
/**
@@ -79,11 +78,12 @@ public class DoodleView extends View implements IDoodle {
7978

8079
private float mTouchX, mTouchY;
8180
private boolean mEnableZoomer = false; // 放大镜功能
81+
private boolean mEnableOverview = true; // 全图预览功能,建立在放大镜功能开启的前提下
8282
private float mLastZoomerY;
8383
private float mZoomerRadius;
8484
private Path mZoomerPath;
8585
private float mZoomerScale = 0; // 放大镜的倍数
86-
private Paint mZooomerPaint;
86+
private Paint mZooomerPaint, mZoomerTouchPaint;
8787
private int mZoomerHorizonX; // 放大器的位置的x坐标,使其水平居中
8888
private boolean mIsScrollingDoodle = false; // 是否正在滑动,只要用于标志触摸时才显示放大镜
8989

@@ -139,6 +139,12 @@ public DoodleView(Context context, Bitmap bitmap, IDoodleListener listener, IDoo
139139
mZooomerPaint.setStrokeCap(Paint.Cap.ROUND);// 圆滑
140140
mZooomerPaint.setStrokeWidth(Util.dp2px(getContext(), 10));
141141

142+
mZoomerTouchPaint = new Paint();
143+
mZoomerTouchPaint.setStyle(Paint.Style.STROKE);
144+
mZoomerTouchPaint.setAntiAlias(true);
145+
mZoomerTouchPaint.setStrokeJoin(Paint.Join.ROUND);
146+
mZoomerTouchPaint.setStrokeCap(Paint.Cap.ROUND);// 圆滑
147+
142148
mDefaultTouchDetector = defaultDetector;
143149

144150
mInner = new DoodleViewInner();
@@ -311,7 +317,9 @@ protected void dispatchDraw(Canvas canvas) {
311317
if (mIsScrollingDoodle && mEnableZoomer && mZoomerScale > 0) { //启用放大镜
312318
canvas.save();
313319

314-
if (mTouchY <= mZoomerRadius * 2) { // 在放大镜的范围内, 把放大镜仿制底部
320+
float unitSize = getUnitSize();
321+
322+
if (mTouchY <= mZoomerRadius * 2) { // 在放大镜的范围内, 把放大镜放在底部
315323
mLastZoomerY = getHeight() - mZoomerRadius * 2;
316324
} else if (mTouchY >= getHeight() - mZoomerRadius * 2) {
317325
mLastZoomerY = 0;
@@ -327,11 +335,52 @@ protected void dispatchDraw(Canvas canvas) {
327335
// draw inner
328336
canvas.rotate(mDoodleRotateDegree, getWidth() / 2, getHeight() / 2);
329337
mInner.onDraw(canvas);
338+
339+
// 触摸点
340+
float left = getAllTranX();
341+
float top = getAllTranY();
342+
// 画布和图片共用一个坐标系,只需要处理屏幕坐标系到图片(画布)坐标系的映射关系
343+
canvas.translate(left, top); // 偏移画布
344+
scale = getAllScale();
345+
canvas.scale(scale, scale); // 缩放画布
346+
mZoomerTouchPaint.setStrokeWidth(getUnitSize());
347+
mZoomerTouchPaint.setColor(0xaa000000);
348+
drawCircle(canvas, toX(mTouchX), toY(mTouchY), mSize / 2, mZoomerTouchPaint);
349+
mZoomerTouchPaint.setColor(0xaaffffff);
350+
drawCircle(canvas, toX(mTouchX), toY(mTouchY), mSize / 2 - unitSize / 4, mZoomerTouchPaint);
351+
330352
canvas.restore();
331353

332354
// 画放大器的边框
333355
drawCircle(canvas, mZoomerRadius, mZoomerRadius, mZoomerRadius, mZooomerPaint);
334356
canvas.restore();
357+
358+
// overview
359+
canvas.save();
360+
canvas.translate(mZoomerHorizonX, mLastZoomerY);
361+
scale = (mZoomerRadius / 2) / getWidth();
362+
canvas.scale(scale, scale);
363+
float strokeWidth = 1 / scale;
364+
canvas.clipRect(-strokeWidth, -strokeWidth, getWidth() + strokeWidth, getHeight() + strokeWidth);
365+
canvas.drawColor(0x88888888);
366+
canvas.save();
367+
canvas.rotate(mDoodleRotateDegree, getWidth() / 2, getHeight() / 2);
368+
float tempScale = mScale;
369+
float tempTransX = mTransX;
370+
float tempTransY = mTransY;
371+
mScale = 1;
372+
mTransX = mTransY = 0;
373+
mInner.onDraw(canvas);
374+
mScale = tempScale;
375+
mTransX = tempTransX;
376+
mTransY = tempTransY;
377+
canvas.restore();
378+
mZoomerTouchPaint.setStrokeWidth(strokeWidth);
379+
mZoomerTouchPaint.setColor(0xaa000000);
380+
drawRect(canvas, 0, 0, getWidth(), getHeight(), mZoomerTouchPaint);
381+
mZoomerTouchPaint.setColor(0xaaffffff);
382+
drawRect(canvas, strokeWidth, strokeWidth, getWidth() - strokeWidth, getHeight() - strokeWidth, mZoomerTouchPaint);
383+
canvas.restore();
335384
}
336385

337386
}
@@ -837,6 +886,22 @@ public boolean isEnableZoomer() {
837886
return mEnableZoomer;
838887
}
839888

889+
/**
890+
* 设置是否开启全图预览功能,开启后可以在放大镜功能下显示全图涂鸦
891+
* @param enableOverview
892+
*/
893+
public void enableOverview(boolean enableOverview) {
894+
mEnableOverview = enableOverview;
895+
}
896+
897+
/**
898+
* 是否开启全图预览功能
899+
* @return
900+
*/
901+
public boolean isEnableOverview() {
902+
return mEnableOverview;
903+
}
904+
840905
/**
841906
* 是否正在滚动涂鸦,只要用于标志触摸时才显示放大镜
842907
*
Loading

doodle/src/main/res/layout/doodle_layout.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
android:background="@drawable/doodle_btn_effect_round"
6464
android:onClick="onClick"
6565
android:scaleType="centerInside"
66-
android:src="@drawable/doodle_ic_texture"
66+
android:src="@drawable/doodle_ic_mosaic"
6767
android:visibility="visible" />
6868

6969
<ImageView

0 commit comments

Comments
 (0)