【Android】实现截屏功能

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.http://blog.csdn.net/arui319/article/details/9273563


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值