ImageSelectActivity.java
7.62 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
package com.hjx.parent;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;
import com.hjx.parent.databinding.ActivityImageSelectBinding;
import com.prws.common.bean.CutPicBean;
import com.prws.common.bean.ImageBean;
import com.prws.common.utils.BitmapUtils;
import com.prws.common.utils.CommonUtil;
import com.prws.common.widget.MarkSizeView;
public class ImageSelectActivity extends BaseActivity<ActivityImageSelectBinding> {
private Bitmap bitmap;
Rect rect;
private double widthFold, heightFold;
private Bitmap backGroundBitmap;
private int bitmapWidth, bitmapHeight;
private Rect select;
@Override
public void initView(Bundle savedInstanceState) {
String path = getIntent().getStringExtra("path");
bitmap = BitmapFactory.decodeFile(path);
showImage();
binding.markView.setmOnClickListener(new MarkSizeView.onClickListener() {
@Override
public void onConfirm(Rect markedArea) {
int markSizeViewWidth = binding.markView.getWidth();
int markSizeHeight = binding.markView.getHeight();
backGroundBitmap = binding.markView.getBackGroundBitmap();
bitmapWidth = backGroundBitmap.getWidth();
bitmapHeight = backGroundBitmap.getHeight();
widthFold = CommonUtil.div(bitmapWidth, markSizeViewWidth, 2);
heightFold = CommonUtil.div(bitmapHeight, markSizeHeight, 2);
Log.d(toString(), markedArea.toString());
drawRectangles(markedArea);
binding.markView.reset();
}
@Override
public void onCancel() {
rect = null;
}
@Override
public void onTouch() {
}
});
binding.tvSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (rect != null) {
if (select == null) {
Intent intent = new Intent();
intent.putExtra("cut", rect);
setResult(1004, intent);
finish();
} else {
Intent intent = new Intent();
intent.putExtra("cut", rect);
intent.putExtra("position", getIntent().getIntExtra("position", 0));
setResult(1005, intent);
finish();
}
} else {
showToast("未选择框题区域");
}
}
});
binding.llDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (select == null) {
finish();
} else {
rect = null;
Intent intent = new Intent();
intent.putExtra("cut", rect);
intent.putExtra("position", getIntent().getIntExtra("position", 0));
setResult(1005, intent);
finish();
}
}
});
}
private void simulateClick(int x1, int x2, int y1, int y2) {
long downTime = SystemClock.uptimeMillis();
final MotionEvent downEvent = MotionEvent.obtain(downTime, downTime, MotionEvent.ACTION_DOWN, x1, y1, 0);
downTime += 1000;
final MotionEvent moveEvent = MotionEvent.obtain(downTime, downTime, MotionEvent.ACTION_MOVE, x2, y2, 0);
final MotionEvent upEvent = MotionEvent.obtain(downTime, downTime, MotionEvent.ACTION_UP, x2, y2, 0);
binding.markView.onTouchEvent(downEvent);
binding.markView.onTouchEvent(moveEvent);
binding.markView.onTouchEvent(upEvent);
downEvent.recycle();
moveEvent.recycle();
upEvent.recycle();
}
private void drawRectangles(Rect ints) {
rect = new Rect(ints.left, ints.top, ints.right, ints.bottom);
Log.d(toString(), ints.toString());
Rect rect = new Rect(CommonUtil.mulForInt(ints.left, widthFold), CommonUtil.mulForInt(ints.top, heightFold), CommonUtil.mulForInt(ints.right, widthFold), CommonUtil.mulForInt(ints.bottom, heightFold));
// Log.d(toString(),rect.toString());
Bitmap mutableBitmap = backGroundBitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(mutableBitmap);
//画矩形框
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL);//填充
paint.setStrokeWidth(CommonUtil.dpToPx(this, 1)); //线的宽度
paint.setColor(getColor(R.color.select_fill));
canvas.drawRect(rect, paint);
paint.setStyle(Paint.Style.STROKE);//不填充
paint.setColor(getColor(R.color.select_border));
canvas.drawRect(rect, paint);
binding.markView.setBackGroundBitmap(mutableBitmap);
}
private void showImage() {
float imageHeight = bitmap.getHeight();
float imageWidth = bitmap.getWidth();
float viewHeight = CommonUtil.getScreenHeight(context) - CommonUtil.dpToPx(context, 250);
float viewWidth = CommonUtil.getScreenWidth(context) - CommonUtil.dpToPx(context, 60);
binding.markView.post(new Runnable() {
@Override
public void run() {
binding.markView.reset();
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(binding.markView.getLayoutParams());
if (imageHeight / imageWidth > viewHeight / viewWidth) {
if (viewHeight > imageHeight) {
layoutParams.height = (int) imageHeight;
layoutParams.width = (int) imageWidth;
} else {
layoutParams.height = (int) viewHeight;
layoutParams.width = (int) ((float) (imageWidth / imageHeight) * viewHeight);
}
} else {
if (viewWidth > imageWidth) {
layoutParams.height = (int) imageHeight;
layoutParams.width = (int) imageWidth;
} else {
layoutParams.height = (int) ((float) (imageHeight / imageWidth) * viewWidth);
layoutParams.width = (int) viewWidth;
}
}
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
binding.markView.setLayoutParams(layoutParams);
binding.markView.setBackGroundBitmap(bitmap);
binding.markView.reset();
select = getIntent().getParcelableExtra("rect");
if (select != null) {
simulateClick(select.left, select.right, select.top, select.bottom);
}
}
});
}
@Override
protected void onDestroy() {
if (backGroundBitmap != null) {
backGroundBitmap.recycle();
backGroundBitmap = null;
}
if (bitmap != null) {
bitmap.recycle();
bitmap = null;
}
super.onDestroy();
}
@Override
protected ActivityImageSelectBinding getViewBinding() {
return ActivityImageSelectBinding.inflate(getLayoutInflater());
}
}