本文来源:http://myhpu2008.iteye.com/blog/999779<wbr style="line-height:25px"><div style="line-height:25px"><span style="color:#003366; line-height:25px">这种方法应该只能对当前Activity本身进行截屏,因而你只能在你应用程序中参照该代码对其应用程序本身截屏。</span></div> <div style="line-height:25px"> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">import java.io.FileNotFoundException;</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">import java.io.FileOutputStream;</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">import java.io.IOException;</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"><br style="line-height:25px"></span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">import android.app.Activity;</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">import android.graphics.Bitmap;</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">import android.graphics.Rect;</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">import android.util.Log;</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">import android.view.View;</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"><br style="line-height:25px"></span></div> <div style="line-height:25px"><span style="color:#808000; line-height:25px">public class ScreenShot {</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> // 获取指定Activity的截屏,保存到png文件</span></div> <div style="line-height:25px"> <span style="color:#3366ff; line-height:25px"> </span><span style="color:#99cc00; line-height:25px">private static Bitmap takeScreenShot(Activity activity) {</span> </div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> // View是你需要截图的View</span></div> <div style="line-height:25px"><span style="color:#ff6600; line-height:25px"> View view = activity.getWindow().getDecorView();</span></div> <div style="line-height:25px"><span style="color:#ff6600; line-height:25px"> view.setDrawingCacheEnabled(true);</span></div> <div style="line-height:25px"><span style="color:#ff6600; line-height:25px"> view.buildDrawingCache();</span></div> <div style="line-height:25px"><span style="color:#ff6600; line-height:25px"> Bitmap b1 = view.getDrawingCache();</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"><br style="line-height:25px"></span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> // 获取状态栏高度</span></div> <div style="line-height:25px"><span style="color:#0000ff; line-height:25px"> Rect frame = new Rect();</span></div> <div style="line-height:25px"><span style="color:#0000ff; line-height:25px"> activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);</span></div> <div style="line-height:25px"><span style="color:#0000ff; line-height:25px"> int statusBarHeight = frame.top;</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> Log.i("TAG", "" + statusBarHeight);</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"><br style="line-height:25px"></span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> // 获取屏幕长和高</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> int width = activity.getWindowManager().getDefaultDisplay().getWidth();</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> int height = activity.getWindowManager().getDefaultDisplay()</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> .getHeight();</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> // 去掉标题栏</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> // Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> - statusBarHeight);</span></div> <div style="line-height:25px"> <span style="color:#3366ff; line-height:25px"> </span><span style="color:#0000ff; line-height:25px">view.destroyDrawingCache();</span> </div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> return b;</span></div> <div style="line-height:25px"> <span style="color:#3366ff; line-height:25px"> </span><span style="color:#99cc00; line-height:25px">}</span> </div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"><br style="line-height:25px"></span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> // 保存到sdcard</span></div> <div style="line-height:25px"> <span style="color:#3366ff; line-height:25px"></span><span style="color:#99cc00; line-height:25px"> private static void savePic(Bitmap b, String strFileName) {</span> </div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> FileOutputStream fos = null;</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> try {</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> fos = new FileOutputStream(strFileName);</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> if (null != fos) {</span></div> <div style="line-height:25px"> <span style="color:#3366ff; line-height:25px"> </span><span style="color:#0000ff; line-height:25px">b.compress(Bitmap.CompressFormat.PNG, 90, fos);</span> </div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> fos.flush();</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> fos.close();</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> }</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> } catch (FileNotFoundException e) {</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> e.printStackTrace();</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> } catch (IOException e) {</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> e.printStackTrace();</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> }</span></div> <div style="line-height:25px"> <span style="color:#3366ff; line-height:25px"></span><span style="color:#99cc00; line-height:25px"> }</span> </div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"><br style="line-height:25px"></span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> // 程序入口</span></div> <div style="line-height:25px"> <span style="color:#3366ff; line-height:25px"> </span><span style="color:#99cc00; line-height:25px">public static void shoot(Activity a) {</span> </div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> ScreenShot.savePic(ScreenShot.takeScreenShot(a), "sdcard/xx.png");</span></div> <div style="line-height:25px"> <span style="color:#3366ff; line-height:25px"> </span><span style="color:#99cc00; line-height:25px">}</span> </div> <div style="line-height:25px"><span style="color:#808000; line-height:25px">}</span></div> </div> <div style="line-height:25px"> <span style="line-height:25px">注意</span>:<span style="color:#000080; line-height:25px">shoot方法只能在view已经被加载后方可调用。</span> </div> </wbr>