【问题标题】:How to crop the screenshot in Android如何在 Android 中裁剪屏幕截图
【发布时间】:2012-08-11 17:52:32
【问题描述】:

我正在尝试在我的应用中画线。完成后,我想将屏幕另存为图像。但我不希望按钮出现在图像中。我想裁剪它然后保存它。我尝试使用此代码在 onclicklistener 中裁剪我的位图。但它没有用。 Bitmap.createBitmap(位图, 5, 5, 5, 5); 这是我的所有代码:

    content.setDrawingCacheEnabled(true); 
    content.buildDrawingCache(); 
    Bitmap bitmap = Bitmap.createBitmap(content.getDrawingCache()); // Bitmap
    Bitmap.createBitmap(bitmap, 5, 5, 5, 5);
    content.setDrawingCacheEnabled(false);

    File file = new File("/sdcard/SurucuImza.jpg");
    FileOutputStream ostream = null;



    try {
        if (bitmap != null) {
            file.createNewFile();
            ostream = new FileOutputStream(file);
            bitmap.compress(CompressFormat.JPEG, 100, ostream); 
            ostream.flush();
            ostream.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    bitmap.recycle();
    bitmap = null;
    ostream = null;
    content.setDrawingCacheEnabled(false);

}

【问题讨论】:

标签: android


【解决方案1】:

由于 Android 屏幕尺寸和密度的大小可变,我建议您不要使用 content,但在您的 XML 布局中,您有一个“可打印”的 ViewGroup 和其他区域(在视图组)与按钮。

这样您就必须执行相同的操作,但使用 viewGroup 而不是 content

【讨论】:

    【解决方案2】:

    您应该将此行的结果分配给一个变量:

    Bitmap.createBitmap(bitmap, 5, 5, 5, 5);
    
    //Ex:
    Bitmap croppedBitmap = Bitmap.createBitmap(bitmap, 5, 5, 5, 5);
    

    然后保存croppedBitmap 而不是bitmap.

    还有一件事,你确定你的按钮是 5x5 像素吗? createBitmap()的最后两个参数指定widthheight

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多