删除区域在android中显示为黑色

时间:2014-09-01 14:24:10

标签: android

我正在Android中开发一个应用程序,其中使用移动手指擦除功能。 在我的应用程序擦除功能正常工作,但当我们切换到下一个活动时,擦除区域显示为黑色。请提供任何建议或链接,以提前删除此问题。 我的代码是

class EraseView extends View{
Bitmap bgr,bgr1;
Bitmap overlayDefault;
Bitmap overlay;
Paint pTouch;
int X = -100;
int Y = -100;
Canvas c2;
GlobalVariable globalVariable;
@SuppressLint("NewApi")
public EraseView(Context context,Bitmap bitmap) {
    super(context);
    globalVariable=(GlobalVariable)context.getApplicationContext();
    bgr = BitmapFactory.decodeResource(getResources(),R.drawable.erase_bg);
    bgr1 = BitmapFactory.decodeResource(getResources(),R.drawable.erase_bg);
    overlayDefault =bitmap;
    overlayDefault = Bitmap.createScaledBitmap(overlayDefault,globalVariable.bitmap_width, globalVariable.bitmap_height, true);
    overlay = bitmap.copy(Config.ARGB_8888, true); 
    overlay = Bitmap.createScaledBitmap(overlay, globalVariable.bitmap_width, globalVariable.bitmap_height, true);
    c2 = new Canvas(overlay);
    pTouch = new Paint(Paint.ANTI_ALIAS_FLAG);         
    pTouch.setXfermode(new PorterDuffXfermode(Mode.SRC_OUT)); 
    pTouch.setColor(Color.TRANSPARENT);
    pTouch.setMaskFilter(new BlurMaskFilter(15, Blur.NORMAL));
    globalVariable.bitmapArray=new ArrayList<Bitmap>(); 
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
    switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN: {
            X = (int) ev.getX();
            Y = (int) ev.getY();
            invalidate();
            break;
        }
        case MotionEvent.ACTION_MOVE: {
                X = (int) ev.getX();
                Y = (int) ev.getY();
                invalidate();
                break;
        }           
        case MotionEvent.ACTION_UP:
            globalVariable.count++;
            globalVariable.bitmapArray.add(globalVariable.bitmap);
            break;
    }
    return true;
}
@Override
public void onDraw(Canvas canvas){
    super.onDraw(canvas);
    /*//draw background
    canvas.drawColor(Color.TRANSPARENT);
    canvas.drawBitmap(bgr, 0, 0, null);
    //copy the default overlay into temporary overlay and punch a hole in it                          
    c2.drawBitmap(overlayDefault, 0, 0, null); //exclude this line to show all as you draw
    c2.drawCircle(X, Y, 80, pTouch);
    //draw the overlay over the background  
    canvas.drawBitmap(overlay, 0, 0, null);*/
    canvas.drawColor(Color.TRANSPARENT);
    Paint new_paint = new Paint(/*Paint.ANTI_ALIAS_FLAG*/);
    new_paint.setXfermode(new PorterDuffXfermode(Mode.SRC_ATOP));
    canvas.drawBitmap(bgr, 0, 0, new_paint);
    c2.drawCircle(X, Y, 25, pTouch);
    canvas.drawBitmap(overlay, 0, 0, new_paint);
    Bitmap tempBitmap = Bitmap.createBitmap(globalVariable.bitmap_width,globalVariable.bitmap_height, Bitmap.Config.RGB_565);
   Canvas can=new Canvas(tempBitmap);
   can.drawBitmap(bgr, 0, 0, null);
   can.drawBitmap(overlay, 0, 0, null);
    globalVariable.bitmap=tempBitmap;
}
}

1 个答案:

答案 0 :(得分:0)

在上面的问题之后,擦除图像擦除区域在下一个活动中显示为黑色。 为此,您可以通过以下代码替换此代码

Bitmap tempBitmap = Bitmap.createBitmap(globalVariable.bitmap_width,globalVariable.bitmap_height,       Bitmap.Config.RGB_565);

在这个如果你设置这个代码然后你发现擦除区域透明..

Bitmap tempBitmap = Bitmap.createBitmap(globalVariable.bitmap_width,globalVariable.bitmap_height,       Bitmap.Config.ARGB_8888);