图片数据来源:
添加要编辑图片的URL;
关于URL的获取这边的例子是 截屏图片:
getWindowBitmapPath()方法内容请移步
Android 截屏并保存到本地(兼容Android 10.0)_深海呐的博客-CSDN博客调用:mImageView?.setImageURI(getWindowBitmapPath())截屏方法: /** * author:xingHai.zhao * duty: 截屏并返回保存地址 */ fun getWindowBitmapPath(): Uri? { val view = activity?.window?.decorView val bitmap: Bitmap = Bitmap.createB
https://blog.csdn.net/qq_39731011/article/details/123640342
R.id.screenshot -> {//截屏
Toast.makeText(activity, "截图成功!已保存到相册", Toast.LENGTH_SHORT).show()
val windowBitmapPath = getWindowBitmapPath()
startSetImg(windowBitmapPath)
}
操作按钮初始化(含部分必要逻辑)
fun startSetImg(url: Uri?) {
findViewById<View>(R.id.screenshot_layout).visibility = View.VISIBLE
var edit = findViewById<EditText>(R.id.screenshot_mat_edit)
val mat = findViewById<MouseMat>(R.id.screenshot_mat)
mat.setImageURI(url)
findViewById<View>(R.id.screenshot_mat_blue).setOnClickListener {
mat.setColor(Color.BLUE)
}
findViewById<View>(R.id.screenshot_mat_red).setOnClickListener {
mat.setColor(Color.RED)
}
findViewById<View>(R.id.screenshot_mat_green).setOnClickListener {
mat.setColor(Color.GREEN)
}
findViewById<View>(R.id.screenshot_mat_back).setOnClickListener {
mat.back()
}
findViewById<View>(R.id.screenshot_mat_delete).setOnClickListener {
mat.reset()
}
findViewById<View>(R.id.screenshot_mat_text).setOnClickListener {
if (edit.text.isNotEmpty())
mat.addText(edit.text.toString())
}
findViewById<View>(R.id.screenshot_mat_text_back).setOnClickListener {
mat.backText()
}
findViewById<View>(R.id.screenshot_mat_save).setOnClickListener {
val view = mat //findViewById<View>(R.id.screenshot_layout)
val bitmap: Bitmap = Bitmap.createBitmap(
view?.width ?: 0, view?.height ?: 0,
Bitmap.Config.ARGB_8888
)
view?.draw(Canvas(bitmap))
ImageSaveUtil.saveAlbum(this, bitmap, Bitmap.CompressFormat.JPEG, 80, true)
mat.reset()
findViewById<View>(R.id.screenshot_layout).visibility = View.GONE
}
}
布局
<FrameLayout
android:id="@+id/screenshot_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone">
<TextView
android:id="@+id/screenshot_background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#aa112233"
android:visibility="visible" />
<com.ys.bdtp.adm.utils.MouseMat
android:id="@+id/screenshot_mat"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="60dp"
android:background="#00000000"
android:visibility="visible" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/screenshot_mat_red"
android:layout_width="40dp"
android:layout_height="30dp"
android:layout_marginRight="12dp"
android:background="#ff0000"
android:padding="6dp" />
<TextView
android:id="@+id/screenshot_mat_blue"
android:layout_width="40dp"
android:layout_height="30dp"
android:layout_marginRight="12dp"
android:background="#0000ff"
android:padding="6dp" />
<TextView
android:id="@+id/screenshot_mat_green"
android:layout_width="40dp"
android:layout_height="30dp"
android:layout_marginRight="12dp"
android:background="#00ff00"
android:padding="6dp" />
<Button
android:id="@+id/screenshot_mat_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:padding="6dp"
android:text="删线" />
<Button
android:id="@+id/screenshot_mat_text_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:padding="6dp"
android:text="删字" />
<Button
android:id="@+id/screenshot_mat_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:padding="6dp"
android:text="还原" />
<Button
android:id="@+id/screenshot_mat_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:padding="6dp"
android:text="添字" />
<EditText
android:id="@+id/screenshot_mat_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:hint="输入添加的文字"
android:padding="6dp" />
<Button
android:id="@+id/screenshot_mat_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:padding="6dp"
android:text="保存" />
</LinearLayout>
</FrameLayout>
画板实体类
/**
* Author:XingHai.Zhao
* Purpose:画板
*/
public class MouseMat extends androidx.appcompat.widget.AppCompatImageView {
private boolean isText;
private String thisText;
class TextBean {
public TextBean(String text, float x, float y, Paint paint) {
this.text = text;
this.x = x;
this.y = y;
this.paint = paint;
}
String text = "";
float x;
float y;
Paint paint;
}
public MouseMat(Context context) {
super(context);
}
public MouseMat(Context context, AttributeSet attrs) {
super(context, attrs);
init();
setClickable(true);//设置可点击
}
public MouseMat(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
setClickable(true);//设置可点击
}
private List<Path> pathList = new ArrayList<>();
private List<Paint> paintList = new ArrayList<>();
private List<TextBean> textList = new ArrayList<>();
private int thisColor = Color.RED;
private void init() {
path = new Path();
paint = new Paint();
paint.setColor(thisColor);//颜色
paint.setStyle(Paint.Style.STROKE);
paint.setTypeface(Typeface.DEFAULT);
paint.setAntiAlias(true);
paint.setTextSize(40);
paint.setStrokeWidth(2); //边框宽度
}
public MouseTouchListener mouseTouchListener;
public interface MouseTouchListener {
void MouseEvent(MotionEvent event);
}
public void setMouseTouchListener(MouseTouchListener mouseTouchListener) {
this.mouseTouchListener = mouseTouchListener;
}
Path path = new Path();
Paint paint = new Paint();
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event) {
if (!isText) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
init();
path.moveTo(event.getX(), event.getY());
break;
case MotionEvent.ACTION_MOVE:
path.lineTo(event.getX(), event.getY());
invalidate();
break;
case MotionEvent.ACTION_UP:
pathList.add(0, path);
paintList.add(0, paint);
invalidate();
break;
}
} else {
//文字添加模式
if (event.getAction() == MotionEvent.ACTION_UP) {
if (thisText != null) {
textList.add(0, new TextBean(thisText, event.getX(), event.getY(),paint));
invalidate();
}
isText = false;
}
}
if (mouseTouchListener != null)
mouseTouchListener.MouseEvent(event);
return super.onTouchEvent(event);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (int i = 0; i < pathList.size(); i++) {
try {
canvas.drawPath(pathList.get(i), paintList.get(i));
} catch (Exception e) {
e.printStackTrace();
}
}
for (int i = 0; i < textList.size(); i++) {
try {
canvas.drawText(textList.get(i).text, textList.get(i).x,
textList.get(i).y, textList.get(i).paint);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* Author:XingHai.Zhao
* Purpose: 清除内容
*/
public void reset() {
pathList.clear();
textList.clear();
invalidate();
}
public void back() {
if (paintList.size() > 0) {
try {
pathList.remove(0);
} catch (Exception e) {
e.printStackTrace();
}
invalidate();
}
}
public void backText() {
if (textList.size() > 0) {
try {
textList.remove(0);
} catch (Exception e) {
e.printStackTrace();
}
invalidate();
}
}
/**
* Author:XingHai.Zhao
* Purpose: 设置颜色
*/
public void setColor(int color) {
this.thisColor = color;
}
public void addText(String text) {
thisText = text;
isText = true;
}
}
