-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomClipView.java
More file actions
303 lines (264 loc) · 9.22 KB
/
CustomClipView.java
File metadata and controls
303 lines (264 loc) · 9.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package hxy.ttt.com.customclipview;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.util.FloatMath;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by huangxy on 2016/4/1.
* Origin by https://github.com/GitSmark/Android-CustomClipView
*/
public class CustomClipView extends View {
//clip picture of type
public final static int CLIPTYPE1 = 0; //正方形
public final static int CLIPTYPE2 = 1; //固定宽高矩形
public final static int CLIPTYPE3 = 2; //等比例矩形
//These matrices will be used to move and zoom image
private Matrix savedMatrix = new Matrix();
private Matrix matrix = new Matrix();
//Remember some things for zooming
private PointF start = new PointF();
private PointF mid = new PointF();
private float oldDist = 1f;
//We can be in one of these 3 states
private final int NONE = 0;
private final int DRAG = 1;
private final int ZOOM = 2;
private int mode = NONE;
//default Clip width, height, type
private boolean hadDraw = false;
private int clipwidth = 1200;
private int clipheight= 1200;
private int cliptype = 0;
private int width;
private int height;
public CustomClipView(Context context) {
super(context);
}
public CustomClipView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomClipView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
hadDraw = true;
setView();
// Paint paint = new Paint();
// paint.setColor(0xaa000000);
// canvas.drawRect(0, 0, width, height / 4, paint);
// canvas.drawRect(0, height / 4, (width - height / 2) / 2, height * 3 / 4, paint);
// canvas.drawRect((width + height / 2) / 2, height / 4, width, height * 3 / 4, paint);
// canvas.drawRect(0, height * 3 / 4, width, height, paint);
// paint.setColor(getResources().getColor(R.color.white));
// canvas.drawRect((width - height / 2) / 2 - 1, height / 4 - 1, (width + height / 2) / 2 + 1, (height / 4), paint);
// canvas.drawRect((width - height / 2) / 2 - 1, height / 4, (width - height / 2) / 2, height * 3 / 4, paint);
// canvas.drawRect((width + height / 2) / 2, height / 4, (width + height / 2) / 2 + 1, height * 3 / 4, paint);
// canvas.drawRect((width - height / 2) / 2 - 1, height * 3 / 4, (width + height / 2) / 2 + 1, height * 3 / 4 + 1, paint);
//drawRect of clip width, height
Paint paint = new Paint();
paint.setColor(0xaa000000);
canvas.drawRect(0, 0, width, (height - clipheight)/2, paint);
canvas.drawRect(0, (height - clipheight)/2, (width - clipwidth)/2, (height + clipheight)/2, paint);
canvas.drawRect((width + clipwidth)/2, (height - clipheight)/2, width, (height + clipheight)/2, paint);
canvas.drawRect(0, (height + clipheight)/2, width, height, paint);
paint.setColor(0xffffff);
canvas.drawRect((width - clipwidth)/2, (height - clipheight)/2 - 1, (width + clipwidth)/2, (height - clipheight)/2 + 1, paint);
canvas.drawRect((width - clipwidth)/2 - 1, (height - clipheight)/2, (width - clipwidth)/2 + 1, (height + clipheight)/2, paint);
canvas.drawRect((width + clipwidth)/2 - 1, (height - clipheight)/2, (width + clipwidth)/2 + 1, (height + clipheight)/2, paint);
canvas.drawRect((width - clipwidth)/2, (height + clipheight)/2 - 1, (width + clipwidth)/2, (height + clipheight)/2 + 1, paint);
}
public boolean onTouch(View v, MotionEvent event) {
// Handle touch events here...
ImageView view = (ImageView) v;
switch (event.getAction() & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
mode = DRAG;
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
if (oldDist > 10f)
{
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG)
{
// ...
matrix.set(savedMatrix);
matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);
} else if (mode == ZOOM)
{
float newDist = spacing(event);
if (newDist > 10f)
{
matrix.set(savedMatrix);
float scale = newDist / oldDist;
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
break;
}
view.setImageMatrix(matrix);
return true;
}
/** Determine the space between the first two fingers */
private float spacing(MotionEvent event)
{
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}
/** Calculate the mid point of the first two fingers */
private void midPoint(PointF point, MotionEvent event)
{
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
point.set(x / 2, y / 2);
}
public String ActionClip(Window window, String filedir){
Bitmap fianBitmap = getBitmap(window);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
fianBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] bitmapByte = baos.toByteArray();
try {
File file = new File(filedir);
if(!file.exists()){
file.mkdirs();
}
String imageFileName = filedir + "PIC_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()) + ".jpg";
FileOutputStream fos = new FileOutputStream(imageFileName);
fos.write(baos.toByteArray());
fos.flush();
fos.close();
return imageFileName;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public Bitmap getBitmap(Window window){
int[] location = new int[2];
this.getLocationOnScreen(location);
setView();
//getBarHeight(window);
Bitmap screenShoot = takeScreenShot(window);
Bitmap finalBitmap = Bitmap.createBitmap(screenShoot, (width - clipwidth)/2+1 + location[0], (height - clipheight)/2+1 + location[1], clipwidth-2, clipheight-2);
// Bitmap finalBitmap = Bitmap.createBitmap(screenShoot, (width - height / 2) / 2, height / 4 + titleBarHeight + statusBarHeight + head, height / 2, height / 2);
return finalBitmap;
}
// int titleBarHeight = 0;
// int statusBarHeight = 0;
// private void getBarHeight(Window window)
// {
// Rect frame = new Rect();
// window.getDecorView().getWindowVisibleDisplayFrame(frame);
// statusBarHeight = frame.top;
// int contenttop = window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
// titleBarHeight = contenttop - statusBarHeight;
// }
private Bitmap takeScreenShot(Window window)
{
View view = window.getDecorView();
view.setDrawingCacheEnabled(false);
view.destroyDrawingCache();
view.buildDrawingCache();
return view.getDrawingCache();
}
private void setView() {
if (clipwidth <= 0 || clipwidth > getDisplayWidthPx()){
clipwidth = getDisplayWidthPx();
}
if (clipheight <= 0 || clipheight > getDisplayHeightPx()){
clipwidth = getDisplayHeightPx();
}
width = this.getWidth();
height = this.getHeight();
clipwidth = (clipwidth > width) ? width : clipwidth;
switch (cliptype) {
case CLIPTYPE1:
clipheight = clipwidth;
break;
case CLIPTYPE2:
clipheight = (clipheight > height) ? height : clipheight;
break;
case CLIPTYPE3:
clipheight = (clipwidth > width) ? (int) Math.ceil(clipheight/(clipwidth*1.0/clipwidth)) : clipheight;
if(clipheight > height){
clipheight = height;
clipwidth = (int) Math.ceil(clipwidth/(clipheight*1.0/clipheight));
}
break;
default:
break;
}
}
public void setClipParamsPx(int width, int height, int type){
clipwidth = width;
clipheight = height;
cliptype = type;
if (hadDraw) {
setView();
invalidate();
}
}
public void setClipParamsDp(int width, int height, int type){
clipwidth = dip2px(width);
clipheight = dip2px(height);
cliptype = type;
if (hadDraw) {
setView();
invalidate();
}
}
/**
* 获取设备屏幕宽度 px
*/
private int getDisplayWidthPx() {
return getContext().getResources().getDisplayMetrics().widthPixels;
}
/**
* 获取设备屏幕高度 px
*/
private int getDisplayHeightPx() {
return getContext().getResources().getDisplayMetrics().heightPixels;
}
/**
* 根据手机的分辨率从 dp的单位 转成为 px(像素)
*/
private int dip2px(float dpValue) {
final float scale = getContext().getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
* 根据手机的分辨率从 px(像素)的单位 转成为 dp
*/
private int px2dip( float pxValue) {
final float scale = getContext().getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
}