我正在创建一个具有以下目的的 android 应用程序:
- 将画布另存为 SD 卡上的图像
- 即使在我清洁后也始终保留第一张图片(使用 ClearPaint 按钮)
- 绘制新图片将再次保留以前的图像
代码:
Button Colorpaint = (Button) findViewById(R.id.color);
Colorpaint.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int _color = R.color.red;
new PickerDialog(v.getContext(),new OnColorChangedListener() {
public void colorChanged(int color) {
mPaint.setColor(color);
Log.i("TAG", "mpaint one" +mPaint);
}
}, mPaint.getColor(), _color).show();
Log.i("TAG", "mpaint two" +mPaint);
}
});
ClearPaint = (Button) findViewById(R.id.ClearPaint);
ClearPaint.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mBitmap.eraseColor(Color.TRANSPARENT);
mPath.reset();
mView.invalidate();
}
});
btn_shoot = (Button)findViewById(R.id.btn_shoot);
btn_shoot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View view = findViewById(R.id.item);
view.setDrawingCacheEnabled(true);
Bitmap bitmap = view.getDrawingCache();
BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
//we check if external storage is available, otherwise display an error message to the user
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File (sdCard.getAbsolutePath() + "/Basketball_Coach_Board");
directory.mkdirs();
String filename = "tactics" + i + ".jpg";
File yourFile = new File(directory, filename);
while (yourFile.exists()) {
i++;
filename = "tactics" + i + ".jpg";
yourFile = new File(directory, filename);
}
if (!yourFile.exists()) {
if (directory.canWrite())
{
try {
FileOutputStream out = new FileOutputStream(yourFile, true);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
Toast.makeText(MainActivity.this, "Tactics saved at /sdcard/Basketball_Coach_Board/tactics" + i + ".jpg", Toast.LENGTH_SHORT).show();
i++;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
else
{
Toast.makeText(MainActivity.this, "SD Card not available!", Toast.LENGTH_SHORT).show();
}
}
});