【发布时间】:2014-07-22 03:18:43
【问题描述】:
主要问题是将整个滚动视图保存为位图图像,而不仅仅是屏幕上显示的内容。有没有办法保存整个滚动视图,如果有,怎么办?
【问题讨论】:
-
@Govind Rathod 这不起作用它只提供屏幕截图而不是整个滚动视图
标签: android bitmap scrollview screen-capture
主要问题是将整个滚动视图保存为位图图像,而不仅仅是屏幕上显示的内容。有没有办法保存整个滚动视图,如果有,怎么办?
【问题讨论】:
标签: android bitmap scrollview screen-capture
在 ScrollView 中创建一个 RelativeLayout 或 LinearLayout 以从 Layout 中获取 Bitmap。
这样组织:
public class ActivityA extends Activity {
LinearLayout PP_Ll;
Button btn_capture;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
PP_Ll = (RelativeLayout) findViewById(R.id.PP_Ll);
Button btn_capture= (Button) findViewById(R.id.btn_capture);
btn_capture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bitmap bitmap = takeScreenshot();
saveBitmap(bitmap);
}
});
public Bitmap takeScreenshot() {
View rootView = getWindow().getDecorView().findViewById(R.id.PP_Ll);
rootView.setDrawingCacheEnabled(true);
return rootView.getDrawingCache();
}
public void saveBitmap(Bitmap bitmap) {
String root = Environment.getExternalStorageDirectory().toString();
File newDir = new File(root + "/Folder");
newDir.mkdirs();
Random gen = new Random();
int n = 10000;
n = gen.nextInt(n);
String fotoname = "Photo-" + n + ".jpg";
File file = new File(newDir, fotoname);
if (file.exists())
file.delete();
try {
FileOutputStream fos = new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
Toast.makeText(getApplicationContext(),
"Saved in folder: 'Folder'", Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
}
}}
您的 RelativeLayout 将作为位图保存在某个目录中。祝你好运。
注意:您必须将您的 imageviews、textviews 等插入到 .xml 文件中的 RelativeLayout 中,所有这些都可以进入屏幕截图。
【讨论】:
试试下面的代码:-
public static Bitmap loadBitmapFromView(View v, int width, int height) {
Bitmap b = Bitmap.createBitmap(width , height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
v.draw(c);
return b;
}
请看下面的链接
【讨论】:
可能的解决方案,您可以创建一个更大屏幕的模拟器,尺寸可能为 10 英寸,并在其上运行您的应用程序,如果您的列表视图适合屏幕,您可以对其进行屏幕截图。
【讨论】: