用Android studio写构建画板文件。包括以下功能:1、修改描边宽度;2、更改画板颜色;3、擦除;4、全部清除;5、保存;6、退出。
时间: 2023-06-20 19:04:12 浏览: 52
好的,下面是一个简单的实现:
首先,我们需要一个画板布局文件,可以使用一个`SurfaceView`来实现:
```xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<SurfaceView
android:id="@+id/surface_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
```
然后,我们需要一个自定义的`SurfaceView`类,来实现画板的绘制和操作:
```java
public class DrawingView extends SurfaceView implements SurfaceHolder.Callback {
private Paint paint;
private Path path;
private Canvas canvas;
private SurfaceHolder surfaceHolder;
private int strokeColor = Color.BLACK;
private int strokeWidth = 10;
public DrawingView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
surfaceHolder = getHolder();
surfaceHolder.addCallback(this);
paint = new Paint();
paint.setColor(strokeColor);
paint.setStrokeWidth(strokeWidth);
paint.setStyle(Paint.Style.STROKE);
paint.setAntiAlias(true);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeJoin(Paint.Join.ROUND);
path = new Path();
}
public void setStrokeColor(int color) {
strokeColor = color;
paint.setColor(strokeColor);
}
public void setStrokeWidth(int width) {
strokeWidth = width;
paint.setStrokeWidth(strokeWidth);
}
public void clear() {
canvas.drawColor(Color.WHITE);
}
public void erase() {
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
}
public void resetEraser() {
paint.setXfermode(null);
}
public void save(String fileName) {
try {
FileOutputStream fos = new FileOutputStream(fileName);
Bitmap bmp = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
canvas.setBitmap(bmp);
draw(canvas);
bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
canvas = surfaceHolder.lockCanvas();
clear();
surfaceHolder.unlockCanvasAndPost(canvas);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(x, y);
break;
case MotionEvent.ACTION_MOVE:
path.lineTo(x, y);
canvas = surfaceHolder.lockCanvas();
canvas.drawPath(path, paint);
surfaceHolder.unlockCanvasAndPost(canvas);
break;
default:
return false;
}
return true;
}
}
```
在`init`方法中,我们初始化了`SurfaceHolder`、`Paint`和`Path`对象。`Paint`对象控制描边颜色、宽度、样式等属性。
`setStrokeColor`和`setStrokeWidth`方法用于修改描边颜色和宽度。
`clear`方法用于清空画板,`erase`方法用于擦除,`resetEraser`方法用于重置擦除模式。
`save`方法用于将画板保存为图片文件。
在`surfaceCreated`方法中,我们初始化了`Canvas`对象,并使用`clear`方法清空画板。
在`onTouchEvent`方法中,我们处理了触摸事件,将绘制路径添加到`Path`对象中,并使用`Canvas`对象进行绘制。
最后,在`Activity`中使用`DrawingView`类即可:
```java
public class MainActivity extends AppCompatActivity {
private DrawingView drawingView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawingView = findViewById(R.id.drawing_view);
}
public void onColorSelected(View view) {
int color = ((ColorDrawable) view.getBackground()).getColor();
drawingView.setStrokeColor(color);
}
public void onWidthSelected(View view) {
int width = Integer.parseInt(view.getTag().toString());
drawingView.setStrokeWidth(width);
}
public void onClearAll(View view) {
drawingView.clear();
}
public void onErase(View view) {
drawingView.erase();
}
public void onSave(View view) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String fileName = "drawing_" + sdf.format(new Date()) + ".png";
drawingView.save(Environment.getExternalStorageDirectory() + "/" + fileName);
Toast.makeText(this, "Saved " + fileName, Toast.LENGTH_SHORT).show();
}
public void onExit(View view) {
finish();
}
}
```
其中,`onColorSelected`和`onWidthSelected`方法用于处理颜色和宽度选择,`onClearAll`方法用于清空画板,`onErase`方法用于擦除,`onSave`方法用于保存画板,`onExit`方法用于退出应用程序。注意,在保存画板时,需要添加`WRITE_EXTERNAL_STORAGE`权限。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)