1. 程式人生 > >塗鴉功能

塗鴉功能

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