|
@@ -1,3 +1,12 @@
|
|
|
+/**
|
|
|
+ * Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
|
|
|
+ * <p>
|
|
|
+ * THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
|
|
|
+ * AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
|
|
|
+ * UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
|
|
|
+ * This notice may not be removed from this file.
|
|
|
+ */
|
|
|
+
|
|
|
package com.compdfkit.tools.security.watermark.view;
|
|
|
|
|
|
|
|
@@ -34,6 +43,7 @@ import android.view.View;
|
|
|
|
|
|
import androidx.annotation.ColorInt;
|
|
|
import androidx.annotation.DrawableRes;
|
|
|
+import androidx.annotation.IntRange;
|
|
|
import androidx.annotation.Nullable;
|
|
|
import androidx.core.content.ContextCompat;
|
|
|
|
|
@@ -44,50 +54,82 @@ import com.compdfkit.tools.common.utils.viewutils.CDimensUtils;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
|
|
|
+
|
|
|
+
|
|
|
public class CWatermarkView extends View {
|
|
|
|
|
|
/**
|
|
|
- * 控制缩放,旋转图标所在四个点得位置
|
|
|
+ * Control zoom and rotate the position of the four points of the icon
|
|
|
*/
|
|
|
public static final int LEFT_TOP = 0;
|
|
|
public static final int RIGHT_TOP = 1;
|
|
|
public static final int RIGHT_BOTTOM = 2;
|
|
|
public static final int LEFT_BOTTOM = 3;
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
- * 一些默认的常量
|
|
|
+ * Default border and text padding values
|
|
|
*/
|
|
|
public static final int DEFAULT_FRAME_PADDING = 8;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Default border width
|
|
|
+ */
|
|
|
public static final int DEFAULT_FRAME_WIDTH = 2;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * default border color value
|
|
|
+ */
|
|
|
public static final int DEFAULT_FRAME_COLOR = Color.WHITE;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Default watermark scaling value
|
|
|
+ */
|
|
|
public static final float DEFAULT_SCALE = 1.0F;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Default watermark rotation angle
|
|
|
+ */
|
|
|
public static final float DEFAULT_DEGREE = 0F;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * The display position of the icon used for dragging, rotating, and scaling the watermark.
|
|
|
+ * The default is the lower right corner.
|
|
|
+ */
|
|
|
public static final int DEFAULT_CONTROL_LOCATION = RIGHT_BOTTOM;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Edit mode default value, edit mode is enabled by default
|
|
|
+ * true: editable mode
|
|
|
+ * false : preview mode
|
|
|
+ */
|
|
|
public static final boolean DEFAULT_EDITABLE = true;
|
|
|
+
|
|
|
public static final int DEFAULT_OTHER_DRAWABLE_WIDTH = 50;
|
|
|
public static final int DEFAULT_OTHER_DRAWABLE_HEIGHT = 50;
|
|
|
|
|
|
/**
|
|
|
- * 点击时间小于300ms 视为单击
|
|
|
+ * Click time less than 300ms is regarded as a click
|
|
|
**/
|
|
|
public static final int CLICK_TIME = 100;
|
|
|
|
|
|
/**
|
|
|
- * 初始状态
|
|
|
+ * init status
|
|
|
*/
|
|
|
public static final int STATUS_INIT = 0;
|
|
|
|
|
|
/**
|
|
|
- * 拖动状态
|
|
|
+ * drag status
|
|
|
*/
|
|
|
public static final int STATUS_DRAG = 1;
|
|
|
|
|
|
/**
|
|
|
- * 旋转或者放大状态
|
|
|
+ * Rotate or zoom in state
|
|
|
*/
|
|
|
public static final int STATUS_ROTATE_ZOOM = 2;
|
|
|
|
|
|
+ /**
|
|
|
+ * Text watermark default text content
|
|
|
+ */
|
|
|
public static final String DEFAULT_STR = "";
|
|
|
|
|
|
public enum EditType {
|
|
@@ -95,54 +137,55 @@ public class CWatermarkView extends View {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 设置当前编辑的水印类型
|
|
|
+ * Set the currently edited watermark type
|
|
|
*/
|
|
|
private EditType editType = EditType.TXT;
|
|
|
|
|
|
/**
|
|
|
- * 用于旋转缩放的Bitmap
|
|
|
+ * image watermark bitmap
|
|
|
*/
|
|
|
private Bitmap mBitmap = null;
|
|
|
|
|
|
/**
|
|
|
- * SingleTouchView的中心点坐标,相对于其父类布局而言的
|
|
|
+ * The center point coordinates of Single Touch View, relative to its parent layout
|
|
|
*/
|
|
|
private PointF mCenterPoint = new PointF();
|
|
|
|
|
|
/**
|
|
|
- * View的宽度和高度,随着图片的旋转而变化(不包括控制旋转,缩放图片的宽高)
|
|
|
+ * The width and height of the View change with the rotation of the image
|
|
|
+ * (excluding controlling rotation and scaling the width and height of the image)
|
|
|
*/
|
|
|
private int mViewWidth = 0;
|
|
|
|
|
|
private int mViewHeight = 0;
|
|
|
|
|
|
/**
|
|
|
- * 图片的旋转角度
|
|
|
+ * Image rotation angle
|
|
|
*/
|
|
|
private float mDegree = DEFAULT_DEGREE;
|
|
|
|
|
|
/**
|
|
|
- * 图片的缩放比例
|
|
|
+ * Image scaling
|
|
|
*/
|
|
|
private float mScale = DEFAULT_SCALE;
|
|
|
|
|
|
/**
|
|
|
- * 用于缩放,旋转,平移的矩阵
|
|
|
+ * Matrices for scaling, rotation, translation
|
|
|
*/
|
|
|
private Matrix mMatrix = new Matrix();
|
|
|
|
|
|
/**
|
|
|
- * SingleTouchView距离父类布局的左间距
|
|
|
+ * Left spacing of Single Touch View from parent layout
|
|
|
*/
|
|
|
private int mViewPaddingLeft = 0;
|
|
|
|
|
|
/**
|
|
|
- * SingleTouchView距离父类布局的上间距
|
|
|
+ * Single Touch View's top spacing from parent layout
|
|
|
*/
|
|
|
private int mViewPaddingTop = 0;
|
|
|
|
|
|
/**
|
|
|
- * 图片四个点坐标
|
|
|
+ * Coordinates of four points in the picture
|
|
|
*/
|
|
|
private Point mLTPoint = new Point();
|
|
|
private Point mRTPoint = new Point();
|
|
@@ -150,113 +193,114 @@ public class CWatermarkView extends View {
|
|
|
private Point mLBPoint = new Point();
|
|
|
|
|
|
/**
|
|
|
- * 用于缩放,旋转的控制点的坐标
|
|
|
+ * Coordinates of control points used for scaling, rotation
|
|
|
*/
|
|
|
private Point mControlPoint = new Point();
|
|
|
|
|
|
/**
|
|
|
- * 用于缩放,旋转的图标
|
|
|
+ * 用于缩放,Icons for zoom, rotate
|
|
|
*/
|
|
|
- private Drawable controlDrawable = null; //context?.drawableByResId(R.drawable.ic_watermark_zoom)
|
|
|
+ private Drawable controlDrawable = null;
|
|
|
|
|
|
/**
|
|
|
- * 控制缩放,旋转图标的宽和高
|
|
|
+ * Control scaling, rotating icon width and height
|
|
|
*/
|
|
|
private int mControlDrawableWidth = 0;
|
|
|
private int mControlDrawableHeight = 0;
|
|
|
|
|
|
/**
|
|
|
- * 初始图片边界点到图片中心的距离
|
|
|
+ * The distance from the initial image boundary point to the image center
|
|
|
**/
|
|
|
private float drawToCenterDistance = 0F;
|
|
|
|
|
|
/**
|
|
|
- * 画外围框的Path
|
|
|
+ * Path to draw the outer frame
|
|
|
*/
|
|
|
private Path mPath = new Path();
|
|
|
|
|
|
/**
|
|
|
- * 画外围框的画笔
|
|
|
+ * Brush for drawing outer borders
|
|
|
*/
|
|
|
private Paint mPaint = new Paint();
|
|
|
|
|
|
/**
|
|
|
- * 画图片的画笔
|
|
|
+ * Brush for painting pictures
|
|
|
**/
|
|
|
private Paint mDrawPaint = new Paint();
|
|
|
|
|
|
/**
|
|
|
- * 文字的画笔
|
|
|
+ * text brush
|
|
|
**/
|
|
|
private TextPaint txtPaint = new TextPaint();
|
|
|
|
|
|
/**
|
|
|
- * 当前所处的状态
|
|
|
+ * Current status
|
|
|
*/
|
|
|
private int mStatus = STATUS_INIT;
|
|
|
|
|
|
/**
|
|
|
- * 外边框与图片之间的间距, 单位是dip
|
|
|
+ * The distance between the outer border and the image, the unit is dip
|
|
|
*/
|
|
|
private int framePadding = DEFAULT_FRAME_PADDING;
|
|
|
|
|
|
/**
|
|
|
- * 外边框颜色
|
|
|
+ * Outer border color
|
|
|
*/
|
|
|
private int frameColor = DEFAULT_FRAME_COLOR;
|
|
|
|
|
|
/**
|
|
|
- * 外边框线条粗细, 单位是 dip
|
|
|
+ * Outer border line thickness, unit is dip
|
|
|
*/
|
|
|
private int frameWidth = DEFAULT_FRAME_WIDTH;
|
|
|
|
|
|
/**
|
|
|
- * 是否处于可以缩放,平移,旋转状态
|
|
|
+ * Whether it is in a state where it can be zoomed, translated, and rotated
|
|
|
*/
|
|
|
private boolean isEditable = DEFAULT_EDITABLE;
|
|
|
- private DisplayMetrics metrics = null; //context?.resources?.displayMetrics
|
|
|
+ private DisplayMetrics metrics = null;
|
|
|
private PointF mPreMovePointF = new PointF();
|
|
|
private PointF mCurMovePointF = new PointF();
|
|
|
|
|
|
/**
|
|
|
- * 图片在旋转时x方向的偏移量
|
|
|
+ * The offset in the x direction when the image is rotated
|
|
|
*/
|
|
|
private int offsetX = 0;
|
|
|
|
|
|
/**
|
|
|
- * 图片在旋转时y方向的偏移量
|
|
|
+ * The offset in the y direction of the image when rotating
|
|
|
*/
|
|
|
private int offsetY = 0;
|
|
|
|
|
|
/**
|
|
|
- * 控制图标所在的位置(比如左上,右上,左下,右下)
|
|
|
+ * Control the location of the icon (such as upper left, upper right, lower left, lower right)
|
|
|
*/
|
|
|
private int controlLocation = DEFAULT_CONTROL_LOCATION;
|
|
|
|
|
|
/**
|
|
|
- * txt水印的文字
|
|
|
+ * txt Watermark Text
|
|
|
**/
|
|
|
private String txtContent = DEFAULT_STR;
|
|
|
|
|
|
/**
|
|
|
- * 记录单击点击时间和坐标
|
|
|
+ * Record click time and coordinates
|
|
|
**/
|
|
|
private long downClickTime = 0L;
|
|
|
private PointF downClickPoint = new PointF();
|
|
|
|
|
|
/**
|
|
|
- * 绘制txt的基线
|
|
|
+ * Draw the baseline of txt
|
|
|
**/
|
|
|
private float baseLineY = 0F;
|
|
|
|
|
|
/**
|
|
|
- * 绘制txt的原始宽高
|
|
|
+ * Draw the original width and height of txt
|
|
|
**/
|
|
|
- private float textWidth = 0f;
|
|
|
- private float textHeight = 0f;
|
|
|
+ private float textWidth = 0F;
|
|
|
+
|
|
|
+ private float textHeight = 0F;
|
|
|
|
|
|
/**
|
|
|
- * 绘制区域mPath区域,判断点击和拖拽
|
|
|
+ * Draw area m Path area, determine click and drag
|
|
|
**/
|
|
|
private Region dragArea = new Region();
|
|
|
|
|
@@ -329,9 +373,9 @@ public class CWatermarkView extends View {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 初始化image水印
|
|
|
+ * Initialize image watermark
|
|
|
*
|
|
|
- * @param bitmap 作为水印的图片
|
|
|
+ * @param bitmap Picture as watermark
|
|
|
*/
|
|
|
public void setImageBitmap(Bitmap bitmap) {
|
|
|
editType = EditType.Image;
|
|
@@ -343,13 +387,13 @@ public class CWatermarkView extends View {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * image水印预览模式
|
|
|
+ * image watermark preview mode
|
|
|
*
|
|
|
- * @param bitmap 水印图片
|
|
|
- * @param alpha 图片透明度
|
|
|
- * @param degree 角度
|
|
|
- * @param scale 缩放比例
|
|
|
- * @param center 相对于父布局而言中心点坐标
|
|
|
+ * @param bitmap watermark image
|
|
|
+ * @param alpha image opacity
|
|
|
+ * @param degree image rotation angle
|
|
|
+ * @param scale scale
|
|
|
+ * @param center center point
|
|
|
**/
|
|
|
public void initPreviewImageBitmap(Bitmap bitmap, int alpha, float degree, float scale, PointF center) {
|
|
|
editType = EditType.Image;
|
|
@@ -368,15 +412,15 @@ public class CWatermarkView extends View {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * txt水印预览模式
|
|
|
+ * txt watermark preview mode
|
|
|
*
|
|
|
- * @param text 水印文字
|
|
|
- * @param color 文子颜色
|
|
|
- * @param size 文字大小
|
|
|
- * @param alpha 文字透明度
|
|
|
- * @param degree 旋转角度
|
|
|
- * @param scale 缩放比例
|
|
|
- * @param center
|
|
|
+ * @param text text content
|
|
|
+ * @param color text color
|
|
|
+ * @param size font size
|
|
|
+ * @param alpha font opacity
|
|
|
+ * @param degree rotation angle
|
|
|
+ * @param scale scale
|
|
|
+ * @param center center point
|
|
|
**/
|
|
|
public void initPreviewTextWaterMark(String text, @ColorInt int color, float size, int alpha, float degree, float scale, PointF center) {
|
|
|
editType = EditType.TXT;
|
|
@@ -399,59 +443,94 @@ public class CWatermarkView extends View {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 初始化txt水印参数
|
|
|
+ * init text watermark
|
|
|
+ * @param text text content
|
|
|
+ * @param color font color
|
|
|
+ * @param size font size
|
|
|
+ * @param alpha font color opacity
|
|
|
**/
|
|
|
public void initTextWaterMark(String text, @ColorInt int color, float size, int alpha) {
|
|
|
editType = EditType.TXT;
|
|
|
txtPaint.setColor(color);
|
|
|
txtPaint.setTextSize(size);
|
|
|
txtPaint.setAlpha(alpha);
|
|
|
+ watermarkAlpha = alpha;
|
|
|
txtContent = TextUtils.isEmpty(text) ? DEFAULT_STR : text;
|
|
|
calculateBaseLine();
|
|
|
calculateDrawToCenterDistance();
|
|
|
transformDraw();
|
|
|
}
|
|
|
|
|
|
- public void setTextSize(float textSize){
|
|
|
+ public void setTextSize(float textSize) {
|
|
|
txtPaint.setTextSize(textSize);
|
|
|
calculateBaseLine();
|
|
|
calculateDrawToCenterDistance();
|
|
|
transformDraw();
|
|
|
}
|
|
|
|
|
|
- public int getTextSize(){
|
|
|
+ public int getTextSize() {
|
|
|
return (int) txtPaint.getTextSize();
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- public void setWatermarkAlpha(int alpha) {
|
|
|
+ /**
|
|
|
+ * set text or image watermark opacity
|
|
|
+ * @param alpha opacity
|
|
|
+ */
|
|
|
+ public void setWatermarkAlpha(@IntRange(from = 0, to = 255) int alpha) {
|
|
|
watermarkAlpha = alpha;
|
|
|
txtPaint.setAlpha(alpha);
|
|
|
mDrawPaint.setAlpha(alpha);
|
|
|
invalidate();
|
|
|
}
|
|
|
|
|
|
- public int getWatermarkAlpha(){
|
|
|
+ public int getWatermarkAlpha() {
|
|
|
return watermarkAlpha;
|
|
|
}
|
|
|
|
|
|
- public boolean isBold(){
|
|
|
+ /**
|
|
|
+ * Get text watermark font is bold
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public boolean isBold() {
|
|
|
return isBold;
|
|
|
}
|
|
|
|
|
|
- public boolean isItalic(){
|
|
|
+ /**
|
|
|
+ * get text watermark font is italic
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public boolean isItalic() {
|
|
|
return isItalic;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * set text watermark font is bold
|
|
|
+ * @param bold
|
|
|
+ */
|
|
|
public void setBold(boolean bold) {
|
|
|
setTypeface(getFontType(), bold, isItalic());
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * set text watermark font is italic
|
|
|
+ * @param italic
|
|
|
+ */
|
|
|
public void setItalic(boolean italic) {
|
|
|
setTypeface(getFontType(), isBold(), italic);
|
|
|
}
|
|
|
|
|
|
- public void setTypeface(CPDFTextAttribute.FontNameHelper.FontType fontType, boolean isBold, boolean isItalic){
|
|
|
+ /**
|
|
|
+ * Set text watermark font typeface
|
|
|
+ * @param fontType font type
|
|
|
+ * @param isBold set font is bold
|
|
|
+ * @param isItalic set font is italic
|
|
|
+ *
|
|
|
+ * @see com.compdfkit.core.annotation.CPDFTextAttribute.FontNameHelper.FontType#Helvetica
|
|
|
+ * @see com.compdfkit.core.annotation.CPDFTextAttribute.FontNameHelper.FontType#Times_Roman
|
|
|
+ * @see com.compdfkit.core.annotation.CPDFTextAttribute.FontNameHelper.FontType#Courier
|
|
|
+ *
|
|
|
+ */
|
|
|
+ public void setTypeface(CPDFTextAttribute.FontNameHelper.FontType fontType, boolean isBold, boolean isItalic) {
|
|
|
String fontName = CPDFTextAttribute.FontNameHelper.obtainFontName(fontType, isBold, isItalic);
|
|
|
Typeface typeface = CPDFTextAttribute.FontNameHelper.getInnerTypeface(getContext(), fontName);
|
|
|
this.typeface = typeface;
|
|
@@ -478,7 +557,7 @@ public class CWatermarkView extends View {
|
|
|
transformDraw();
|
|
|
}
|
|
|
|
|
|
- public Bitmap getImageWatermarkBitmap(){
|
|
|
+ public Bitmap getImageWatermarkBitmap() {
|
|
|
return mBitmap;
|
|
|
}
|
|
|
|
|
@@ -498,7 +577,7 @@ public class CWatermarkView extends View {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public void setImageScale(float scale) {
|
|
|
+ public void setScale(float scale) {
|
|
|
if (mScale != scale) {
|
|
|
mScale = scale;
|
|
|
transformDraw();
|
|
@@ -509,15 +588,10 @@ public class CWatermarkView extends View {
|
|
|
return mScale;
|
|
|
}
|
|
|
|
|
|
- public PointF centerPoint(){
|
|
|
+ public PointF centerPoint() {
|
|
|
return mCenterPoint;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 根据id设置旋转图
|
|
|
- *
|
|
|
- * @param resId
|
|
|
- */
|
|
|
public void setImageResource(@DrawableRes int resId) {
|
|
|
Drawable drawable = ContextCompat.getDrawable(getContext(), resId);
|
|
|
setImageDrawable(drawable);
|
|
@@ -532,7 +606,7 @@ public class CWatermarkView extends View {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 外围边框颜色
|
|
|
+ * Peripheral border color
|
|
|
*
|
|
|
* @param frameColor
|
|
|
*/
|
|
@@ -555,11 +629,12 @@ public class CWatermarkView extends View {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 设置控制图标的位置, 设置的值只能选择LEFT_TOP ,RIGHT_TOP, RIGHT_BOTTOM,LEFT_BOTTOM
|
|
|
+ * Set the position of the control icon. The set values can only be
|
|
|
+ * LEFT_TOP ,RIGHT_TOP, RIGHT_BOTTOM,LEFT_BOTTOM
|
|
|
*
|
|
|
* @param location controlLocation
|
|
|
*/
|
|
|
- public void setControlLocation(int location) {
|
|
|
+ public void setControlLocation(@IntRange(from = 0, to = 3) int location) {
|
|
|
if (controlLocation == location) {
|
|
|
return;
|
|
|
}
|
|
@@ -568,7 +643,7 @@ public class CWatermarkView extends View {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 设置图片中心点位置,相对于父布局而言
|
|
|
+ * Set the center point position of the image, relative to the parent layout
|
|
|
*
|
|
|
* @param center
|
|
|
*/
|
|
@@ -578,7 +653,7 @@ public class CWatermarkView extends View {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 设置是否处于可缩放,平移,旋转状态
|
|
|
+ * Set whether it is zoomable, panning, and rotating
|
|
|
*
|
|
|
* @param isEditable
|
|
|
*/
|
|
@@ -597,7 +672,7 @@ public class CWatermarkView extends View {
|
|
|
invalidate();
|
|
|
}
|
|
|
|
|
|
- public int getTextColor(){
|
|
|
+ public int getTextColor() {
|
|
|
return txtPaint.getColor();
|
|
|
}
|
|
|
|
|
@@ -610,7 +685,7 @@ public class CWatermarkView extends View {
|
|
|
return txtContent;
|
|
|
}
|
|
|
|
|
|
- public EditType getWatermarkType(){
|
|
|
+ public EditType getWatermarkType() {
|
|
|
return editType;
|
|
|
}
|
|
|
|
|
@@ -619,7 +694,7 @@ public class CWatermarkView extends View {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 调整View的大小,位置
|
|
|
+ * Adjust the size and position of View
|
|
|
*/
|
|
|
private void adjustLayout() {
|
|
|
int actualWidth = mViewWidth + mControlDrawableWidth;
|
|
@@ -643,7 +718,7 @@ public class CWatermarkView extends View {
|
|
|
|
|
|
@Override
|
|
|
protected void onDraw(Canvas canvas) {
|
|
|
- //每次draw之前调整View的位置和大小
|
|
|
+ //Adjust the position and size of the View before each draw
|
|
|
super.onDraw(canvas);
|
|
|
switch (editType) {
|
|
|
case Image:
|
|
@@ -660,11 +735,11 @@ public class CWatermarkView extends View {
|
|
|
float drawWidth = textWidth * mScale;
|
|
|
float drawHeight = textHeight * mScale;
|
|
|
|
|
|
- //1、设置画文字的起始点(其实位置的偏移量)
|
|
|
+ //1、Set the starting point for drawing text (the offset of the actual position)
|
|
|
canvas.translate((offsetX + mControlDrawableWidth / 2F), (offsetY + mControlDrawableHeight / 2F));
|
|
|
- //2、绕着文字中心进行旋转
|
|
|
+ //2、Rotate around the center of the text
|
|
|
canvas.rotate(mDegree % 360, drawWidth / 2, drawHeight / 2);
|
|
|
- //3、设置缩放比例
|
|
|
+ //3、Set zoom ratio
|
|
|
canvas.scale(mScale, mScale);
|
|
|
canvas.drawText(txtContent, 0f, baseLineY, txtPaint);
|
|
|
canvas.restore();
|
|
@@ -673,10 +748,10 @@ public class CWatermarkView extends View {
|
|
|
}
|
|
|
|
|
|
canvas.save();
|
|
|
- //处于可编辑状态才画边框和控制图标
|
|
|
+ //Draw borders and control icons only when in editable state
|
|
|
if (isEditable) {
|
|
|
canvas.drawPath(mPath, mPaint);
|
|
|
- //画旋转, 缩放图标
|
|
|
+ //draw rotate zoom icon
|
|
|
controlDrawable.setBounds(
|
|
|
mControlPoint.x - mControlDrawableWidth / 2,
|
|
|
mControlPoint.y - mControlDrawableHeight / 2, mControlPoint.x + mControlDrawableWidth
|
|
@@ -705,7 +780,8 @@ public class CWatermarkView extends View {
|
|
|
break;
|
|
|
case MotionEvent.ACTION_UP:
|
|
|
mStatus = STATUS_INIT;
|
|
|
- //判断是否为单击事件: 点击时间要小于CLICK_TIME,坐标点为ACTION_DOWN坐标点
|
|
|
+ // Determine whether it is a click event: the click time must be less than CLICK_TIME,
|
|
|
+ // and the coordinate point is the ACTION_DOWN coordinate point
|
|
|
if ((System.currentTimeMillis() - downClickTime) <= CLICK_TIME && abs(px - downClickPoint.x) < 1 && abs(py - downClickPoint.y) < 1) {
|
|
|
if (judgeStatus(px, py) == STATUS_DRAG) {
|
|
|
if (clickDrawAreaListener != null) {
|
|
@@ -713,36 +789,37 @@ public class CWatermarkView extends View {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- //拖动后回调
|
|
|
+ // Callback after dragging
|
|
|
if (upActionListener != null) {
|
|
|
upActionListener.callback(mScale, mCenterPoint);
|
|
|
}
|
|
|
break;
|
|
|
case MotionEvent.ACTION_MOVE:
|
|
|
- //如果坐标点为ACTION_DOWN坐标点,则不做处理
|
|
|
+ // If the coordinate point is an ACTION DOWN coordinate point, no processing will be performed.
|
|
|
if (px == downClickPoint.x && py == downClickPoint.y) {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
mCurMovePointF.set(px + mViewPaddingLeft, py + mViewPaddingTop);
|
|
|
if (mStatus == STATUS_ROTATE_ZOOM) {
|
|
|
- //移动的点到图片中心的距离
|
|
|
+ // The distance between the moved point and the center of the picture
|
|
|
float moveToCenterDistance = distance4PointF(mCenterPoint, mCurMovePointF);
|
|
|
- //上次的点到图片中心的距离
|
|
|
+ // The distance from the last point to the center of the picture
|
|
|
float preToCenterDistance = distance4PointF(mCenterPoint, mPreMovePointF);
|
|
|
|
|
|
- //是否是缩小的手势拖动
|
|
|
+ // Whether it is a zoom gesture drag
|
|
|
boolean isReduce = preToCenterDistance > moveToCenterDistance;
|
|
|
- //计算缩放比例
|
|
|
+ // Calculate scaling
|
|
|
float scale = moveToCenterDistance / drawToCenterDistance;
|
|
|
- //如果是缩小的手势,但是计算的scale比上次大,则不进行缩放,反之亦然
|
|
|
+ // If it is a zoom out gesture, but the calculated scale is larger than the last time,
|
|
|
+ // no scaling will be performed, and vice versa.
|
|
|
if (isReduce && scale < mScale) {
|
|
|
mScale = scale;
|
|
|
} else if (!isReduce && scale > mScale) {
|
|
|
mScale = scale;
|
|
|
}
|
|
|
|
|
|
- // 角度
|
|
|
+ // angle
|
|
|
double a = distance4PointF(mCenterPoint, mPreMovePointF);
|
|
|
double b = distance4PointF(mPreMovePointF, mCurMovePointF);
|
|
|
double c = distance4PointF(mCenterPoint, mCurMovePointF);
|
|
@@ -753,13 +830,14 @@ public class CWatermarkView extends View {
|
|
|
double radian = acos(cosb);
|
|
|
double newDegree = radianToDegree(radian);
|
|
|
|
|
|
- //center -> proMove的向量, 我们使用PointF来实现
|
|
|
+ //center -> vector of proMove, we use PointF to implement
|
|
|
PointF centerToProMove = new PointF(mPreMovePointF.x - mCenterPoint.x, mPreMovePointF.y - mCenterPoint.y);
|
|
|
|
|
|
- //center -> curMove 的向量
|
|
|
+ //center -> vector of curMove
|
|
|
PointF centerToCurMove = new PointF(mCurMovePointF.x - mCenterPoint.x, mCurMovePointF.y - mCenterPoint.y);
|
|
|
|
|
|
- //向量叉乘结果, 如果结果为负数, 表示为逆时针, 结果为正数表示顺时针
|
|
|
+ // Vector cross product result. If the result is a negative number, it means counterclockwise.
|
|
|
+ // If the result is a positive number, it means clockwise.
|
|
|
double result = centerToProMove.x * centerToCurMove.y - centerToProMove.y * centerToCurMove.x;
|
|
|
if (result < 0) {
|
|
|
newDegree = -newDegree;
|
|
@@ -767,7 +845,7 @@ public class CWatermarkView extends View {
|
|
|
mDegree += newDegree;
|
|
|
transformDraw();
|
|
|
} else if (mStatus == STATUS_DRAG) {
|
|
|
- // 修改中心点
|
|
|
+ // Modify center point
|
|
|
mCenterPoint.x += mCurMovePointF.x - mPreMovePointF.x;
|
|
|
mCenterPoint.y += mCurMovePointF.y - mPreMovePointF.y;
|
|
|
adjustLayout();
|
|
@@ -779,7 +857,7 @@ public class CWatermarkView extends View {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 设置Matrix, 强制刷新
|
|
|
+ * Set Matrix, force refresh
|
|
|
*/
|
|
|
private void transformDraw() {
|
|
|
float drawWidth = 0;
|
|
@@ -803,18 +881,18 @@ public class CWatermarkView extends View {
|
|
|
|
|
|
computeRect(-framePadding, -framePadding, (int) (drawWidth + framePadding), (int) (drawHeight + framePadding), (int) mDegree);
|
|
|
|
|
|
- //设置缩放比例
|
|
|
+ //Set zoom ratio
|
|
|
mMatrix.setScale(mScale, mScale);
|
|
|
- //绕着图片中心进行旋转
|
|
|
+ //Rotate around the center of the image
|
|
|
mMatrix.postRotate(mDegree % 360, (drawWidth / 2F), (drawHeight / 2F));
|
|
|
- //设置画该图片的起始点
|
|
|
+ //Set the starting point for drawing the picture
|
|
|
mMatrix.postTranslate((offsetX + mControlDrawableWidth / 2F), (offsetY + mControlDrawableHeight / 2F));
|
|
|
|
|
|
adjustLayout();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取四个点和View的大小
|
|
|
+ * Get four points and the size of the View
|
|
|
*
|
|
|
* @param left
|
|
|
* @param top
|
|
@@ -833,40 +911,38 @@ public class CWatermarkView extends View {
|
|
|
mRBPoint = obtainRotationPoint(cp, rb, degree);
|
|
|
mLBPoint = obtainRotationPoint(cp, lb, degree);
|
|
|
|
|
|
- //计算X坐标最大的值和最小的值
|
|
|
+ //Calculate the maximum value and minimum value of the X coordinate
|
|
|
int maxCoordinateX = maxOrNull(Arrays.asList(mLTPoint.x, mRTPoint.x, mRBPoint.x, mLBPoint.x), 0);
|
|
|
|
|
|
int minCoordinateX = minOrNull(Arrays.asList(mLTPoint.x, mRTPoint.x, mRBPoint.x, mLBPoint.x), 0);
|
|
|
mViewWidth = maxCoordinateX - minCoordinateX;
|
|
|
|
|
|
-
|
|
|
- //计算Y坐标最大的值和最小的值
|
|
|
+ //Calculate the largest and smallest value of the Y coordinate
|
|
|
int maxCoordinateY = maxOrNull(Arrays.asList(mLTPoint.y, mRTPoint.y, mRBPoint.y, mLBPoint.y), 0);
|
|
|
int minCoordinateY = minOrNull(Arrays.asList(mLTPoint.y, mRTPoint.y, mRBPoint.y, mLBPoint.y), 0);
|
|
|
mViewHeight = maxCoordinateY - minCoordinateY;
|
|
|
|
|
|
-
|
|
|
- //View中心点的坐标
|
|
|
+ //The coordinates of the center point of the View
|
|
|
Point viewCenterPoint = new Point((maxCoordinateX + minCoordinateX) / 2, (maxCoordinateY + minCoordinateY) / 2);
|
|
|
offsetX = mViewWidth / 2 - viewCenterPoint.x;
|
|
|
offsetY = mViewHeight / 2 - viewCenterPoint.y;
|
|
|
int halfControlDrawableWidth = mControlDrawableWidth / 2;
|
|
|
int halfControlDrawableHeight = mControlDrawableHeight / 2;
|
|
|
|
|
|
- //将Bitmap的四个点的X的坐标移动offsetX + halfDrawableWidth
|
|
|
+ //Move the X coordinates of the four points of the Bitmap by offsetX + halfDrawableWidth
|
|
|
mLTPoint.x += offsetX + halfControlDrawableWidth;
|
|
|
mRTPoint.x += offsetX + halfControlDrawableWidth;
|
|
|
mRBPoint.x += offsetX + halfControlDrawableWidth;
|
|
|
mLBPoint.x += offsetX + halfControlDrawableWidth;
|
|
|
|
|
|
- //将Bitmap的四个点的Y坐标移动offsetY + halfDrawableHeight
|
|
|
+ //Move the Y coordinates of the four points of the Bitmap by offsetY + halfDrawableHeight
|
|
|
mLTPoint.y += offsetY + halfControlDrawableHeight;
|
|
|
mRTPoint.y += offsetY + halfControlDrawableHeight;
|
|
|
mRBPoint.y += offsetY + halfControlDrawableHeight;
|
|
|
mLBPoint.y += offsetY + halfControlDrawableHeight;
|
|
|
mControlPoint = locationToPoint(controlLocation);
|
|
|
|
|
|
- //计算绘制区域
|
|
|
+ //Calculate drawing area
|
|
|
RectF rect = new RectF();
|
|
|
|
|
|
mPath.reset();
|
|
@@ -881,7 +957,7 @@ public class CWatermarkView extends View {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取旋转某个角度之后的点
|
|
|
+ * Get the point after rotating by a certain angle
|
|
|
*
|
|
|
* @param center viewCenter
|
|
|
* @param source
|
|
@@ -889,55 +965,55 @@ public class CWatermarkView extends View {
|
|
|
* @return
|
|
|
*/
|
|
|
private Point obtainRotationPoint(Point center, Point source, int degree) {
|
|
|
- //两者之间的距离
|
|
|
+ //distance between the two
|
|
|
Point disPoint = new Point();
|
|
|
disPoint.x = source.x - center.x;
|
|
|
disPoint.y = source.y - center.y;
|
|
|
|
|
|
- //没旋转之前的弧度
|
|
|
+ //The arc before rotation
|
|
|
double originRadian = 0.0;
|
|
|
|
|
|
- //没旋转之前的角度
|
|
|
+ //Angle before rotation
|
|
|
double originDegree = 0.0;
|
|
|
|
|
|
- //旋转之后的角度
|
|
|
+ //Angle after rotation
|
|
|
double resultDegree = 0.0;
|
|
|
|
|
|
- //旋转之后的弧度
|
|
|
+ //arc after rotation
|
|
|
double resultRadian = 0.0;
|
|
|
|
|
|
- //经过旋转之后点的坐标
|
|
|
+ //The coordinates of the point after rotation
|
|
|
Point resultPoint = new Point();
|
|
|
double distance = sqrt((disPoint.x * disPoint.x + disPoint.y * disPoint.y));
|
|
|
if (disPoint.x == 0 && disPoint.y == 0) {
|
|
|
return center;
|
|
|
- // 第一象限
|
|
|
+ // Quadrant 1
|
|
|
} else if (disPoint.x >= 0 && disPoint.y >= 0) {
|
|
|
- // 计算与x正方向的夹角
|
|
|
+ // Calculate the angle with the positive x direction
|
|
|
originRadian = asin(disPoint.y / distance);
|
|
|
|
|
|
- // 第二象限
|
|
|
+ // Quadrant 2
|
|
|
} else if (disPoint.x < 0 && disPoint.y >= 0) {
|
|
|
- // 计算与x正方向的夹角
|
|
|
+ // Calculate the angle with the positive x direction
|
|
|
originRadian = asin(abs(disPoint.x) / distance);
|
|
|
originRadian += Math.PI / 2;
|
|
|
|
|
|
- // 第三象限
|
|
|
+ // Quadrant 3
|
|
|
} else if (disPoint.x < 0 && disPoint.y < 0) {
|
|
|
- // 计算与x正方向的夹角
|
|
|
+ // Calculate the angle with the positive x direction
|
|
|
originRadian = asin(abs(disPoint.y) / distance);
|
|
|
originRadian += Math.PI;
|
|
|
} else if (disPoint.x >= 0 && disPoint.y < 0) {
|
|
|
- // 计算与x正方向的夹角
|
|
|
+ // Calculate the angle with the positive x direction
|
|
|
originRadian = asin(disPoint.x / distance);
|
|
|
originRadian += Math.PI * 3 / 2;
|
|
|
}
|
|
|
|
|
|
- // 弧度换算成角度
|
|
|
+ // Convert radians to degrees
|
|
|
originDegree = radianToDegree(originRadian);
|
|
|
resultDegree = originDegree + degree;
|
|
|
|
|
|
- // 角度转弧度
|
|
|
+ // angle to radian
|
|
|
resultRadian = degreeToRadian(resultDegree);
|
|
|
resultPoint.x = (int) Math.round(distance * cos(resultRadian));
|
|
|
resultPoint.y = (int) Math.round(distance * sin(resultRadian));
|
|
@@ -947,7 +1023,7 @@ public class CWatermarkView extends View {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 根据位置判断控制图标处于那个点
|
|
|
+ * Determine which point the control icon is based on its position
|
|
|
*
|
|
|
* @return
|
|
|
*/
|
|
@@ -966,7 +1042,7 @@ public class CWatermarkView extends View {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 角度换算成弧度
|
|
|
+ * Convert angles to radians
|
|
|
*
|
|
|
* @param degree
|
|
|
* @return
|
|
@@ -976,7 +1052,8 @@ public class CWatermarkView extends View {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 根据点击的位置判断是否点中控制旋转,缩放的图片, 初略的计算
|
|
|
+ * Based on the clicked position, determine whether the click is in the control of rotation,
|
|
|
+ * zooming of the picture, and rough calculation.
|
|
|
*
|
|
|
* @param x
|
|
|
* @param y
|
|
@@ -986,10 +1063,11 @@ public class CWatermarkView extends View {
|
|
|
PointF touchPoint = new PointF(x, y);
|
|
|
PointF controlPointF = new PointF(mControlPoint);
|
|
|
|
|
|
- //点击的点到控制旋转,缩放点的距离
|
|
|
+ //The distance from the clicked point to the point that controls rotation and scaling
|
|
|
float distanceToControl = distance4PointF(touchPoint, controlPointF);
|
|
|
|
|
|
- //如果两者之间的距离小于 控制图标的宽度,高度的最小值,则认为点中了控制图标
|
|
|
+ // If the distance between the two is less than the minimum value of the width and height of the control icon,
|
|
|
+ // it is considered that the control icon has been clicked.
|
|
|
if (distanceToControl < coerceAtMost((mControlDrawableWidth / 2), mControlDrawableHeight / 2)) {
|
|
|
return STATUS_ROTATE_ZOOM;
|
|
|
} else if (dragArea.contains((int) x, (int) y)) {
|
|
@@ -1001,7 +1079,7 @@ public class CWatermarkView extends View {
|
|
|
|
|
|
/**
|
|
|
* @methodName: created by liujiyuan on 2021/3/29 下午6:13.
|
|
|
- * @description:计算初始水印图片边界点到图片中心的距离
|
|
|
+ * @description:Calculate the distance from the initial watermark image boundary point to the image center
|
|
|
*/
|
|
|
private void calculateDrawToCenterDistance() {
|
|
|
float halfDrawWidth = 0;
|
|
@@ -1024,21 +1102,21 @@ public class CWatermarkView extends View {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 计算基线
|
|
|
+ * Calculate baseline
|
|
|
**/
|
|
|
private void calculateBaseLine() {
|
|
|
Paint.FontMetrics fontMetrics = txtPaint.getFontMetrics();
|
|
|
textHeight = fontMetrics.descent - fontMetrics.ascent;
|
|
|
- // 计算中线跟基线的差值
|
|
|
+ // Calculate the difference between the midline and the baseline
|
|
|
float dy = textHeight / 2 - fontMetrics.bottom;
|
|
|
- //计算baseLineY
|
|
|
+ // calculate baseLineY
|
|
|
int drawHeight = (int) textHeight;
|
|
|
baseLineY = drawHeight / 2 + dy;
|
|
|
textWidth = txtPaint.measureText(txtContent);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 两个点之间的距离
|
|
|
+ * distance between two points
|
|
|
*
|
|
|
* @param pf1 (x1,y1)
|
|
|
* @param pf2 (x2,y2)
|
|
@@ -1050,7 +1128,7 @@ public class CWatermarkView extends View {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 从Drawable中获取Bitmap对象
|
|
|
+ * Get Bitmap object from Drawable
|
|
|
*
|
|
|
* @param drawable
|
|
|
* @return
|