我有一个应用程序,我通过画布绘制对象。我想像图片一样保存这些图片。例如 bmp 或 jpg 到文件夹。我该怎么做 ?这是我的代码。
public class hell extends View{
public static int width= aktivita.width;
public static int koeficient = 5;
final Paint mPaint;
public static int t;
public boolean filter = true;
static ArrayList<Circle> mCircles;
private static boolean Kontroler = true;
public void draw(Canvas canvas) {
Paint p = new Paint();
p.setColor(Color.RED);
kres(canvas);
invalidate();
}
}
public krouzky(Context context, AttributeSet atrs) {
super(context, atrs);
mMalovani = new Paint();
mMalovani.setColor(Color.RED);
mMalovani.setAntiAlias(true);
createCircles();
}
添加绘图对象
public static void Prid() {
int ran = aktivita.width/8;
mCircles.add(new Circle(80, 200, ran));
}
制作数组列表
private static void createCircles() { if (mCircles == null) { mCircles = new ArrayList<Circle>(); }
int r = aktivita.width/8;
mCircles.add(new Circle(80, 200, r));
}
绘图对象 private void kres(Canvas canvas) {
for (Circle c : mCircles) {
//
canvas.drawCircle(c.getCurrentX(), c.getCurrentY(), c.getRadius(),
mPaint);
}
}
我试过这个,但在按钮开始后点击 loadBitmapFromView(v); 节省(); 我的程序关闭了,并且在 SD 卡中是带有 .png 后缀的文件。但它有 0 口,我没有打开它。
public static Bitmap loadBitmapFromView(View view) {
// width measure spec
int widthSpec = View.MeasureSpec.makeMeasureSpec(
view.getMeasuredWidth(), View.MeasureSpec.AT_MOST);
// height measure spec
int heightSpec = View.MeasureSpec.makeMeasureSpec(
view.getMeasuredHeight(), View.MeasureSpec.AT_MOST);
// measure the view
view.measure(widthSpec, heightSpec);
// set the layout sizes
view.layout(view.getLeft(), view.getTop(), view.getMeasuredWidth() + view.getLeft(), view.getMeasuredHeight() + view.getTop());
// create the bitmap
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
// create a canvas used to get the view's image and draw it on the bitmap
Canvas c = new Canvas(bitmap);
// position the image inside the canvas
c.translate(-view.getScrollX(), -view.getScrollY());
// get the canvas
view.draw(c);
return bitmap;
}
public void save(){
String fileName = String.valueOf(Calendar.getInstance().getTimeInMillis());
// generate the image path
String imagePath = Environment.getExternalStorageDirectory().toString() + File.separator + fileName + ".png";
try {
// save the image as png
FileOutputStream out = new FileOutputStream(imagePath);
View view = null;
// compress the image to png and pass it to the output stream
loadBitmapFromView(view).compress(Bitmap.CompressFormat.PNG, 90, out);
// save the image
out.flush();
out.close();
} catch (Exception error) {
Log.e("Error saving image", error.getMessage());
}
}