一、布局,背景图片是一张灰色的照片
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="66dp"
android:layout_alignParentTop="true"
android:src="@mipmap/a"
/>
<LinearLayout
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/red_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="红色"/>
<Button
android:id="@+id/green_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="绿色"/>
<Button
android:id="@+id/brush"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="刷子"/>
<Button
android:id="@+id/save_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="保存"/>
<Button
android:id="@+id/clrar_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="清除"/>
</LinearLayout>
</RelativeLayout>
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
二、MainActivity代码
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView mIv;
private int mStartX;
private int mStartY;
private static final String TAG = "小家辉哈哈哈";
private Canvas mCanvas;
private Paint mPaint;
private ImageView iv;
private Button red_btn;
private Button green_btn;
private Button brush;
private Button save_btn;
private Button clrar_btn;
private Bitmap mBmCopy;
private OutputStream mStream;
@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
initView();
//加载原图
Bitmap bmSrc = BitmapFactory.decodeResource(getResources(), R.mipmap.bg);
//创建白纸
mBmCopy = Bitmap.createBitmap(bmSrc.getWidth(), bmSrc.getHeight(), bmSrc.getConfig());
//创建画板
mCanvas = new Canvas(mBmCopy);
//创建画笔
mPaint = new Paint();
//开始画,将原图的内容绘制在白纸上
mCanvas.drawBitmap(bmSrc, new Matrix(), mPaint);
//将绘制的图放入imageView中
mIv.setImageBitmap(mBmCopy);
//设置触摸监听
mIv.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//获取用户所按下的X、Y
mStartX = (int) event.getX();
mStartY = (int) event.getY();
//Toast.makeText( MainActivity.this, " ACTION_DOWN : " + " mStartX : " + mStartX + " mStartY: " + mStartY, Toast.LENGTH_SHORT).show();
break;
case MotionEvent.ACTION_MOVE:
int x = (int) event.getX();
int y = (int) event.getY();
mCanvas.drawLine(mStartX, mStartY, x, y, mPaint);
//绘制完成后本次结束的位置,是下次开始的位置,所以进行初始化
mStartX = x;
mStartY = y;
mIv.setImageBitmap(mBmCopy);
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
});
}
private void initView() {
mIv = findViewById(R.id.iv);
iv = (ImageView) findViewById(R.id.iv);
iv.setOnClickListener(this);
red_btn = (Button) findViewById(R.id.red_btn);
red_btn.setOnClickListener(this);
green_btn = (Button) findViewById(R.id.green_btn);
green_btn.setOnClickListener(this);
brush = (Button) findViewById(R.id.brush);
brush.setOnClickListener(this);
save_btn = (Button) findViewById(R.id.save_btn);
save_btn.setOnClickListener(this);
clrar_btn = (Button) findViewById(R.id.clrar_btn);
clrar_btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.red_btn:
mPaint.setColor(Color.RED);
break;
case R.id.green_btn:
mPaint.setColor(Color.GREEN);
break;
case R.id.brush:
mPaint.setStrokeWidth(55);
break;
case R.id.save_btn://保存到sd卡
//创建文件夹,需要写入内存的权限
File file = new File("/sdcard/wjh");
FileOutputStream fos = null ;
//判断文件是否为空
if(!file.exists()){
//为空创建
file.mkdir();
try {
//创建输出流,将文件的路径和照片的命名
fos = new FileOutputStream(file.getPath() + "/2.png");
//为图片设置格式
mBmCopy.compress(Bitmap.CompressFormat.PNG,100,fos);
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
try {
//关闭流
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
break;
case R.id.clrar_btn:
mPaint.setColor(Color.rgb(245,245,245));
mPaint.setStrokeWidth(55);
break;
}
}
}
---------------------
作者:家辉,喂
来源:CSDN
原文:https://blog.csdn.net/jiahui6666/article/details/83719382
版权声明:本文为博主原创文章,转载请附上博文链接!
|
|