您的位置:首页 > 移动开发 > Android开发

【Android】实现截屏功能

2014-07-29 02:22 417 查看
1.截屏功能在像QQ等软件中我们有所接触,今天特意实践了一下,竟然成功了,O(∩_∩)O~

2.实现代码

package com.example.ui;

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;

/**
* Description: 显示截屏功能
*
* @author danDingCongRong
* @Version 1.0.0
* @Created at 2014-7-29 01:55:44
* @Modified by [作者] on [修改日期]
*/
public class ScreenShot {

private static Bitmap takeScreenShot(Activity activity) {
// View是你需要截图的View
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bmp = 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 noTitleBmp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width,
height - statusBarHeight);
view.destroyDrawingCache();

return noTitleBmp;
}

private static void savePic(Bitmap bmp, File filePath) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(filePath);

if (null != fos) {
bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != fos) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

public static void shoot(Activity activity, String filePath) {
if (null == filePath) {
return;
}

File file = new File(filePath);
if (!file.exists()) {
file.mkdirs();
}

Bitmap screentBmp = ScreenShot.takeScreenShot(activity);
ScreenShot.savePic(screentBmp, file);
}

}

3.测试代码

public void testScreenShot(View view) {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_UNMOUNTED)) {
return;
}

String filePath = Environment.getExternalStorageDirectory()
+ "/Android/data/com.example.ui/image";
ScreenShot.shoot(this, filePath);
}

public void testReadScreenShotImageView(View view) {
Intent intent = new Intent();
intent.setClass(this, TestScreenShotActivity.class);
startActivity(intent);
}
package com.example.ui.activity;

import java.io.File;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.Window;
import android.widget.ImageView;

import com.example.ui.R;

public class TestScreenShotActivity extends Activity {

private ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_test_ui);

initView();
}

private void initView() {
imageView = (ImageView) findViewById(R.id.imageView);

String filename = Environment.getExternalStorageDirectory()
+ "/Android/data/com.example.ui/image";
File file = new File(filename);
if (file.exists()) {
Bitmap bm = BitmapFactory.decodeFile(filename);// 从文件中读取图片
imageView.setImageBitmap(bm);
}
}

}


参考:

1./article/2474054.html


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: