Android实现截图功能(可根据该代码进行扩展功能)

            Android实现截图功能(可根据该代码进行扩展)(博客地址:http://blog.csdn.net/developer_jiangqq)

         Author:hmjiangqq

         Email:jiangqqlmj@163.com

         有些时候我们在进行做分享功能或者截图保存,今天写了一个简单的demo来实现截图功能,并且把截图保存到sdcard中.(当然这里只是简单的实现一下功能,当然还可以进行功能扩展.例如:摇一摇监听事件然后截图或者开启一个固定的时间然后进行截图操作)

         废话不多说,效果比较简单,代码也不多(已经添加详细注释了);

          ScreenShotUtils.java

package com.pps.screen.activity;

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;

/**
 * 进行截屏工具类
 * @author jiangqingqing
 * @time 2013/09/29
 */
public class ScreenShotUtils {	
	/**
	 * 进行截取屏幕  
	 * @param pActivity
	 * @return bitmap
	 */
	public static Bitmap takeScreenShot(Activity pActivity)
	{
		Bitmap bitmap=null;
		View view=pActivity.getWindow().getDecorView();
		// 设置是否可以进行绘图缓存
		view.setDrawingCacheEnabled(true);
		// 如果绘图缓存无法,强制构建绘图缓存
		view.buildDrawingCache();
	    // 返回这个缓存视图 
		bitmap=view.getDrawingCache();
		
		// 获取状态栏高度
		Rect frame=new Rect();
		// 测量屏幕宽和高
		view.getWindowVisibleDisplayFrame(frame);
		int stautsHeight=frame.top;
		Log.d("jiangqq", "状态栏的高度为:"+stautsHeight);
		
		int width=pActivity.getWindowManager().getDefaultDisplay().getWidth();
		int height=pActivity.getWindowManager().getDefaultDisplay().getHeight();
		// 根据坐标点和需要的宽和高创建bitmap
		bitmap=Bitmap.createBitmap(bitmap, 0, stautsHeight, width, height-stautsHeight);
		return bitmap;
	}
	
	
	/**
	 * 保存图片到sdcard中
	 * @param pBitmap
	 */
	private static boolean savePic(Bitmap pBitmap,String strName)
	{
	  FileOutputStream fos=null;
	  try {
		fos=new FileOutputStream(strName);
		if(null!=fos)
		{
			pBitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);
			fos.flush();
			fos.close();
			return true;
		}
		
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	}catch (IOException e) {
		e.printStackTrace();
	}
	  return false;
	} 
	/**
	 * 截图
	 * @param pActivity 
	 * @return 截图并且保存sdcard成功返回true,否则返回false
	 */
	public static boolean shotBitmap(Activity pActivity)
	{
		
		return  ScreenShotUtils.savePic(takeScreenShot(pActivity), "sdcard/"+System.currentTimeMillis()+".png");
	}
	
	
}
  然后直接在需要的地方进行调用就shotBitmap(pActivity)方法就行,布局文件如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@android:color/black"
     >

    
    <TextView 
        android:id="@+id/tv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textColor="@android:color/darker_gray"
        android:text="    这是模拟截图的demo,点击下面的按钮直接截图,当然你还可以在此基础上面进行扩展更多丰富的功能截图;(例如:摇一摇,或者开启截图服务,N秒时间后自动截图保存)"/>
    
    <ImageView 
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/keji"
        android:scaleType="fitXY"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dip"/>
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击截图" 
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dip"/>

</LinearLayout>
调用代码:

		boolean result = ScreenShotUtils.shotBitmap(MainActivity.this);
				if(result)
				{
					Toast.makeText(MainActivity.this, "截图成功.", Toast.LENGTH_SHORT).show();
				}else {
					Toast.makeText(MainActivity.this, "截图失败.", Toast.LENGTH_SHORT).show();
				}
				
例子比较简单,效果如下:



例子代码下载:http://download.csdn.net/detail/jiangqq781931404/6338555

          

  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值