我们知道android中所有的可视组件都是绘制在屏幕上的,Android SDK因此提供了API允许将可视组件绘制在Bitmap上。主要的两个方法是View.setDrawingCacheEnabled和View.getDrawingCache。
==================================贴代码====================================
=============================亮晶晶的分割线===================================
View view = getLayoutInflater().inflate(R.layout.main,null); //打开图像缓存 view.setDrawingCacheEnabled(true); //在保存可视组件的截图前,需要先调用measure和layout方法 //测量大小 view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),); //然后就可以把位置和尺寸发送view和所有子view了 view.layout(0, 0, view.getMeasureWidth, view.getMeasureHeight); try{ //另一个方法获取可视组件的截图 Bitmap bitmap = view.getDrawingCache(); //保存截图,位置随意,格式就png吧 FileOutputStream outstr = new FileOutputStream("/sdcard/my.png"); //100指压缩率 bitmap.compress(CompressFormat.PNG, 100, outstr); }catch(Exception e) { }
很简单对吧。
