功能分类:工具
支持平台:Android
运行环境:Eclipse
开发语言:Java
开发工具:Eclipse
源码大小:30.93KB
源码下载地址:http://down.51cto.com/data/1984802
源码简介
该Demo主要以当前桌面为背景,完成画笔的相关功能。
1、画笔的添加、移除等功能;
2、针对不同的生命周期,响应不同的桌面背景。
源码运行截图
桌面背景,进行涂鸦
返回键撤销画笔路径
home,切换背景后,继续涂鸦
画笔涂鸦
源码片段
- public ScreenPaintView(Context context, int w, int h) {
- super(context);
- screenWidth = w;
- screenHeight = h;
- mBitmap = Bitmap.createBitmap(screenWidth, screenHeight,
- Bitmap.Config.ARGB_8888);
- // 保存一次一次绘制出来的图形
- mCanvas = new Canvas(mBitmap);
- mBitmapPaint = new Paint(Paint.DITHER_FLAG);
- mPaint = new Paint();
- mPaint.setAntiAlias(true);
- mPaint.setStyle(Paint.Style.STROKE);
- mPaint.setStrokeJoin(Paint.Join.ROUND);// 设置外边缘
- mPaint.setStrokeCap(Paint.Cap.SQUARE);// 形状
- mPaint.setStrokeWidth(8);// 画笔宽度
- mPaint.setColor(0xFF2145FF);// 画笔颜色
- savePath = new ArrayList<drawpath>();
- }
- @Override
- public void onDraw(Canvas canvas) {
- canvas.drawColor(0x0FFFFFFF);
- // 将前面已经画过得显示出来
- canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
- if (mPath != null) {
- // 实时的显示
- canvas.drawPath(mPath, mPaint);
- }
- }
- private void touch_start(float x, float y) {
- mPath.moveTo(x, y);
- mX = x;
- mY = y;
- }
- private void touch_move(float x, float y) {
- float dx = Math.abs(x - mX);
- float dy = Math.abs(mY - y);
- if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
- // 从x1,y1到x2,y2画一条贝塞尔曲线,更平滑(直接用mPath.lineTo也是可以的)
- // 由此就可以制作各种画笔
- mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
- mX = x;
- mY = y;
- }
- }
- private void touch_up() {
- mPath.lineTo(mX, mY);
- mCanvas.drawPath(mPath, mPaint);
- // 将一条完整的路径保存下来(相当于入栈操作)
- savePath.add(dp);
- mPath = null;// 重新置空
- }</drawpath>