scrollview的可以截取屏幕(包括看不见的部分)整个的高度
public class ScreenShot {
publicstatic
Bitmap takeScreenShot(Activity activity) {
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap b1 = view.getDrawingCache();
Rect frame =new
Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
intstatusBarHeight = frame.top;
System.out.println(statusBarHeight);
intwidth = activity.getWindowManager().getDefaultDisplay().getWidth();
intheight = activity.getWindowManager().getDefaultDisplay()
.getHeight();
Bitmap b = Bitmap.createBitmap(b1,0, statusBarHeight, width, height
- statusBarHeight);
view.destroyDrawingCache();
savePic(b,"/sdcard/screen_test.png");
returnb;
}
publicstatic
void savePic(Bitmap b, String strFileName) {
FileOutputStream fos =null;
try{
fos =new
FileOutputStream(strFileName);
if(null
!= fos) {
b.compress(Bitmap.CompressFormat.PNG,90, fos);
fos.flush();
fos.close();
}
}catch
(FileNotFoundException e) {
e.printStackTrace();
}catch
(IOException e) {
e.printStackTrace();
}
}
/**
* 把View对象转换成bitmap
* */
publicstatic
Bitmap convertViewToBitmap(View view) {
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
view.layout(0,0, view.getMeasuredWidth(),
view.getMeasuredHeight());
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
if(bitmap !=
null) {
System.out.println("这不是nullde1");
Log.d("nullde1","nullde1");
}else
{
System.out.println("这nullnulllnulnlul");
}
returnbitmap;
}
publicstatic
void shoot(Activity a) {
ScreenShot.savePic(ScreenShot.takeScreenShot(a),"/sdcard/screen_test.png");
}
publicstatic
void shootView(View view) {
ScreenShot.savePic(ScreenShot.convertViewToBitmap(view),
"sdcard/xx.png");
}
publicstatic
Bitmap getViewBitmap(View v) {
v.clearFocus();
v.setPressed(false);
booleanwillNotCache = v.willNotCacheDrawing();
v.setWillNotCacheDrawing(false);
intcolor = v.getDrawingCacheBackgroundColor();
v.setDrawingCacheBackgroundColor(0);
if(color !=
0) {
v.destroyDrawingCache();
}
v.buildDrawingCache();
Bitmap cacheBitmap = v.getDrawingCache();
if(cacheBitmap ==
null) {
Log.e("TTTTTTTTActivity","failed getViewBitmap("
+ v + ")",
newRuntimeException());
returnnull;
}
Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
v.destroyDrawingCache();
v.setWillNotCacheDrawing(willNotCache);
v.setDrawingCacheBackgroundColor(color);
returnbitmap;
}
/**
* 截取scrollview的屏幕
* **/
publicstatic
Bitmap getBitmapByView(ScrollView scrollView) {
inth =
0;
Bitmap bitmap =null;
for(int
i = 0; i < scrollView.getChildCount(); i++) {
h += scrollView.getChildAt(i).getHeight();
scrollView.getChildAt(i).setBackgroundResource(R.drawable.bg3);
}
Log.d(TAG,"实际高度:"
+ h);
Log.d(TAG," 高度:"
+ scrollView.getHeight());
bitmap = Bitmap.createBitmap(scrollView.getWidth(), h,
Bitmap.Config.ARGB_8888);
finalCanvas canvas =
newCanvas(bitmap);
scrollView.draw(canvas);
FileOutputStream out =null;
try{
out =new
FileOutputStream("/sdcard/screen_test.png");
}catch
(FileNotFoundException e) {
e.printStackTrace();
}
try{
if(null
!= out) {
bitmap.compress(Bitmap.CompressFormat.PNG,100, out);
out.flush();
out.close();
}
}catch
(IOException e) {
}
returnbitmap;
}
privatestatic
String TAG = "Listview and ScrollView item 截图:";
/**
* 截图listview
* **/
publicstatic
Bitmap getbBitmap(ListView listView) {
inth =
0;
Bitmap bitmap =null;
for(int
i = 0; i < listView.getChildCount(); i++) {
h += listView.getChildAt(i).getHeight();
}
Log.d(TAG,"实际高度:"
+ h);
Log.d(TAG,"list 高度:"
+ listView.getHeight());
bitmap = Bitmap.createBitmap(listView.getWidth(), h,
Bitmap.Config.ARGB_8888);
finalCanvas canvas =
newCanvas(bitmap);
listView.draw(canvas);
FileOutputStream out =null;
try{
out =new
FileOutputStream("/sdcard/screen_test.png");
}catch
(FileNotFoundException e) {
e.printStackTrace();
}
try{
if(null
!= out) {
bitmap.compress(Bitmap.CompressFormat.PNG,100, out);
out.flush();
out.close();
}
}catch
(IOException e) {
}
returnbitmap;
}
}
转载自:http://www.cnblogs.com/lee0oo0/p/3355468.html