Paint Xfermode再次在android 2.2 ---->上绘制黑线

时间:2013-09-26 17:45:49

标签: android

我想要删除图像的一部分,然后将XferMode设置为清除图像。但是当我在android> 3.0上进行测试时,它工作得很好,它在Android上绘制黑线< 3.0(2.2)。我找不到解决这个问题的方法。谁能解释我为什么? 这是TouchView方法:

public TouchView(Context context) {
        super(context);
        mPath = new Path();
        Display display = getWindowManager().getDefaultDisplay();
        @SuppressWarnings("deprecation")
        int dWidth = display.getWidth();
        @SuppressWarnings("deprecation")
        int dHeight = display.getHeight();
        int id = getIntent().getIntExtra("id", -1);
        bgr1 = BitmapFactory.decodeResource(getResources(),BackgroundAdapter.mThumbIds[id]);
        bgr = Bitmap.createScaledBitmap(bgr1, dWidth, dHeight, true);
        overlay1 = BitmapFactory.decodeResource(getResources(),OverlayAdapter.mThumbIds[id]).copy(Config.ARGB_8888, true);
        overlay = Bitmap.createScaledBitmap(overlay1, dWidth, dHeight, true);
        c2 = new Canvas(
        pTouch = new Paint(/*Paint.ANTI_ALIAS_FLAG*/);
        pTouch.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
        pTouch.setColor(Color.TRANSPARENT);
        pTouch.setDither(true);
        pTouch.setStrokeWidth(20);
        pTouch.setAntiAlias(true);
        pTouch.setFilterBitmap(true);
        pTouch.setStyle(Paint.Style.STROKE);
        pTouch.setMaskFilter(new BlurMaskFilter(10, Blur.   
    }

这将绘制设置为TouchView:

    private void touch_start(float x, float y){

        mPath.reset(); 
        mPath.moveTo(x, y);
        mX = x;
        mY = y;
        mDrawPoint = true;
        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(y - mY);
            if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
                mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);

                mPath.lineTo(mX, mY);
                c2.drawPath(mPath, pTouch);
                mPath.reset();
                mPath.moveTo(mX, mY);

                mX = x;
                mY = y;
            }
    }
    private void touch_up() {
        mPath.reset();          
    }

     public boolean onTouchEvent(MotionEvent event) {
            float x = event.getX();
            float y = event.getY();

            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    touch_start(x, y);
                    invalidate();
                    break;
                case MotionEvent.ACTION_MOVE:
                    touch_move(x, y);
                    invalidate();
                    break;
                case MotionEvent.ACTION_UP:
                    touch_up();
                    invalidate();
                    break;

            }
            return true;
        }

和onDraw:

public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(bgr, 0, 0, null);
        Paint new_paint = new Paint();
        new_paint.setXfermode(new PorterDuffXfermode(Mode.SRC_ATOP));
        new_paint.setStyle(Paint.Style.STROKE );
        new_paint.setFilterBitmap(true);
        canvas.drawBitmap(overlay, 0, 0, new_paint);
        canvas.drawColor(Color.TRANSPARENT);
        canvas.isHardwareAccelerated();
        c2.drawPath(mPath, pTouch);

        }

1 个答案:

答案 0 :(得分:2)

这是硬件加速的问题。尝试在自定义视图的构造函数中执行此操作:

if (android.os.Build.VERSION.SDK_INT >= 11) { setLayerType(View.LAYER_TYPE_SOFTWARE, null); }