我想要删除我的位图,但我不想删除背景图像。当我尝试擦除时是白色并且它在帧中画得很难。
这是我的MainActivity代码
private final String tag = "MainActivity";
private ImageView eraser;
private Button btnChooseImage;
private ImageButton btnClear, btnSave, btnShare, btnCamera;
private DrawingView drawingView;
private static final int SELECT_PHOTO = 100;
private static final int CAMERA_REQUEST = 1888;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
setContentView(R.layout.activity_main);
drawingView = (DrawingView) findViewById(R.id.drawing);
btnChooseImage = (Button) findViewById(R.id.btnChooseImage);
btnChooseImage.setOnClickListener(this);
btnClear = (ImageButton) findViewById(R.id.btnClear);
btnClear.setOnClickListener(this);
btnSave = (ImageButton) findViewById(R.id.btnSave);
btnSave.setOnClickListener(this);
btnShare = (ImageButton) findViewById(R.id.btnShare);
btnShare.setOnClickListener(this);
btnCamera = (ImageButton) findViewById(R.id.btnCamera);
btnCamera.setOnClickListener(this);
eraser = (ImageView) findViewById(R.id.eraser);
eraser.setOnClickListener(this);
}
}
@SuppressLint("NewApi") @Override
public void onClick(View v) {
if (v == eraser) {
if (drawingView.isEraserActive()) {
drawingView.deactivateEraser();
eraser.setImageResource(R.drawable.eraser);
} else {
drawingView.activateEraser();
eraser.setImageResource(R.drawable.pencil);
}
} else if (v == btnClear) {
drawingView.reset();
drawingView.setBackground(null);
} else if (v == btnSave) {
saveImage();
} else if (v == btnCamera) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
} else if (v == btnShare) {
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/png");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse(saveImage().getAbsolutePath())); //"file:///sdcard/temporary_file.jpg"
startActivity(Intent.createChooser(share, "Share Image"));
} else if (v == btnChooseImage) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
}
}
public File saveImage() {
drawingView.setDrawingCacheEnabled(true);
Bitmap bm = drawingView.getDrawingCache();
File fPath = Environment.getExternalStorageDirectory();
File f = null;
f = new File(fPath, UUID.randomUUID().toString() + ".png");
try {
FileOutputStream strm = new FileOutputStream(f);
bm.compress(Bitmap.CompressFormat.PNG, 80, strm);
strm.close();
Toast.makeText(getApplicationContext(), "Image is saved successfully.", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
return f;
}
@SuppressLint("NewApi") @SuppressWarnings("deprecation")
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case SELECT_PHOTO:
if (resultCode == RESULT_OK) {
Uri selectedImage = imageReturnedIntent.getData();
InputStream imageStream = null;
try {
imageStream = getContentResolver().openInputStream(selectedImage);
Bitmap bitmap = BitmapFactory.decodeStream(imageStream);
BitmapDrawable ob = new BitmapDrawable(getResources(), bitmap);
if(Build.VERSION.SDK_INT >= 16)
{
drawingView.setBackground(ob);
}else {
drawingView.setBackgroundDrawable(ob);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
break;
case CAMERA_REQUEST:
if (resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) imageReturnedIntent.getExtras().get("data");
BitmapDrawable ob = new BitmapDrawable(getResources(), photo);
if(Build.VERSION.SDK_INT >= 16)
{
drawingView.setBackground(ob);
}else {
drawingView.setBackgroundDrawable(ob);
}
}
}
}
}
这是DrawingView代码
private Canvas m_Canvas;
private Path m_Path;
private Paint m_Paint;
private ArrayList<Pair<Path, Paint>> paths = new ArrayList<Pair<Path, Paint>>();
private ArrayList<Pair<Path, Paint>> undonePaths = new ArrayList<Pair<Path, Paint>>();
private float mX, mY;
private static final float TOUCH_TOLERANCE = 0.001f;
private boolean isEraserActive = false;
public DrawingView(Context context, AttributeSet attr) {
super(context, attr);
setFocusable(true);
setFocusableInTouchMode(true);
setBackgroundColor(Color.WHITE);
this.setOnTouchListener(this);
onCanvasInitialization();
}
public void onCanvasInitialization() {
m_Paint = new Paint();
m_Paint.setAntiAlias(true);
m_Paint.setDither(true);
m_Paint.setColor(Color.parseColor("#000000"));
m_Paint.setStyle(Paint.Style.STROKE);
m_Paint.setStrokeJoin(Paint.Join.ROUND);
m_Paint.setStrokeCap(Paint.Cap.ROUND);
m_Paint.setStrokeWidth(2);
m_Canvas = new Canvas();
m_Path = new Path();
Paint newPaint = new Paint(m_Paint);
paths.add(new Pair<Path, Paint>(m_Path, newPaint));
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}
public boolean onTouch(View arg0, 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;
}
@Override
protected void onDraw(Canvas canvas) {
for (Pair<Path, Paint> p : paths) {
canvas.drawPath(p.first, p.second);
}
}
private void touch_start(float x, float y) {
if (isEraserActive) {
m_Paint.setColor(Color.WHITE);
m_Paint.setStrokeWidth(10);
Paint newPaint = new Paint(m_Paint); // Clones the mPaint object
paths.add(new Pair<Path, Paint>(m_Path, newPaint));
} else {
m_Paint.setColor(Color.BLACK);
m_Paint.setStrokeWidth(2);
Paint newPaint = new Paint(m_Paint); // Clones the mPaint object
paths.add(new Pair<Path, Paint>(m_Path, newPaint));
}
m_Path.reset();
m_Path.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) {
m_Path.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
mX = x;
mY = y;
}
}
private void touch_up() {
m_Path.lineTo(mX, mY);
// commit the path to our offscreen
m_Canvas.drawPath(m_Path, m_Paint);
// kill this so we don't double draw
m_Path = new Path();
Paint newPaint = new Paint(m_Paint); // Clones the mPaint object
paths.add(new Pair<Path, Paint>(m_Path, newPaint));
}
public void activateEraser()
{
isEraserActive = true;
}
public void deactivateEraser()
{
isEraserActive = false;
}
public boolean isEraserActive()
{
return isEraserActive;
}
public void reset()
{
paths.clear();
invalidate();
}
}
我想删除图纸视图而不更改从图库
导入的背景图像答案 0 :(得分:0)
在View类中创建一个函数并清除之前保存的所有列表视图,并重新绘制视图,如:
public void clearDrawing()
{
paths.clear();
invalidate();
}
invalidate()whill刷新视图。在活动中的click事件中从视图对象中调用此方法。希望它会有所帮助。