Как стереть последнее размещенное растровое изображение с холста с несколькими растровыми изображениями на нем

#android

#Android

Вопрос:

У меня есть код, в котором я размещаю изображение на холсте и пытаюсь нарисовать его с помощью Fingerpaint. Но пока я использую ластик для удаления последних нарисованных линий или что-то еще с помощью Fingerpaint, оно также стирает фоновое изображение…Я копирую приведенный ниже код.Если у вас есть какое-либо решение, пожалуйста, дайте мне знать.

  @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();
    }

 }
  

И часть меню, которая включает опцию ластика, является….

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

Заранее спасибо

Arun

Ответ №1:

если вы просто пытаетесь очистить нарисованный вами путь, просто вызовите

 mPath.reset();
invalidate();
  

Ответ №2:

установите background для вашего вида рисования и

в canvas.drawColor(0x000FFF);

заменить на canvas.drawColor(0x00000000);

Ответ №3:

Попробуйте поместить изображение в другой макет. Я работаю над аналогичным проектом, и я столкнулся с той же проблемой. Я использовал следующий вид:

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

Затем в моем приложении я поместил фоновое изображение в ImageView и записываю в FrameLayout. Таким образом, вы никогда не измените исходное изображение.

Надеюсь, это поможет.