热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Android自定义绘图板应用

本文介绍如何使用Android的Canvas和View组件创建一个简单的绘图板应用程序,支持触摸绘画和保存图片功能。
在 Android 开发中,使用 Canvas 和 View 组件可以实现丰富的图形绘制功能。本文将展示如何创建一个自定义的绘图板应用,用户可以通过触摸屏幕进行绘画,并且可以保存绘制的内容为图片。

### 布局文件 (activity_main.xml)

```xml

android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="保存"
android:OnClick="saveDrawing" />

android:id="@+id/drawingView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />

```

### Java 代码 (MainActivity.java)

```java
package com.example.customdrawingapp;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends Activity {
private ImageView drawingView;
private Bitmap bitmap;
private Canvas canvas;
private Paint paint;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

drawingView = findViewById(R.id.drawingView);

// 初始化画笔
paint = new Paint();
paint.setColor(Color.BLACK);
paint.setStrokeWidth(5f);

// 创建可修改的 Bitmap
int width = getResources().getDisplayMetrics().widthPixels;
int height = getResources().getDisplayMetrics().heightPixels - 200; // 考虑按钮高度
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);
canvas.drawColor(Color.WHITE);

// 设置触摸监听器
drawingView.setOnTouchListener((v, event) -> {
float x = event.getX();
float y = event.getY();

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
canvas.drawPoint(x, y, paint);
break;
case MotionEvent.ACTION_MOVE:
canvas.drawLine(x, y, x + 1, y + 1, paint);
break;
case MotionEvent.ACTION_UP:
drawingView.setImageBitmap(bitmap);
break;
}
return true;
});
}

public void saveDrawing(View view) {
try {
File file = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".png");
FileOutputStream fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
Toast.makeText(this, "图片已成功保存", Toast.LENGTH_LONG).show();

// 通知系统更新媒体库
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
sendBroadcast(intent);
} catch (FileNotFoundException e) {
Toast.makeText(this, "保存失败:文件未找到", Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (IOException e) {
Toast.makeText(this, "保存失败:IO 异常", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
}
```

### 关键点说明

1. **布局文件**:我们使用 `LinearLayout` 来组织界面元素,包括一个按钮用于保存图片和一个 `ImageView` 作为绘图区域。
2. **Java 代码**:通过 `Canvas` 和 `Paint` 对象来处理绘图逻辑,监听触摸事件并实时更新 `ImageView` 中的图像内容。
3. **保存功能**:当用户点击“保存”按钮时,会将当前绘制的内容保存为图片文件,并更新系统的媒体库以确保文件可见。

推荐阅读
author-avatar
mobiledu2502874377
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有