import java.io.FileOutputStream;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.view.View;
public class Screenshots {
private static Bitmap takeScreenShot(Activity a) {
View view = a.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap b1 = view.getDrawingCache();
Rect frame = new Rect();
a.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
int width = a.getWindowManager().getDefaultDisplay().getWidth();
int height = a.getWindowManager().getDefaultDisplay().getHeight();
// 删除标题栏
Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight,
width, height - statusBarHeight);
view.destroyDrawingCache();
return b;
}
private 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 (Exception e) {
e.printStackTrace();
}
}
public static void shoot(Activity a) {
savePic(takeScreenShot(a), "/sdcard/xxx.png");
}
}
Android抓屏源码
最新推荐文章于 2025-12-05 09:24:48 发布
1186

被折叠的 条评论
为什么被折叠?



