涂鸦功能

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cuizehui123/article/details/81270475

canvers.setMartex 按照矩阵变换

  mCacheCanvas.save();
  mCacheCanvas.setMatrix(matrix);
  mCacheCanvas.drawBitmap(bitmap, null, new RectF(x, y, x + width, y + height), null);
  mCacheCanvas.restore();

抗锯齿
给Canvas加上抗锯齿标志。

canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG));  

绘制画笔

if (type == TYPE_BRUSH) {
            paint.setAntiAlias(true);
            paint.setDither(true);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeJoin(Paint.Join.ROUND);
            paint.setStrokeCap(Paint.Cap.ROUND);
        } else if (type == TYPE_ERASE) {
            paint.setAntiAlias(true);
            paint.setDither(true);
            paint.setColor(Color.WHITE);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeJoin(Paint.Join.ROUND);
            paint.setStrokeCap(Paint.Cap.ROUND);
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
        }

获取屏幕宽高

public static DisplayMetrics getRealDisplayMetrics(Context context) {
        DisplayMetrics metrics = new DisplayMetrics();
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        if (wm != null) {
            if (hasJellyBeanMr1()) {
                wm.getDefaultDisplay().getRealMetrics(metrics);
            } else {
                wm.getDefaultDisplay().getMetrics(metrics);
            }
        }
        return metrics;
    }
        DisplayMetrics metrics = DoodleUtils.getRealDisplayMetrics(getContext());
        int width = Math.min(metrics.widthPixels, metrics.heightPixels);
        int height = Math.max(metrics.widthPixels, metrics.heightPixels);
//获取屏幕区域的宽高等尺寸获取
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int widthPixels = metrics.widthPixels;
int heightPixels = metrics.heightPixels;

获取点击位置
getX() 当前触摸事件距离当前View左边的距离

 private void touchDown(MotionEvent event) {
        preX = startX = event.getX();
        preY = startY = event.getY();
        if (mode == MODE_NONE) {
            mode = MODE_DRAW_BASIC_SHAPE;
        }
        if (listener != null) {
            if (mode == MODE_DRAW_BASIC_SHAPE) {
                listener.onDrawStart(startX, startY);
            } else if (mode == MODE_DRAW_STICKER) {
                listener.onStickerDragStart(startX, startY);
            }
        }
    }

换算尺寸:



    private float screeX2DataX(float screeX) {
        return screeX * 2 / mCanvasWidth - 1.0f;
    }

    private float screenY2dataY(float screenY) {
        return screenY * 2 / mCanvasHeight - 1.0f;
    }


    private float dataX2screenX(float dataX) {
        return (dataX + 1.0f) * mCanvasWidth / 2;
    }

    private float dataY2screenY(float dataY) {
        return (dataY + 1.0f) * mCanvasHeight / 2;
    }

获得当前屏幕旋转角度

  private Matrix getTransformMatrix() {
        int rotation = mWindowManager.getDefaultDisplay().getRotation();
        return mMatrices[rotation];
    }

创建bitmap 放在canvase上

mCacheBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

        mCacheCanvas = new Canvas();
        mCacheCanvas.setDrawFilter(mDrawFilter);
        mCacheCanvas.setBitmap(mCacheBitmap);

绘制矩阵

   mCacheCanvas.save();
                mCacheCanvas.setMatrix(matrix);
                mCacheCanvas.drawPath(path, paint);
                mCacheCanvas.restore();

drawImage

猜你喜欢

转载自blog.csdn.net/cuizehui123/article/details/81270475