Android画板的实现
这是一个常见的画板功能,常用于画画和手写输入等等,今天就教大家实现这个小功能,这个功能还是比较简单的,只有一个Java文件
先看效果图
布局代码,只有三个按钮和一张图片
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.dell.myapplication.Main2Activity">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="bold"
android:text="变粗" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="changeColor"
android:text="改变颜色" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="save"
android:text="保存图片" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
Java代码,每个方法都很明了,其中最重要的是ImageView的触摸事件,这是画画的关键,还要留意图片的初始化操作
public class Main2Activity extends AppCompatActivity {
private ImageView imageView;
private Bitmap copyBitmap;
private Paint paint;
private Canvas canvas;
private float startX;
private float startY;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
imageView = (ImageView) findViewById(R.id.iv_image);
//使用Bitmap工厂把图片加载进来
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.huaban);
//创建一个空的图片,宽度和高度 还有信息跟原图片一样
copyBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
//创建画笔
paint = new Paint();
//创建一个画布
canvas = new Canvas(copyBitmap);
//开始画画
canvas.drawBitmap(bitmap, new Matrix(), paint);
imageView.setImageBitmap(copyBitmap);
//图片的触摸事件
imageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//获取动作的事件
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
//按下事件
startX = event.getX();
startY = event.getY();
Log.e("按下", startX + "," + startY);
break;
case MotionEvent.ACTION_MOVE:
//滑动事件
float x = event.getX();
float y = event.getY();
//在画布上画直线,不能画点,滑动事件获得的坐标不是连续的
canvas.drawLine(startX, startY, x, y, paint);
//更新图片
imageView.setImageBitmap(copyBitmap);
startX = x;
startY = y;
Log.e("滑动", x + "," + y);
break;
case MotionEvent.ACTION_UP:
//抬起事件
float upX = event.getX();
float upY = event.getY();
Log.e("抬起", upX + "," + upY);
break;
}
//必须设置为true,否则只执行按下事件
return true;
}
});
}
//修改画笔的颜色
public void changeColor(View view) {
paint.setColor(Color.RED);
}
//设置画笔的粗细
public void bold(View view) {
paint.setStrokeWidth(5);
}
//保存图片
public void save(View view) {
//创建一个图片的文件
File file = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".png");
FileOutputStream straeam;
try {
straeam = new FileOutputStream(file);
//生成图片,参数①为图片的类型,参数②为图片质量,参数③为文件输出流
copyBitmap.compress(Bitmap.CompressFormat.PNG, 100, straeam);
Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}