当前位置:操作系统 > 安卓/Android >>

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

   Android实现截图功能(可根据该代码进行扩展)(博客地址:http://blog.csdn.net/developer_jiangqq)
         Author:hmjiangqq
         Email:jiangqqlmj@163.com
         有些时候我们在进行做分享功能或者截图保存,今天写了一个简单的demo来实现截图功能,并且把截图保存到sdcard中.(当然这里只是简单的实现一下功能,当然还可以进行功能扩展.例如:摇一摇监听事件然后截图或者开启一个固定的时间然后进行截图操作)
         废话不多说,效果比较简单,代码也不多(已经添加详细注释了);
          ScreenShotUtils.java
[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)方法就行,布局文件如下:
[html] 
<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"/>  
   
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,