【问题标题】:Android - Get a ScreenShot of the visible area of a ScrollviewAndroid - 获取滚动视图可见区域的屏幕截图
【发布时间】:2015-04-29 09:31:35
【问题描述】:

如何在 Android 中获取 ScrollView 可见区域的屏幕截图?

我的视图层次结构如下:
Scrollview - 线性布局 - Imageview

【问题讨论】:

  • 您想要只包含可见区域还是完整的滚动视图包含孩子的?
  • 是的,我的滚动视图中有一个可滚动的图像视图,我只想要该图像视图的一部分可见区域意味着滚动视图

标签: android imageview scrollview


【解决方案1】:

这是我在我的实用程序类中使用的代码:

@SuppressLint("SimpleDateFormat")
protected final static boolean shoot
(final Context ctx, final View v, final String appName)
{
    boolean isOK = false;

    // Get the bitmap from the view
    v.setDrawingCacheEnabled(true);
    final Bitmap bmp = v.getDrawingCache();

    final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd@HHmmss");
    final Calendar cal = Calendar.getInstance();

    // Set file properties
    fileJPG = appName + "_" + sdf.format(cal.getTime());

    /*
    Create a path where we will place our picture in the user's public
    pictures directory. Note that you should be careful about what you
    place here, since the user often manages these files.
    For pictures and other media owned by the application, consider
    Context.getExternalMediaDir().
    */
    final File path =
        Environment.getExternalStoragePublicDirectory
        (
            //Environment.DIRECTORY_PICTURES
            //Environment.DIRECTORY_DCIM
            Environment.DIRECTORY_DCIM + "/yourAppName/"
        );

    // Make sure the Pictures directory exists.
    if(!path.exists())
    {
        path.mkdirs();
    }

    final File file = new File(path, fileJPG + ".jpg");

    try
    {
        final FileOutputStream fos = new FileOutputStream(file);
        final BufferedOutputStream bos = new BufferedOutputStream(fos, 8192);

        bmp.compress(CompressFormat.JPEG, 85, bos);

        bos.flush();
        bos.close();

        fileJPG = file.getPath();
        isOK = true;
    }
    catch (final IOException e)
    {
        e.printStackTrace();
    }
    return isOK;
}

【讨论】:

  • 是的,我知道。实际上,我使用它。我希望代码足够清晰——我尽量让我的代码得到很好的注释,以备将来重复使用。
猜你喜欢
  • 2021-07-12
  • 1970-01-01
  • 1970-01-01
  • 2012-02-22
  • 1970-01-01
  • 2015-03-23
  • 2019-07-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多