手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >Android截屏保存png图片的实例代码
Android截屏保存png图片的实例代码
摘要:复制代码代码如下:importjava.io.FileNotFoundException;importjava.io.FileOutputS...

复制代码 代码如下:

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.util.Log;

import android.view.View;

public class ScreenShot {

// 获取指定Activity的截屏,保存到png文件

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;

Log.i("TAG", "" + 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();

return b;

}

// 保存到sdcard

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 (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

// 程序入口

public static void shoot(Activity a) {

ScreenShot.savePic(ScreenShot.takeScreenShot(a), "sdcard/xx.png");

}

}

需要注意的是,shoot方法只能在view已经被加载后方可调用。

或者在

复制代码 代码如下:

@Override

public void onWindowFocusChanged(boolean hasFocus) {

// TODO Auto-generated method stub

super.onWindowFocusChanged(hasFocus);

ScreenShot.shoot(this);

}中调用

【Android截屏保存png图片的实例代码】相关文章:

android LinearLayout 布局实例代码

Android如何实现非本地图片的点击态

android 设置圆角图片实现代码

Android裁剪图片为圆形图片的实现原理与代码

Android开场动画实例类Java代码

Android用户界面开发之:TextView的使用实例

android客户端从服务器端获取json数据并解析的实现代码

android实现横屏的代码及思路

Android 异步加载图片的实例代码

Android 自定义标题栏 显示网页加载进度的方法实例

精品推荐
分类导航