主要问题是将整个滚动视图保存为位图图像,而不仅仅是屏幕上显示的图像。有没有办法保存整个滚动视图,如果可以的话如何保存?
在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 将作为BitMap 保存在某个目录中。祝你好运。
Note:您必须将图像视图、文本视图等插入到 .xml 文件中的相对布局中,所有这些都可以进入屏幕截图。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)