android canvas 绘制bitmap并保存到本地

自定义一个view,重载draw方法(不是重载ondraw,不然图片保存下来是空白的。)在activity里面findviewbyid找到view,用view调用draw方法。

在activity的oncreate里面:

if (mBackgroundBitmap == null) {
            mBackgroundBitmap = Bitmap.createBitmap(screenWidth, screenHeight, Bitmap.Config.RGB_565);
            canvas = new Canvas(mBackgroundBitmap);
        }

        myView.draw(canvas);

自定义的myView(extend View)里面,

@Override
    public void draw(Canvas canvas) {
//canvas.drawLine().....
}

保存到本地:

activity里面一些方法,

public Bitmap getBitmap() {
        
        Bitmap whiteBgBitmap = Bitmap.createBitmap(mBackgroundBitmap.getWidth(), mBackgroundBitmap.getHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(whiteBgBitmap);
        canvas.drawColor(Color.WHITE);
        canvas.drawBitmap(mBackgroundBitmap, 0, 0, null);
        return whiteBgBitmap;
    }
public boolean addGraphToGallery(Bitmap bmp) {
        boolean result = false;
        try {
            File photo = new File(getAlbumStorageDir("cece"), String.format("cece_bitmap_%d.jpg",
                    System.currentTimeMillis()));
            saveBitmapToJPG(bmp, photo);
            Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            Uri contentUri = Uri.fromFile(photo);
            mediaScanIntent.setData(contentUri);
            SpiderNetActivity.this.sendBroadcast(mediaScanIntent);
            result = true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
public void saveBitmapToJPG(Bitmap bitmap, File photo) throws IOException {
        Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(newBitmap);
        canvas.drawColor(Color.WHITE);
        canvas.drawBitmap(bitmap, 0, 0, null);
        OutputStream stream = new FileOutputStream(photo);
        newBitmap.compress(Bitmap.CompressFormat.JPEG, 80, stream);
        stream.close();
    }
public File getAlbumStorageDir(String albumName) {
        // Get the directory for the user's public pictures directory.
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), albumName);
        if (!file.mkdirs()) {
            Log.e("SignaturePad", "Directory not created");
        }
        return file;
    }
开始保存:

Bitmap spiderNetBitmap = getBitmap();
if (addGraphToGallery(spiderNetBitmap)) {
                    Toast.makeText(SpiderNetActivity.this, "Spider graph saved into the Gallery", Toast.LENGTH_SHORT)
                            .show();
                } else {
                    Toast.makeText(SpiderNetActivity.this, "Unable to store Spider graph", Toast.LENGTH_SHORT).show();
                }
不要忘记在manifest里面添加存储权限:


bitmap转byte[] 方法:

public byte[] Bitmap2Bytes(Bitmap bm) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    }







你可能感兴趣的:(android)