设计一个手绘图形的画板
使用java语言,在Android studio平台上进行开发
实现一个可以绘图的画板,界面有相关的选择按钮。可以根据按钮切换画笔的颜色,刷子可以加粗画笔的线条大小,橡皮可以用于抹除已经绘制的图案,清屏可实现清屏重置画板
首先设计界面,然后设计按钮点击功能。橡皮擦的功能可通过把画笔颜色设置与背景颜色一致来实现,清屏功能可通过背景重置覆盖原背景实现
activity_main.xml
HandWrite.java
package com.xdw.exercise;
import android.content.Context;
import android.graphics.*;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class HandWrite extends View
{
Paint paint = null;
Bitmap originalBitmap = null;
Bitmap new1_Bitmap = null;
Bitmap new2_Bitmap = null;
float startX = 0,startY = 0;
float clickX = 0,clickY = 0;
boolean isMove = true;
boolean isClear = false;
int color=Color.BLUE;
float strokeWidth=10.0f;
public HandWrite(Context context, AttributeSet attrs)
{
super(context, attrs);
originalBitmap = BitmapFactory
.decodeResource(getResources(), R.drawable.iv).copy(Bitmap.Config.ARGB_8888,true);
new1_Bitmap = Bitmap.createBitmap(originalBitmap);
}
public void clear(){
isClear = true;
new2_Bitmap = Bitmap.createBitmap(originalBitmap);
invalidate();
}
public void red(){
isClear=false;
color=Color.RED;
}
public void blue(){
isClear=false;
color=Color.BLUE;
}
public void brush(){
strokeWidth=20.0f;
}
public void eraser(){
color=Color.WHITE;
strokeWidth=80.0f;
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
canvas.drawBitmap(HandWriting(new1_Bitmap), 0, 0,null);
}
public Bitmap HandWriting(Bitmap o_Bitmap)
{
Canvas canvas = null;
if(isClear) {
canvas = new Canvas(new2_Bitmap);
}
else{
canvas = new Canvas(o_Bitmap);
}
paint = new Paint();
paint.setStyle(Style.STROKE);
paint.setAntiAlias(true);
paint.setColor(color);
paint.setStrokeWidth(strokeWidth);
if(isMove)
{
canvas.drawLine(startX, startY, clickX, clickY, paint);
}
startX = clickX;
startY = clickY;
if(isClear)
{
return new2_Bitmap;
}
return o_Bitmap;
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
clickX = event.getX();
clickY = event.getY();
if(event.getAction() == MotionEvent.ACTION_DOWN)
{
isMove = false;
invalidate();
return true;
}
else if(event.getAction() == MotionEvent.ACTION_MOVE)
{
isMove = true;
invalidate();
return true;
}
return super.onTouchEvent(event);
}
}
MainActivity.java
package com.xdw.exercise;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private HandWrite handWrite = null;
Button red,blue,clear,brush,eraser;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handWrite = (HandWrite) findViewById(R.id.handwriteview);
red =(Button)findViewById(R.id.red);
blue=(Button)findViewById(R.id.blue);
clear = (Button) findViewById(R.id.clear);
brush=(Button)findViewById(R.id.brush);
eraser=(Button)findViewById(R.id.eraser);
clear.setOnClickListener(new cClick());
red.setOnClickListener(new rClick());
blue.setOnClickListener(new bClick());
brush.setOnClickListener(new brClick());
eraser.setOnClickListener(new eClick());
}
class cClick implements OnClickListener {
public void onClick(View v) {
handWrite.clear();
}
}
class rClick implements OnClickListener {
public void onClick(View v) {
handWrite.red();
}
}
class bClick implements OnClickListener {
public void onClick(View v) {
handWrite.blue();
}
}
class brClick implements OnClickListener {
public void onClick(View v) {
handWrite.brush();
}
}
class eClick implements OnClickListener {
public void onClick(View v) {
handWrite.eraser();
}
}
}
效果显示: