Android - 获取滚动视图可见区域的屏幕截图
Android - Get a ScreenShot of the visible area of a Scrollview
如何在 Android 中获取 ScrollView 可见区域的屏幕截图?
我的视图层次结构如下:
Scrollview - 线性布局 - Imageview
这是我在我的实用程序 Class 中使用的代码:
@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;
}
如何在 Android 中获取 ScrollView 可见区域的屏幕截图?
我的视图层次结构如下:
Scrollview - 线性布局 - Imageview
这是我在我的实用程序 Class 中使用的代码:
@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;
}