添加了画板的保存,改粗细,改颜色

pull/15/head
dingruihua 3 years ago
parent f2d29565cb
commit e62c3fbc63

@ -23,7 +23,7 @@ import android.view.View;
/**
*
* @category: View
* @category: View
* @author: dingruihua
* @date: 2022.10.10
*
@ -34,7 +34,7 @@ public class PaintView extends View {
private Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;
private Bitmap mBitmap;
public Bitmap mBitmap;
private Paint mPaint;
private ArrayList<DrawPath> savePath;
@ -47,6 +47,9 @@ public class PaintView extends View {
private int bitmapWidth;
private int bitmapHeight;
private int changed_width=0;
private int changed_color=0;
public PaintView(Context c) {
super(c);
//得到屏幕的分辨率
@ -80,7 +83,7 @@ public class PaintView extends View {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(0xFF00FF00);
mPaint.setColor(0xFF000000);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
@ -101,7 +104,29 @@ public class PaintView extends View {
}
public void changewidth(){
if (changed_width==0) {
mPaint.setStrokeWidth(50);
changed_width = 1;
}
else{
mPaint.setStrokeWidth(10);
changed_width = 0;
}
}
public void changecolor(){
if (changed_color==0) {
mPaint.setColor(0xFFFF0000);
changed_color = 1;
}
else{
mPaint.setColor(0xFF000000);
changed_color = 0;
}
}
@Override
protected void onDraw(Canvas canvas) {
@ -126,8 +151,16 @@ public class PaintView extends View {
System.out.println(savePath.size()+"--------------");
if(savePath != null && savePath.size() > 0){
//调用初始化画布函数以清空画布
initCanvas();
//清空画布
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
//画布大小
mBitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight,
Bitmap.Config.RGB_565);
mCanvas = new Canvas(mBitmap); //所有mCanvas画的东西都被保存在了mBitmap中
mCanvas.drawColor(Color.WHITE);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
//将路径保存列表中的最后一个元素删除 ,并将其保存在路径删除列表中
DrawPath drawPath = savePath.get(savePath.size() - 1);
@ -168,52 +201,15 @@ public class PaintView extends View {
public void removeAllPaint(){
//调用初始化画布函数以清空画布
initCanvas();
changed_color=0;
changed_width=0;
invalidate();//刷新
savePath.clear();
deletePath.clear();
}
/*
*
*
*
public String saveBitmap(){
//获得系统当前时间,并以该时间作为文件名
SimpleDateFormat formatter = new SimpleDateFormat ("yyyyMMddHHmmss");
Date curDate = new Date(System.currentTimeMillis());//获取当前时间
String str = formatter.format(curDate);
String paintPath = "";
str = str + "paint.png";
File dir = new File("/sdcard/notes/");
File file = new File("/sdcard/notes/",str);
if (!dir.exists()) {
dir.mkdir();
}
else{
if(file.exists()){
file.delete();
}
}
try {
FileOutputStream out = new FileOutputStream(file);
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
//保存绘图文件路径
paintPath = "/sdcard/notes/" + str;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return paintPath;
}*/
private void touch_start(float x, float y) {

@ -1,6 +1,8 @@
package net.micode.notes.ui;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
@ -8,6 +10,13 @@ import android.widget.ImageButton;
import net.micode.notes.PaintView;
import net.micode.notes.R;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class BoardActivity extends Activity {
private PaintView paintView;
@ -41,6 +50,92 @@ public class BoardActivity extends Activity {
paintView.removeAllPaint();
}
});
final ImageButton paint_save = (ImageButton) findViewById(R.id.save);
//为点击图片按钮设置监听器
paint_save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
saveBitmap();
}
});
final ImageButton paint_width = (ImageButton) findViewById(R.id.paint_width);
//为点击图片按钮设置监听器
paint_width.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
paintView.changewidth();
}
});
final ImageButton paint_color = (ImageButton) findViewById(R.id.paint_color);
//为点击图片按钮设置监听器
paint_color.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
paintView.changecolor();
}
});
final ImageButton paint_back = (ImageButton) findViewById(R.id.paint_back);
//为点击图片按钮设置监听器
paint_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
/* 新建一个Intent对象 */
Intent intent = new Intent();
/* 指定intent要启动的类 */
intent.setClass(BoardActivity.this, NotesListActivity.class);
/* 启动一个新的Activity */
startActivity(intent);
/* 关闭当前的Activity */
BoardActivity.this.finish();
}
});
paintView = (PaintView)findViewById(R.id.paint_layout);
}
/*
*
*/
public String saveBitmap(){
//获得系统当前时间,并以该时间作为文件名
SimpleDateFormat formatter = new SimpleDateFormat ("yyyyMMddHHmmss");
Date curDate = new Date(System.currentTimeMillis());//获取当前时间
String str = formatter.format(curDate);
String paintPath = "";
str = str + "paint.png";
File dir = new File("/sdcard/notes");
File file = new File("/sdcard/notes",str);
if (!dir.exists()) {
dir.mkdir();
}
else{
if(file.exists()){
file.delete();
}
}
try {
FileOutputStream out = new FileOutputStream(file);
paintView.mBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
//保存绘图文件路径
paintPath = "/sdcard/notes/" + str;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return paintPath;
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 445 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 458 B

@ -12,16 +12,16 @@
android:layout_height="match_parent"></net.micode.notes.PaintView>
<ImageButton
android:id="@+id/paint_reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="240dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
android:src="@drawable/paint_reset" />
android:id="@+id/paint_reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="240dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
android:src="@drawable/paint_reset" />
<ImageButton
android:id="@+id/paint_clor"
android:id="@+id/paint_color"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
@ -30,7 +30,7 @@
android:src="@drawable/paint_clor" />
<ImageButton
android:id="@+id/paint"
android:id="@+id/paint_width"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
@ -56,4 +56,20 @@
android:layout_marginBottom="0dp"
android:src="@drawable/undo" />
<ImageButton
android:id="@+id/save"
android:layout_width="50dp"
android:layout_height="59dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="560dp"
android:src="@drawable/paint_save" />
<ImageButton
android:id="@+id/paint_back"
android:layout_width="50dp"
android:layout_height="59dp"
android:layout_marginLeft="360dp"
android:layout_marginTop="560dp"
android:src="@drawable/paint_back" />
</RelativeLayout>

@ -63,7 +63,7 @@
</style>
<style name="NoteActionBarStyle" parent="@android:style/Widget.Holo.Light.ActionBar.Solid">
<item name="android:displayOptions" />
<item name="android:visibility">gone</item>
<!--<item name="android:displayOptions" /-->
<item name="android:visibility">visible</item>
</style>
</resources>
Loading…
Cancel
Save