Android如何仅在特定区域(如着色书)中绘制路径?

时间:2016-01-04 10:49:15

标签: android canvas bitmap path draw

我只是在特定区域画一条路径/线条。 例如,我有一个蝴蝶图像(只有白色和黑色边框)。我只是想在图像内部绘画(仅在白色区域)。

图像可缩放,将在绘图视图内的屏幕上显示。

我的想法是,在ACTION_MOVE - 事件期间检测像素的颜色,然后将颜色的alpha设置为0,如果颜色不是白色的话。但如果我在边境上画画,那条线就不会透明。

这是我的代码:

private void touch_move(float x, float y) {

        float dx = Math.abs(x - mX);
        float dy = Math.abs(y - mY);


        if (fill) {
        } else {
            if (!ausmalen) {
                //Standard
                if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
                    mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
                    mX = x;
                    mY = y;
                    circlePath.reset();
                    circlePath.addCircle(mX, mY, circle_radius, Path.Direction.CW);
                }
            } else {
                //Bestimmen der Farben der Pixel für evtl Füllen der View etc
                try{
                    int pixel = mBitmap.getPixel((int) mX, (int) mY);
                if (pixel == Color.WHITE) {
                        mPaint.setAlpha(255);
                        System.out.println("white at " + mX + " " + mY);

                        System.out.println("mX= "+mX+" m>= "+mY+" x= "+x+" y= "+y);
                        mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
                        mX = x;
                        hX = x;
                        mY = y;
                        hY = y;
                        circlePath.reset();
                        circlePath.addCircle(mX, mY, circle_radius, Path.Direction.CW);
                        mCanvas.drawPath(mPath,mPaint);                     
                } else {
                    mPath.reset();
                    mPath.moveTo(mX, mY);
                    System.out.println("nicht weiß!!!!!!");
                    mPaint.setAlpha(0);
                    mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
                    mX = x;
                    hX = x;
                    mY = y;
                    hY = y;
                    circlePath.reset();
                    circlePath.addCircle(mX, mY, circle_radius, Path.Direction.CW);
                    System.out.println("hX= " + hX + " hY= " + hY + " mX= " + mX + " mY= " + mY + " stroke= " + mPaint.getStrokeWidth());
                    int alpha = Color.alpha(pixel);
                    int redValue = Color.red(pixel);
                    int blueValue = Color.blue(pixel);
                    int greenValue = Color.green(pixel);
                    System.out.println("r= " + redValue + " b= " + blueValue + " g= " + greenValue + " a= " + alpha);
                    mCanvas.drawPath(mPath, mPaint);
                }
            }
            catch(Exception e){
                System.out.println(e);
            };}
            dv_bg.invalidate();
            dv_bg.refreshDrawableState();
            dv.invalidate();
            dv.refreshDrawableState();
            setDrawingCacheEnabled(true);
        }

感谢每一位帮助。也许还有更好的方法来实现这一目标。很多。

0 个答案:

没有答案