Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我可以擦除从SD卡加载到画布或DrawView的部分或全部图像吗_Java_Android_Bitmap_Android Canvas - Fatal编程技术网

Java 我可以擦除从SD卡加载到画布或DrawView的部分或全部图像吗

Java 我可以擦除从SD卡加载到画布或DrawView的部分或全部图像吗,java,android,bitmap,android-canvas,Java,Android,Bitmap,Android Canvas,我已成功将图像从SD卡移到画布,但我想擦除加载图像的某些部分。。。。建议我合适的代码这是我的主要活动,我可以在画布上绘制保存内容,我可以删除绘制的内容,但我必须删除加载的图像,请建议我 //custom drawing view private DrawingView drawView; //buttons private ImageButton currPaint, drawBtn, eraseBtn, newBtn, saveBtn,loadBtn; //sizes private floa

我已成功将图像从SD卡移到画布,但我想擦除加载图像的某些部分。。。。建议我合适的代码这是我的主要活动,我可以在画布上绘制保存内容,我可以删除绘制的内容,但我必须删除加载的图像,请建议我

//custom drawing view
private DrawingView drawView;
//buttons
private ImageButton currPaint, drawBtn, eraseBtn, newBtn, saveBtn,loadBtn;
//sizes
private float Brush;
Bitmap bitmap;

int mWidth;
int mHeight;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   mWidth=mHeight=0;
    //get drawing view
    drawView = (DrawingView)findViewById(R.id.drawing);

    //get the palette and first color button
    LinearLayout paintLayout = (LinearLayout)findViewById(R.id.paint_colors);
    currPaint = (ImageButton)paintLayout.getChildAt(0);
    currPaint.setImageDrawable(getResources().getDrawable(R.drawable.paint_pressed));

    //sizes from dimensions
    Brush = getResources().getInteger(R.integer.small_size);

    //draw button
    drawBtn = (ImageButton)findViewById(R.id.draw_btn);
    drawBtn.setOnClickListener(this);

    //set initial size
    drawView.setBrushSize(Brush);

    //erase button
    eraseBtn = (ImageButton)findViewById(R.id.erase_btn);
    eraseBtn.setOnClickListener(this);

    //new button
    newBtn = (ImageButton)findViewById(R.id.new_btn);
    newBtn.setOnClickListener(this);

    //save button
    saveBtn = (ImageButton)findViewById(R.id.save_btn);
    saveBtn.setOnClickListener(this);
    //load button
    loadBtn = (ImageButton)findViewById(R.id.load_btn);
    loadBtn.setOnClickListener(this);

    }

private Bitmap getBitmapFromUri(Uri data){
    Bitmap bitmap = null;

      // Starting fetch image from file
      InputStream is=null;          
      try {

        is = getContentResolver().openInputStream(data);

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;

        // BitmapFactory.decodeFile(path, options);
        BitmapFactory.decodeStream(is, null, options);          

        // Calculate inSampleSize
    //    options.inSampleSize = calculateInSampleSize(options, mWidth, mHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;         

        is = getContentResolver().openInputStream(data);

        bitmap = BitmapFactory.decodeStream(is,null,options);


        if(bitmap==null){
            Toast.makeText(getBaseContext(), "Image is not Loaded",Toast.LENGTH_SHORT).show();
            return null;
        }

        is.close();
      }catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }catch(NullPointerException e){
        e.printStackTrace();
    }
      return bitmap;
}



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, intent);
    if (requestCode == 10 && resultCode == RESULT_OK && null != intent) {
          Uri data = intent.getData();
          Bitmap bitmap = getBitmapFromUri(data);
          if(bitmap!=null){
              drawView.addBitmap(bitmap);   
          }
      }     
}   


@Override
public void onWindowFocusChanged(boolean hasFocus) {
    // TODO Auto-generated method stub
    super.onWindowFocusChanged(hasFocus);
    mWidth = drawView.getWidth();
    mHeight = drawView.getHeight();
}

@Override
protected void onSaveInstanceState(Bundle outState) {

    outState.putInt("width", mWidth);
    outState.putInt("height", mHeight);
    if(drawView.getBitmap()!=null){
        outState.putParcelable("bitmap", drawView.getBitmap());
    }

    super.onSaveInstanceState(outState);

}


public void paintClicked(View view){
    //use chosen color

    //set erase false
    drawView.setErase(false);
    drawView.setPaintAlpha(100);
    drawView.setBrushSize(Brush);

    if(view!=currPaint){
        ImageButton imgView = (ImageButton)view;
        String color = view.getTag().toString();
        drawView.setColor(color);
        //update ui
        imgView.setImageDrawable(getResources().getDrawable(R.drawable.paint_pressed));
        currPaint.setImageDrawable(getResources().getDrawable(R.drawable.paint));
        currPaint=(ImageButton)view;
    }
}

@Override
public void onClick(View view){

    if(view.getId()==R.id.draw_btn){
                drawView.setErase(false);
                drawView.setBrushSize(Brush);
                drawView.setLastBrushSize(Brush);
    }
    else if(view.getId()==R.id.erase_btn){
        //switch to erase - choose size
        final Dialog brushDialog = new Dialog(this);


                drawView.setErase(true);
                drawView.setBrushSize(getResources().getInteger(R.integer.medium_size));
                brushDialog.dismiss();

    }
      if(view.getId()==R.id.new_btn){
        //new button
        AlertDialog.Builder newDialog = new AlertDialog.Builder(this);
        newDialog.setTitle("Clear Drawing");
        newDialog.setMessage("Are You Sure To Clear (you will lose the current drawing)?");
        newDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
            @SuppressLint("NewApi") public void onClick(DialogInterface dialog, int which){
                drawView.deleteBitmap();
                drawView.startNew();

                dialog.dismiss();
            }
        });
        newDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int which){
                dialog.cancel();
            }
        });
        newDialog.show();
    }
    else if(view.getId()==R.id.save_btn){
        //save drawing
        AlertDialog.Builder saveDialog = new AlertDialog.Builder(this);
        saveDialog.setTitle("Save drawing");
        saveDialog.setMessage("Save drawing to device Gallery?");
        saveDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int which){
                //save drawing
                drawView.setDrawingCacheEnabled(true);
                //attempt to save
                String imgSaved = MediaStore.Images.Media.insertImage(
                        getContentResolver(), drawView.getDrawingCache(),
                        UUID.randomUUID().toString()+".png", "drawing");
                //feedback
                if(imgSaved!=null){
                    Toast savedToast = Toast.makeText(getApplicationContext(), 
                            "Drawing saved to Gallery!", Toast.LENGTH_SHORT);
                    savedToast.show();
                }
                else{
                    Toast unsavedToast = Toast.makeText(getApplicationContext(), 
                            "Oops! Image could not be saved.", Toast.LENGTH_SHORT);
                    unsavedToast.show();
                }
                drawView.destroyDrawingCache();
            }
        });
        saveDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int which){
                dialog.cancel();
            }
        });
        saveDialog.show();
    }
    else if (view.getId()==R.id.load_btn){
            Intent i = new Intent();

            i.setType("image/*");
            i.setAction(Intent.ACTION_GET_CONTENT);
            Intent customChooserIntent = Intent.createChooser(i, "Pick an image");
            startActivityForResult(customChooserIntent, 10);                
        }


    }


}