我要在Canvas位图上添加一个点。 在ImageView上启用了缩放,旋转和移动手势。之后,我通过单击按钮将图像保存在外部存储器中。但是位图被裁剪了(位图的大小与我的自定义imageView相同)。我使用imgView.getDrawingCache()将点保存在画布上。 我不想裁剪保存的位图。
Save Function -
private void saveFunction() {
iv.setDrawingCacheEnabled(true);
Bitmap bitmap = iv.getDrawingCache(true);
/* BitmapDrawable drawable = (BitmapDrawable) iv.getDrawable();
Bitmap newBitmap = drawable.getBitmap();*/
String path =
Environment.getExternalStorageDirectory().getAbsolutePath();
File file = new File(path + File.separator + "name" + ".png");
Toast.makeText(getApplicationContext(), file.getAbsolutePath(),
Toast.LENGTH_LONG).show();
try {
if (!file.exists())
{
file.createNewFile();
}
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 10, ostream);
ostream.close();
System.out.println("Rushi : saveFunction : saved");
} catch (Exception e) {
e.printStackTrace();
}
}
我的自定义ImageView类- 公开课
DrawImageView extends ImageView {
private Paint currentPaint;
public boolean drawRect = false;
public float x;
public float y;
public Bitmap newBitmap;
public Matrix matrix;
public DrawImageView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
currentPaint = new Paint();
currentPaint.setDither(true);
currentPaint.setColor(0xFF00CC00);
currentPaint.setStyle(Paint.Style.STROKE);
currentPaint.setStrokeJoin(Paint.Join.ROUND);
currentPaint.setStrokeCap(Paint.Cap.ROUND);
currentPaint.setStrokeWidth(60);
}
public void setNewBitmap(Bitmap bmp) {
newBitmap = bmp;
}
public Bitmap getNewBitmap() {
return newBitmap;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (drawRect) {
canvas.drawPoint(x, y, currentPaint);
/* Matrix matrix = this.getImageMatrix();
float[] pts = {0, 0};
matrix.mapPoints(pts);
System.out.println("Rushi : NewPoint : "+pts[0]+","+pts[1]);*/
// canvas.drawBitmap(newBitmap, getImageMatrix(), null);
}
}
}
因此,基本上,单击按钮时我要绘制一个点,然后保存图像。