Android编程之截屏实现方法(包括scrollview与listview)
本文实例讲述了Android编程之截屏实现方法。分享给大家供大家参考,具体如下:
publicclassScreenShot{
//获取指定Activity的截屏,保存到png文件
publicstaticBitmaptakeScreenShot(Activityactivity){
//View是你需要截图的View
Viewview=activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmapb1=view.getDrawingCache();
//获取状态栏高度
Rectframe=newRect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
intstatusBarHeight=frame.top;
System.out.println(statusBarHeight);
//获取屏幕长和高
intwidth=activity.getWindowManager().getDefaultDisplay().getWidth();
intheight=activity.getWindowManager().getDefaultDisplay()
.getHeight();
//去掉标题栏
//Bitmapb=Bitmap.createBitmap(b1,0,25,320,455);
Bitmapb=Bitmap.createBitmap(b1,0,statusBarHeight,width,height
-statusBarHeight);
view.destroyDrawingCache();
savePic(b,"/sdcard/screen_test.png");
returnb;
}
//保存到sdcard
publicstaticvoidsavePic(Bitmapb,StringstrFileName){
FileOutputStreamfos=null;
try{
fos=newFileOutputStream(strFileName);
if(null!=fos){
b.compress(Bitmap.CompressFormat.PNG,90,fos);
fos.flush();
fos.close();
}
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
}
/**
*把View对象转换成bitmap
**/
publicstaticBitmapconvertViewToBitmap(Viewview){
view.measure(MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED));
view.layout(0,0,view.getMeasuredWidth(),view.getMeasuredHeight());
view.buildDrawingCache();
Bitmapbitmap=view.getDrawingCache();
if(bitmap!=null){
System.out.println("这不是nullde1");
Log.d("nullde1","nullde1");
}else{
System.out.println("这nullnulllnulnlul");
}
returnbitmap;
}
//程序入口1
publicstaticvoidshoot(Activitya){
ScreenShot.savePic(ScreenShot.takeScreenShot(a),"/sdcard/screen_test.png");
}
//程序入口2
publicstaticvoidshootView(Viewview){
ScreenShot.savePic(ScreenShot.convertViewToBitmap(view),
"sdcard/xx.png");
}
publicstaticBitmapgetViewBitmap(Viewv){
v.clearFocus();
v.setPressed(false);
booleanwillNotCache=v.willNotCacheDrawing();
v.setWillNotCacheDrawing(false);
//Resetthedrawingcachebackgroundcolortofullytransparent
//forthedurationofthisoperation
intcolor=v.getDrawingCacheBackgroundColor();
v.setDrawingCacheBackgroundColor(0);
if(color!=0){
v.destroyDrawingCache();
}
v.buildDrawingCache();
BitmapcacheBitmap=v.getDrawingCache();
if(cacheBitmap==null){
Log.e("TTTTTTTTActivity","failedgetViewBitmap("+v+")",
newRuntimeException());
returnnull;
}
Bitmapbitmap=Bitmap.createBitmap(cacheBitmap);
//Restoretheview
v.destroyDrawingCache();
v.setWillNotCacheDrawing(willNotCache);
v.setDrawingCacheBackgroundColor(color);
returnbitmap;
}
/**
*截取scrollview的屏幕
***/
publicstaticBitmapgetBitmapByView(ScrollViewscrollView){
inth=0;
Bitmapbitmap=null;
//获取listView实际高度
for(inti=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=Bitmap.createBitmap(scrollView.getWidth(),h,
Bitmap.Config.ARGB_8888);
finalCanvascanvas=newCanvas(bitmap);
scrollView.draw(canvas);
//测试输出
FileOutputStreamout=null;
try{
out=newFileOutputStream("/sdcard/screen_test.png");
}catch(FileNotFoundExceptione){
e.printStackTrace();
}
try{
if(null!=out){
bitmap.compress(Bitmap.CompressFormat.PNG,100,out);
out.flush();
out.close();
}
}catch(IOExceptione){
//TODO:handleexception
}
returnbitmap;
}
privatestaticStringTAG="ListviewandScrollViewitem截图:";
/**
*截图listview
***/
publicstaticBitmapgetbBitmap(ListViewlistView){
inth=0;
Bitmapbitmap=null;
//获取listView实际高度
for(inti=0;i<listView.getChildCount();i++){
h+=listView.getChildAt(i).getHeight();
}
Log.d(TAG,"实际高度:"+h);
Log.d(TAG,"list高度:"+listView.getHeight());
//创建对应大小的bitmap
bitmap=Bitmap.createBitmap(listView.getWidth(),h,
Bitmap.Config.ARGB_8888);
finalCanvascanvas=newCanvas(bitmap);
listView.draw(canvas);
//测试输出
FileOutputStreamout=null;
try{
out=newFileOutputStream("/sdcard/screen_test.png");
}catch(FileNotFoundExceptione){
e.printStackTrace();
}
try{
if(null!=out){
bitmap.compress(Bitmap.CompressFormat.PNG,100,out);
out.flush();
out.close();
}
}catch(IOExceptione){
//TODO:handleexception
}
returnbitmap;
}
}
希望本文所述对大家Android程序设计有所帮助。