1.在手机的图片查看器是没有颜色背景的(没有黑色就显示为黑色)。而画笔的颜色是默认黑色的,所以需要一个非黑色的背景才能够看到黑色的字体。
下面是截屏的关键代码:是从顶层View绘制,所以contentView为根的子控件树不加背景也是可以看到黑色字体的,因为顶层View应该是绘制了白色背景的
static Bitmap getListViewScreenShot(Activity a){
View view = a.getWindow().getDecorView();
/*view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap b1 = view.getDrawingCache();*/
Canvas canvas = new Canvas();
Bitmap b1 = Bitmap.createBitmap(720, 1200, Bitmap.Config.ARGB_8888);
canvas.setBitmap(b1);
view.draw(canvas);
Rect frame = new Rect();
a.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
System.out.println(statusBarHeight);
int width = a.getWindowManager().getDefaultDisplay().getWidth();
int height = a.getWindowManager().getDefaultDisplay()
.getHeight();
// Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);
Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height
- statusBarHeight);
view.destroyDrawingCache();
return b;
}
public static void savePic(Bitmap b, String strFileName) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(strFileName);
if (null != fos) {
final boolean isSuccessCompress = b.compress(Bitmap.CompressFormat.PNG, 90, fos);
if(isSuccessCompress){
Log.e("isSuccessCompress", "yyyyyyyy");
}else{
Log.e("isSuccessCompress", "NNNNNNNN");
}
fos.flush();
fos.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void shootLoacleView(Activity a,String picpath) {
savePic(getListViewScreenShot(a), picpath);
}
而不是从顶层View开始绘制的(即不是调用decorView.draw(canvas)),而是从控件树的其他View开始绘制的,就要给该View绘制个背景canvas.drawColor(Color.WHITE),如果要看到绘制的黑色字体的话
public static Bitmap getListViewBitmap(ListView listView,String picpath) { int h = 0; int w = 0; Bitmap bitmap; int count = listView.getChildCount(); for (int i = 0; i < count; i++) { h += listView.getChildAt(i).getHeight(); } w = listView.getWidth(); bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(); canvas.setBitmap(bitmap); canvas.drawColor(Color.WHITE); View view = (View)listView; view.draw(canvas); return bitmap; } public static void shootListView(ListView listView, String picpath) { savePic(getListViewBitmap(listView,picpath), picpath); } public static Bitmap getScrollViewBitmap(ScrollView scrollView,String picpath) { int h = 0; Bitmap bitmap; // 获取listView实际高度 for (int i = 0; i < scrollView.getChildCount(); i++) { h += scrollView.getChildAt(i).getHeight(); } Log.d(TAG, "" + h); Log.d(TAG, " 高度:" + scrollView.getHeight()); // 创建对应大小的bitmap bitmap = Bitmap.createBitmap(scrollView.getWidth(), h, Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(bitmap); canvas.drawColor(Color.WHITE); scrollView.draw(canvas); /* // 测试输出 FileOutputStream out = null; try { out = new FileOutputStream(picpath); } catch (FileNotFoundException e) { e.printStackTrace(); } try { if (null != out) { bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); out.flush(); out.close(); } } catch (IOException e) { }*/ return bitmap; } static Bitmap getTextViewBitmap(TextView tv){ tv.setDrawingCacheEnabled(true); tv.buildDrawingCache(); Bitmap b1 = tv.getDrawingCache(); return b1; } static void shootTextView(TextView tv, String picPath){ savePic(getTextViewBitmap(tv), picPath); } private static String TAG = "Listview and ScrollView item 截图:"; public static void shootScrollView(ScrollView scrollView, String picpath) { savePic(getScrollViewBitmap(scrollView, picpath), picpath); }
/*注意:上面是的ScrollVIew和ListView的截屏的目的是长截屏,就是没有显示出来 的部分都要截取出来。但是ListView做不到,因为ListView为了效率,把没有显示出来的不绘制,所以只能截取到ListView中显示出来的Item。而ScrollVIew没有作这个处理。*/