我有一个View
我希望将其转换为图像以便将其存储在某个地方。
但是,如何将此View
转换为图像?
答案 0 :(得分:9)
尝试使用此视图拍摄视图并存储在SD卡中..
View view = TextView.getRootView();
//You can use any view of your View instead of TextView
if (view != null)
{
System.out.println("view is not null.....");
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bm = view.getDrawingCache();
try
{
if (bm != null)
{
String dir = Environment.getExternalStorageDirectory().toString();
System.out.println("bm is not null.....");
OutputStream fos = null;
File file = new File(dir,"sample.JPEG");
fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bm.compress(Bitmap.CompressFormat.JPEG, 50, bos);
bos.flush();
bos.close();
}
}
catch(Exception e)
{
System.out.println("Error="+e);
e.printStackTrace();
}
}
答案 1 :(得分:5)
在视图上启用绘图缓存:
view.setDrawingCacheEnabled(true);
从缓存中创建一个位图:
bitmap = Bitmap.createBitmap(view.getDrawingCache());
将位图保存在任何地方......
禁用绘图缓存:
view.setDrawingCacheEnabled(false);