将文本附加到以编程方式拍摄的屏幕截图

时间:2015-07-06 19:48:11

标签: android webview

我需要将网页视图的内容保存为图片,以便于分享。

我设法将webview的内容保存为图像(虽然我必须转到横向模式以使图像的高度变小 - 我无法在肖像中获取它!)但我需要附加一些文字和保存的webview截图底部的水印图像。

任何人都可以帮我这样做吗?

我的代码

if (webContentHeight <= 0) webContentHeight = wbContent.getContentHeight();
        Bitmap bitmap = Bitmap.createBitmap(wbContent.getWidth(), (int) (webContentHeight * 1.5), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);



        wbContent.layout(0, 0, wbContent.getLayoutParams().width, wbContent.getLayoutParams().height);
        wbContent.draw(canvas);



        File imgDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "imgDir");
        if (!imgDir.exists()) imgDir.mkdirs();

        File imagePath = new File(imgDir, "Item_" +
                new SimpleDateFormat("ddMMyyyy_hhmmss").format(new Date()) + ".png");

        FileOutputStream fos;
        try {
            fos = new FileOutputStream(imagePath);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();

            startActivity(new Intent(Intent.ACTION_VIEW).setDataAndType(Uri.parse("file://" + imagePath.getAbsolutePath()), "image/*"));

            if (Utils.IsNetworkConnected(getApplicationContext()))
                new AsyncTsk_GrabContent().execute();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            Toast.makeText(context, "Can't save news as image. " + e.getMessage(), Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(context, "Can't save news as image. " + e.getMessage(), Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Toast.makeText(context, "Can't save news as image. " + e.getMessage(), Toast.LENGTH_SHORT).show();
        } finally {
            if (Orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_USER);
            if (pgDialog != null) {
                pgDialog.dismiss();
                pgDialog = null;
            }
        }

我厌倦了但不能满足要求:

 canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    paint.setColor(Color.BLACK);
    paint.setTextSize(16);
    canvas.drawText("My Text", 10, 10, paint);
    wbContent.draw(canvas);

先谢谢。

1 个答案:

答案 0 :(得分:0)

如果要在(10,10)处绘制文本,然后在(0,0)处绘制wbContent,则表示您正在绘制文本。将文本向下移动超过画布高度的2/3(因为您似乎有意将画布设置为wbContent高度的1.5倍),或者只是在绘制wbContent后绘制文本。