取得当前屏幕的截图主要使用了Bitmap 类里的方法,首相取得一个bitmap 然后用取得的bitmap 去绘制一张图片然后进行保存。
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.os.Environment;
import android.util.Log;
import android.view.View;
public class ShotScreen {
private static final String TAG="shotscreen";
@SuppressWarnings("unused")
private static Bitmap takeScreenShot(Activity activity){
View view =activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
Rect rect = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
int statusBarHeight = rect.top;
System.out.println(statusBarHeight);
int width =activity.getWindowManager().getDefaultDisplay().getWidth();
int height =activity.getWindowManager().getDefaultDisplay().getHeight();
Bitmap bitmap2 = Bitmap.createBitmap(bitmap,0,statusBarHeight, width, height - statusBarHeight);
view.destroyDrawingCache();
return bitmap2;
}
@SuppressWarnings("unused")
private static void savePic(Bitmap bitmap,String filename){
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(filename);
if (fileOutputStream != null) {
bitmap.compress(Bitmap.CompressFormat.PNG, 90, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
}
} catch (FileNotFoundException e) {
Log.d(TAG, "Exception:FileNotFoundException");
e.printStackTrace();
} catch (IOException e) {
Log.d(TAG, "IOException:IOException");
e.printStackTrace();
}
}
/**
*
* @param a
*/
public static void shoot(Activity a){
if (android.os.Environment.MEDIA_MOUNTED != "mounted") {
ShotScreen.savePic(ShotScreen.takeScreenShot(a), "/sdcard/抽样.png");
}else{
ShotScreen.savePic(ShotScreen.takeScreenShot(a), "/data/data/"+a.getPackageName()+"/抽样.png");
}
}
android之截屏(包括截取scrollview与listview的)
public class ScreenShot {
// 获取指定Activity的截屏,保存到png文件
public static Bitmap takeScreenShot(Activity activity) {
// View是你需要截图的View
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap b1 = view.getDrawingCache();
// 获取状态栏高度
Rect frame = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
System.out.println(statusBarHeight);
// 获取屏幕长和高
int width = activity.getWindowManager().getDefaultDisplay().getWidth();
int height = activity.getWindowManager().getDefaultDisplay()
.getHeight();
// 去掉标题栏
// Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);
Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height
- statusBarHeight);
view.destroyDrawingCache();
savePic(b, "/sdcard/screen_test.png");
return b;
}
// 保存到sdcard
public static 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
* */
public static 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");
}
return bitmap;
}
// 程序入口1
public static void shoot(Activity a) {
ScreenShot.savePic(ScreenShot.takeScreenShot(a), "/sdcard/screen_test.png");
}
// 程序入口2
public static void shootView(View view) {
ScreenShot.savePic(ScreenShot.convertViewToBitmap(view),
"sdcard/xx.png");
}
public static Bitmap getViewBitmap(View v) {
v.clearFocus();
v.setPressed(false);
boolean willNotCache = v.willNotCacheDrawing();
v.setWillNotCacheDrawing(false);
// Reset the drawing cache background color to fully transparent
// for the duration of this operation
int color = 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 + ")",
new RuntimeException());
return null;
}
Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
// Restore the view
v.destroyDrawingCache();
v.setWillNotCacheDrawing(willNotCache);
v.setDrawingCacheBackgroundColor(color);
return bitmap;
}
/**
* 截取scrollview的屏幕
* **/
public static Bitmap getBitmapByView(ScrollView scrollView) {
int h = 0;
Bitmap bitmap = null;
// 获取listView实际高度
for (int i = 0; i
Android中View转换为Bitmap及getDrawingCache=null的解决方法
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.e1idc.net