网上的画板代码收集和整理
修改后的代码[1]为,少了一个}
package com.example.administrator.myapplication; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.os.Bundle; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.widget.Button; public class MyWallActivity extends Activity { private SurfaceView mSurfaceView = null; private SurfaceHolder mSurfaceHolder = null; private Button cleanButton = null; private Button colorButton = null; private float oldX = 0f; private float oldY = 0f; private boolean canDraw = false; private Paint mPaint = null; //用来记录当前是哪一种颜色 private int whichColor = 0; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mSurfaceView = (SurfaceView) this.findViewById(R.id.surfaceview); mSurfaceHolder = mSurfaceView.getHolder(); mPaint = new Paint(); //画笔的颜色 mPaint.setColor(Color.RED); //画笔的粗细 mPaint.setStrokeWidth(2.0f); cleanButton = (Button) this.findViewById(R.id.flushbutton); //按钮监听 cleanButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub //锁定整个SurfaceView Canvas mCanvas = mSurfaceHolder.lockCanvas(); mCanvas.drawColor(Color.BLACK); //绘制完成,提交修改 mSurfaceHolder.unlockCanvasAndPost(mCanvas); //重新锁一次 mSurfaceHolder.lockCanvas(new Rect(0, 0, 0, 0)); mSurfaceHolder.unlockCanvasAndPost(mCanvas); } }); colorButton = (Button) this.findViewById(R.id.colorbutton); //按钮监听 colorButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Dialog mDialog = new AlertDialog.Builder(MyWallActivity.this) .setTitle("corlor setting") .setSingleChoiceItems(new String[]{"red", "green", "blue"}, whichColor, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub switch (which) { case 0: { //画笔的颜色 mPaint.setColor(Color.RED); whichColor = 0; break; } case 1: { //画笔的颜色 mPaint.setColor(Color.GREEN); whichColor = 1; break; } case 2: { //画笔的颜色106 mPaint.setColor(Color.BLUE); whichColor = 2; break; } } } }) .setPositiveButton("confirm", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.dismiss(); } }) .create(); mDialog.show(); } }); } @Override public boolean onTouchEvent(MotionEvent event) { //获取x坐标 float x = event.getX(); //获取y坐标(不知道为什么要减去一个偏移值才对得准屏幕) float y = event.getY(); //第一次进来先不管 if(canDraw) { //获取触屏事件 switch(event.getAction()) { //如果是拖动事件 case MotionEvent.ACTION_MOVE: { //锁定整个SurfaceView Canvas mCanvas = mSurfaceHolder.lockCanvas(); mCanvas.drawLine(x, y, oldX, oldY, mPaint); mSurfaceHolder.unlockCanvasAndPost(mCanvas); //重新锁一次 mSurfaceHolder.lockCanvas(new Rect(0, 0, 0, 0)); mSurfaceHolder.unlockCanvasAndPost(mCanvas); break; } } } //保存目前的x坐标值 oldX = x; //保存目前的y坐标值 oldY = y; canDraw = true; return true; } }
[2]里面有个demo可以直接使用,应该是eclipse上的,我转换为了as的,不过有锯齿
[3]使用贝塞尔曲线的方法去除了锯齿
在[4]中可以使用橡皮擦,貌似需要更改一些东西,否则会闪退,可能和用的是as相关
package com.example.administrator.myapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.widget.LinearLayout; public class MainActivity extends Activity { private int SCREEN_W; private int SCREEN_H; private int Pen = 1; private int Eraser = 2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); Button paint = new Button(this); paint.setText("画笔"); layout.addView(paint, params); Button eraser = new Button(this); eraser.setText("橡皮"); layout.addView(eraser, params); final MyView myView = new MyView(this); layout.addView(myView); setContentView(layout); paint.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { myView.setMode(Pen); } }); eraser.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { myView.setMode(Eraser); } }); } //MyView就是自定义的画板 class MyView extends View { private int mMode = 1; private Bitmap mBitmap; private Canvas mCanvas; private Paint mEraserPaint; private Paint mPaint; private Path mPath; private float mX, mY; private static final float TOUCH_TOLERANCE = 4; public MyView(Context context) { super(context); setFocusable(true); setScreenWH(); initPaint(); } private void setScreenWH() { DisplayMetrics dm = new DisplayMetrics(); dm = this.getResources().getDisplayMetrics(); int screenWidth = dm.widthPixels; int screenHeight = dm.heightPixels; SCREEN_W = screenWidth; SCREEN_H = screenHeight; } //设置绘制模式是“画笔”还是“橡皮擦” public void setMode(int mode){ this.mMode = mode; } private void initPaint() { //画笔 mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeCap(Paint.Cap.ROUND); mPaint.setStrokeJoin(Paint.Join.ROUND); mPaint.setColor(Color.BLACK); mPaint.setStrokeWidth(10); //橡皮擦 mEraserPaint = new Paint(); mEraserPaint.setAlpha(0); //这个属性是设置paint为橡皮擦重中之重 //这是重点 //下面这句代码是橡皮擦设置的重点 mEraserPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); //上面这句代码是橡皮擦设置的重点(重要的事是不是一定要说三遍) mEraserPaint.setAntiAlias(true); mEraserPaint.setDither(true); mEraserPaint.setStyle(Paint.Style.STROKE); mEraserPaint.setStrokeJoin(Paint.Join.ROUND); mEraserPaint.setStrokeWidth(30); mPath = new Path(); mBitmap = Bitmap.createBitmap(SCREEN_W, SCREEN_H, Config.ARGB_8888); mCanvas = new Canvas(mBitmap); } @Override protected void onDraw(Canvas canvas) { if (mBitmap != null) { canvas.drawBitmap(mBitmap, 0, 0, mPaint); } super.onDraw(canvas); } private void touch_start(float x, float y) { mPath.reset(); mPath.moveTo(x, y); mX = x; mY = y; //如果是“画笔”模式就用mPaint画笔进行绘制 if (mMode == Pen) { mCanvas.drawPath(mPath, mPaint); } //如果是“橡皮擦”模式就用mEraserPaint画笔进行绘制 if (mMode == Eraser) { mCanvas.drawPath(mPath, mEraserPaint); } } private void touch_move(float x, float y) { float dx = Math.abs(x - mX); float dy = Math.abs(y - mY); if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) { mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2); mX = x; mY = y; if (mMode == Pen) { mCanvas.drawPath(mPath, mPaint); } if (mMode == Eraser) { mCanvas.drawPath(mPath, mEraserPaint); } } } private void touch_up() { mPath.lineTo(mX, mY); if (mMode == Pen) { mCanvas.drawPath(mPath, mPaint); } if (mMode == Eraser) { mCanvas.drawPath(mPath, mEraserPaint); } } @Override public boolean onTouchEvent(MotionEvent event) { float x = event.getX(); float y = event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: touch_start(x, y); invalidate(); break; case MotionEvent.ACTION_MOVE: touch_move(x, y); invalidate(); break; case MotionEvent.ACTION_UP: touch_up(); invalidate(); break; } return true; } } }
[1] 一个简单涂鸦http://www.jizhuomi.com/android/example/19.html
[2] 安卓实现代码的多种方式
[3] 安卓path类的lineTo和quadTo方法的区别
[4] 带画板http://www.mamicode.com/info-detail-1480421.html