2013-09-26 39 views
0

我想擦除圖像的一部分,然後設置XferMode來清除圖像。但是當我在android> 3.0上測試它工作正常,它在android < 3.0(2.2)上繪製黑線。我無法找到解決這個問題的方法。任何人都能解釋我爲什麼? 這是TouchView中方法:Paint Xfermode在andorid 2.2上再次繪製黑色線---->再次

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); 

     } 

+0

沒人能幫我嗎? :'( –

回答

2

這是硬件加速的問題。試着這樣做在你的自定義視圖的構造函數:

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

+1

實際上工作!這應該有更多的投票! –