一个将当前屏幕保存为图片文件的类(简单说就是抓屏)
2015-01-04 18:55
260 查看
现在无论是应用,还是游戏中,都经常会有分享的功能。分享,不仅要分享文字,也要分享应用或者游戏的屏幕截图,这样才能做到图文并茂,吸引到更多的用户。
想要做图片的分享功能,首先就需要抓屏,将当前屏幕保存为一个图片文件。
下面就是一个将当前的Activity直接保存为一个图片文件的类库,可以直接使用。需要的,直接拿来主义就好了。
package com.gaolei.framework.android.util;
import java.io.File;
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.view.View;
public class ScreenShot {
private 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;
// 获取屏幕长和高
int width = activity.getWindowManager().getDefaultDisplay().getWidth();
int height = activity.getWindowManager().getDefaultDisplay()
.getHeight();
// 去掉标题栏
Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height
- statusBarHeight);
view.destroyDrawingCache();
return b;
}
private static void savePic(Bitmap b, File filePath) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(filePath);
if (null != fos) {
b.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
}
} catch (FileNotFoundException e) {
// e.printStackTrace();
} catch (IOException e) {
// e.printStackTrace();
}
}
public static void shoot(Activity a, File filePath) {
if (filePath == null) {
return;
}
if (!filePath.getParentFile().exists()) {
filePath.getParentFile().mkdirs();
}
ScreenShot.savePic(ScreenShot.takeScreenShot(a), filePath);
}
}
想要做图片的分享功能,首先就需要抓屏,将当前屏幕保存为一个图片文件。
下面就是一个将当前的Activity直接保存为一个图片文件的类库,可以直接使用。需要的,直接拿来主义就好了。
package com.gaolei.framework.android.util;
import java.io.File;
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.view.View;
public class ScreenShot {
private 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;
// 获取屏幕长和高
int width = activity.getWindowManager().getDefaultDisplay().getWidth();
int height = activity.getWindowManager().getDefaultDisplay()
.getHeight();
// 去掉标题栏
Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height
- statusBarHeight);
view.destroyDrawingCache();
return b;
}
private static void savePic(Bitmap b, File filePath) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(filePath);
if (null != fos) {
b.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
}
} catch (FileNotFoundException e) {
// e.printStackTrace();
} catch (IOException e) {
// e.printStackTrace();
}
}
public static void shoot(Activity a, File filePath) {
if (filePath == null) {
return;
}
if (!filePath.getParentFile().exists()) {
filePath.getParentFile().mkdirs();
}
ScreenShot.savePic(ScreenShot.takeScreenShot(a), filePath);
}
}
相关文章推荐
- 一个将当前屏幕保存为图片文件的类(简单说就是抓屏)
- [Android] 一个将当前屏幕保存为图片文件的类(简单说就是抓屏)
- 一个将当前屏幕保存为图片文件的类(简单说就是抓屏)
- C#——主进程负责向屏幕不间断输出四位随机数,子线程负责每隔 1 秒钟对当前计算机屏幕进行截屏并以 jpg 格式的图片保存文件。
- 当前屏幕保存为图片文件的类(getWindow().getDecorView())-----截屏
- android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte
- 将文件隐藏图片中的一个简单方法
- 用一个字段保存多个图片或文件路径
- 将图片保存到一个XML文件
- 获取当前屏幕图像并保存为bmp文件
- IOS中截取当前屏幕,并保存图片
- 自己写的一个简单的Resources文件,主要功能就是切换文化
- 就是那个上传的。按实际上传时的文件名称保存(不在是test.jpg了)图片可以改大小,图
- 将屏幕保存为图片 将当前MFC程序保存为图片 c++ vc
- 汇编语言:请写一个程序把当前目录下的TEXT.TXT文件中的字符读出来显示在屏幕上,当遇到结束符时就停止读取。
- JQuery Lightbox -- "一个简单而又谦恭的用来把图片覆盖在当前页面上的脚本"
- 将图片保存到一个XML文件
- 将图片保存到一个XML文件
- 将Word中的图片保存为一个文件
- Flash Air2 一个保存本地文件的简单例子。