开发者

How to erase the last placed bitmap from a canvas with multiple bitmaps on it

I have a code in which,I am placing an Image on the canvas and tries to draw on it with Fingerpaint. But while I am using the Eraser to erase the last drawn lines or something with Fingerpaint, It is erasing the background image also...I am copying the code below.If you have any solution,please let me know.

 @Override
 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
     super.onSizeChanged(w, h, oldw, oldh);
     String pathName = Environment.getExternalStorageDirectory().toString()+"/test.jpg";
     Bitmap image = LoadBMPsdcard(pathName);
    // 开发者_如何转开发Bitmap image = BitmapFactory.decodeResource(getResources(),R.drawable.hp).copy(Bitmap.Config.ARGB_8888, true);
     mBitmap = Bitmap.createScaledBitmap(image, sketchBoard.getWidth(), sketchBoard.getHeight(), true);
     //mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
     mCanvas = new Canvas(mBitmap);
 }

 private Bitmap LoadBMPsdcard(String path)  
    {  
        //creates a 'File' object, to check if the image exists (Thanks to Jbeerdev for the tip)  
        File imageFile = new File(path);  

        //if the file exists  
        if(imageFile.exists())  
        {  
            //load the bitmap from the given path  
            return BitmapFactory.decodeFile(path).copy(Bitmap.Config.ARGB_8888, true);    
        }  
        else  
        {  
            //return the standard 'Image not found' bitmap placed on the res folder  
            return BitmapFactory.decodeResource(getResources(), R.drawable.imagenotfound).copy(Bitmap.Config.ARGB_8888, true);   
        }  
    }  

 @Override
 protected void onDraw(Canvas canvas) {



    try{


     canvas.drawColor(0x000FFF);
    //canvas.drawBitmap(mBitmap, srcRect, dstRect,mBitmapPaint);
     canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
     canvas.drawPath(mPath, mPaint);

    }catch(Exception e){
        e.printStackTrace();
    }

 }

And the menu part that includes the eraser option is....

public boolean onOptionsItemSelected(MenuItem item) {
 mPaint.setXfermode(null);
 mPaint.setAlpha(0xFF);

 switch (item.getItemId()) {

     case R.id.newf:

        sketchBoard.removeAllViews();
            view.invalidate();
        view= new MyView(this);
        setContentView(view);

        return true;

     case R.id.red:

     // mPaint.reset();
        mPaint.setColor(0xFF3300);
        mPaint.setAntiAlias(true);
         //view.invalidate();
         return true;

     case R.id.white:

     // mPaint.reset();
        mPaint.setColor(0xFFFFFF);
        mPaint.setAntiAlias(true);
        // view.invalidate();
         return true;

     case R.id.blue:
        //mPaint.reset();
        mPaint.setColor(0x0033FF);
        // view.invalidate();
         return true;

     case R.id.save:

         cReatePNG();

         return true;

     case R.id.eraser:

         mPaint.setXfermode(new PorterDuffXfermode(
                  PorterDuff.Mode.CLEAR));

        return true;


     case R.id.reset:

        sketchBoard.removeAllViews();
            view.invalidate();
        view= new MyView(this);
        setContentView(view);
        return true;

     case R.id.exit:

        finish();






 }
 return super.onOptionsItemSelected(item);
}

Thanks in Advance

Arun


if you are just trying to clear the path that you have drawn then just call

mPath.reset();
invalidate();


setbackground to your draw view and

at canvas.drawColor(0x000FFF);

replace with canvas.drawColor(0x00000000);


Try placing the picture on a different layout. I am working on a similar project and I ran into the same issue. I used the following view:

<FrameLayout
        android:layout_width="fill_parent"
        android:id="@+id/get_user_fingerpaint"
        android:layout_height="fill_parent">
        <ImageView
            android:id="@+id/main_image"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
</FrameLayout>

Then in my application I placed the background image on the ImageView and I write on the FrameLayout. This way you never alter your original image.

Hope it helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜