一个将当前屏幕保存为图片文件的类(简单说就是抓屏)
2016-03-02 15:41
357 查看
现在无论是应用,还是游戏中,都经常会有分享的功能。分享,不仅要分享文字,也要分享应用或者游戏的屏幕截图,这样才能做到图文并茂,吸引到更多的用户。
想要做图片的分享功能,首先就需要抓屏,将当前屏幕保存为一个图片文件。
下面就是一个将当前的Activity直接保存为一个图片文件的类库,可以直接使用。需要的,直接拿来主义就好了。
[java] view
plain copy
print?
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直接保存为一个图片文件的类库,可以直接使用。需要的,直接拿来主义就好了。
[java] view
plain copy
print?
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夜间模式实现,通过在window上加一层半透明的View
- Android弹幕01
- Win7 网络唤醒(当有TCP连接时自动恢复)
- [转发]HttpWebRequest模拟登陆,存储Cookie以便登录请求后使用
- Cordova 系列之创建一个iOS项目
- 执行Git命令时出现 SSL certificate problem 的解决办法
- android 中的文件储存
- SPRING IN ACTION 第4版笔记-第三章Advancing wiring-001-DataSource在应用和开发环境之间切换 profile
- 6. ZigZag Conversion
- SSH之Java.lang.Exception: Socket bind failed:
- IT资讯---------显示图片
- Javascript的setTimeout详细用例
- HTML中非常有用的但容易被忽略的属性
- Codeforces 624C:Graph and String 二分图染色
- PHP查看文件修改时间
- 70.You want to display only those product names with their list prices where the list price is at le
- php设计模式 数据对象映射模式
- Android View自动生成插件
- IT资讯---------查看文章
- C++实验1-复习