使用view的post函数,当view加载完成时会调用,传入的位图大小就是view的大小
tuYaVieew.post(new Runnable() { @Override public void run() { tuYaVieew.setBitmap(Bitmap.createBitmap(tuYaVieew.getMeasuredWidth(),tuYaVieew.getMeasuredHeight(), Bitmap.Config.ARGB_8888)); } });
//外部设置位图,执行初始化画板 public void setBitmap(Bitmap bitmap) { this.bitmap = bitmap; bitmapcanvas = new Canvas(bitmap); bitmapcanvas.drawColor(Color.WHITE); initView(); }
//初始化画板,画笔 private void initView() { setFocusable(true); setFocusableInTouchMode(true); setKeepScreenOn(true); paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG); paint.setStrokeWidth(10); paint.setColor(Color.BLUE); paint.setStyle(Paint.Style.STROKE); paint.setStrokeJoin(Paint.Join.ROUND); paint.setStrokeCap(Paint.Cap.ROUND); }
//绘制涂鸦 @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: { x = event.getX(); y = event.getY(); break; } case MotionEvent.ACTION_MOVE: { float stopx,stopy; stopx = event.getX(); stopy = event.getY(); bitmapcanvas.drawLine(x,y,stopx,stopy,paint); x = stopx; y = stopy; invalidate(); break; } } return true; }
//更新视图 @Override protected void onDraw(Canvas canvas) { if(bitmap != null) canvas.drawBitmap(bitmap,0,0,paint); }
package com.nihaoa.tuya; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; public class TuYaVieew extends View { private Paint paint; private Bitmap bitmap; private Canvas bitmapcanvas; float x,y; public TuYaVieew(Context context) { super(context); initView(); } public TuYaVieew(Context context, @Nullable AttributeSet attrs) { super(context, attrs); initView(); } public TuYaVieew(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(); } public TuYaVieew(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); initView(); } //更新视图 @Override protected void onDraw(Canvas canvas) { if(bitmap != null) canvas.drawBitmap(bitmap,0,0,paint); } //绘制涂鸦 @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: { x = event.getX(); y = event.getY(); break; } case MotionEvent.ACTION_MOVE: { float stopx,stopy; stopx = event.getX(); stopy = event.getY(); bitmapcanvas.drawLine(x,y,stopx,stopy,paint); x = stopx; y = stopy; invalidate(); break; } } return true; } //初始化画板,画笔 private void initView() { setFocusable(true); setFocusableInTouchMode(true); setKeepScreenOn(true); paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG); paint.setStrokeWidth(10); paint.setColor(Color.BLUE); paint.setStyle(Paint.Style.STROKE); paint.setStrokeJoin(Paint.Join.ROUND); paint.setStrokeCap(Paint.Cap.ROUND); } //外部设置位图,执行初始化画板 public void setBitmap(Bitmap bitmap) { this.bitmap = bitmap; bitmapcanvas = new Canvas(bitmap); bitmapcanvas.drawColor(Color.WHITE); initView(); } //注入画笔 public void setPaint(Paint paint) { this.paint = paint; } //获取位图 public Bitmap getBitmap() { return this.bitmap; } }