Android屏幕截图实现

方法一,根据android代码实现的流程在复制一遍,流程上一篇已经大概看过了,网上也有很多已经实现了。下面我就转载一篇

来自http://blog.csdn.net/hk_256 

1. Activity文件

[java]  view plain copy print ?
  1. package com.arvinhe.testscreenshot;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.graphics.Bitmap;  
  6. import android.graphics.Canvas;  
  7. import android.graphics.Matrix;  
  8. import android.os.Bundle;  
  9. import android.util.DisplayMetrics;  
  10. import android.view.Display;  
  11. import android.view.Surface;  
  12. import android.view.View;  
  13. import android.view.WindowManager;  
  14. import android.view.View.OnClickListener;  
  15. import android.widget.Button;  
  16. import android.widget.ImageView;  
  17.   
  18. public class TestScreenShotActivity extends Activity implements OnClickListener{  
  19.       
  20.     private ImageView img_display;  
  21.     private Button bt_screenshot;  
  22.       
  23.     private Display mDisplay;  
  24.     private DisplayMetrics mDisplayMetrics;  
  25.     private Matrix mDisplayMatrix;  
  26.     private Bitmap mScreenBitmap;  
  27.     private WindowManager mWindowManager;  
  28.       
  29.     @Override  
  30.     public void onCreate(Bundle savedInstanceState) {  
  31.         super.onCreate(savedInstanceState);  
  32.         setContentView(R.layout.main);  
  33.           
  34.         bt_screenshot = (Button)findViewById(R.id.bt_screenshot);  
  35.         img_display = (ImageView)findViewById(R.id.img_display);  
  36.           
  37.         bt_screenshot.setOnClickListener(this);  
  38.           
  39.         mDisplayMatrix = new Matrix();  
  40.           
  41.         mWindowManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);  
  42.         mDisplay = mWindowManager.getDefaultDisplay();  
  43.         mDisplayMetrics = new DisplayMetrics();  
  44.         mDisplay.getRealMetrics(mDisplayMetrics);  
  45.     }  
  46.   
  47.     @Override  
  48.     public void onClick(View v) {  
  49.         if(v.equals(bt_screenshot)){  
  50.             mDisplay.getRealMetrics(mDisplayMetrics);  
  51.             float[] dims = {mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels};  
  52.             float degrees = getDegreesForRotation(mDisplay.getRotation());  
  53.             boolean requiresRotation = (degrees > 0);  
  54.             if (requiresRotation) {  
  55.                 // Get the dimensions of the device in its native orientation  
  56.                 mDisplayMatrix.reset();  
  57.                 mDisplayMatrix.preRotate(-degrees);  
  58.                 mDisplayMatrix.mapPoints(dims);  
  59.                 dims[0] = Math.abs(dims[0]);  
  60.                 dims[1] = Math.abs(dims[1]);  
  61.             }  
  62.             mScreenBitmap = Surface.screenshot((int) dims[0], (int) dims[1]);  
  63.             if (requiresRotation) {  
  64.                 // Rotate the screenshot to the current orientation  
  65.                 Bitmap ss = Bitmap.createBitmap(mDisplayMetrics.widthPixels,  
  66.                         mDisplayMetrics.heightPixels, Bitmap.Config.ARGB_8888);  
  67.                 Canvas c = new Canvas(ss);  
  68.                 c.translate(ss.getWidth() / 2, ss.getHeight() / 2);  
  69.                 c.rotate(degrees);  
  70.                 c.translate(-dims[0] / 2, -dims[1] / 2);  
  71.                 c.drawBitmap(mScreenBitmap, 00null);  
  72.                 c.setBitmap(null);  
  73.                 mScreenBitmap = ss;  
  74.             }  
  75.   
  76.             // If we couldn't take the screenshot, notify the user  
  77.             if (mScreenBitmap == null) {  
  78.                  
  79.                 return;  
  80.             }  
  81.   
  82.             // Optimizations  
  83.             mScreenBitmap.setHasAlpha(false);  
  84.             mScreenBitmap.prepareToDraw();  
  85.               
  86.             img_display.setImageBitmap(mScreenBitmap);  
  87.         }  
  88.           
  89.     }  
  90.       
  91.     /** 
  92.      * @return the current display rotation in degrees 
  93.      */  
  94.     private float getDegreesForRotation(int value) {  
  95.         switch (value) {  
  96.         case Surface.ROTATION_90:  
  97.             return 360f - 90f;  
  98.         case Surface.ROTATION_180:  
  99.             return 360f - 180f;  
  100.         case Surface.ROTATION_270:  
  101.             return 360f - 270f;  
  102.         }  
  103.         return 0f;  
  104.     }  
  105. }  

2. AndroidManifest.xml文件

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.arvinhe.testscreenshot"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0"   
  6.     android:sharedUserId="android.uid.system">  
  7.   
  8.     <uses-sdk android:minSdkVersion="15" />  
  9.       
  10.   
  11.     <application  
  12.         android:icon="@drawable/ic_launcher"  
  13.         android:label="@string/app_name" >  
  14.         <activity  
  15.             android:name=".TestScreenShotActivity"  
  16.             android:label="@string/app_name" >  
  17.             <intent-filter>  
  18.                 <action android:name="android.intent.action.MAIN" />  
  19.   
  20.                 <category android:name="android.intent.category.LAUNCHER" />  
  21.             </intent-filter>  
  22.         </activity>  
  23.     </application>  
  24.   
  25. </manifest>  

3. Layout文件

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="@string/hello" />  
  11.       
  12.     <Button  
  13.         android:id="@+id/bt_screenshot"  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="Screen Shot"  
  17.         />  
  18.       
  19.     <ImageView   
  20.         android:id="@+id/img_display"  
  21.         android:layout_width="fill_parent"  
  22.         android:layout_height="wrap_content"  
  23.         android:src="@drawable/ic_launcher"/>  
  24.   
  25. </LinearLayout>  
方法2,根据DDMS里的截图功能实现,也有现成的。

来自:http://blog.csdn.net/ericahdu/article/details/5533091

我们有时候只是需要截图,没必要连DDMS一起开,所以剥离了截图的代码,当然,并不是原生的啊,是根据原理自己写的,供大家参考

 

第一步,准备库包

 

     我们既然是按照DDMS的方法截图,就需要用到ddmlib.jar这个包,它位于android的SDK目录的tools/lib下,我们需要把它加入到我们

 

的Eclipse工程的build path下。

 

第二步,建立连接,获取设备

 

     有了ddmlib,我们就可以使用里面的 AndroidDebugBridge 类来获取已经同步的设备的列表并建立连接

 

    

  1. IDevice device;  
  2. AndroidDebugBridge bridge = AndroidDebugBridge.createBridge();  
  3. waitDeviceList(bridge);  
  4.           
  5. IDevice devices[] = bridge.getDevices();  
  6. device = devices[0];  

 

上面的代码用到了一个waitDeviceList(bridge),主要是为了多次尝试连接,代码如下

 

  1. private static void waitDeviceList(AndroidDebugBridge bridge) {  
  2.     int count = 0;  
  3.     while (bridge.hasInitialDeviceList() == false) {   
  4.        try {  
  5.        Thread.sleep(100); // 如果没有获得设备列表,则等待  
  6.        ount++;  
  7.        } catch (InterruptedException e) {}  
  8.        if (count > 300) {    // 设定时间超过300×100 ms的时候为连接超时  
  9.        System.err.print("Time out");  
  10.        break;  
  11.        }  
  12.     }  
  13. }  

 

这样我们就可以获得一个设备的类,IDevice,其中有一个getScreenshot()方法获得屏幕截图,类型为RawImage

  1. RawImage rawScreen = device.getScreenshot();  

后面的方法就和Android无关了,纯粹的转换,Rawimage转换到bufferedimage,再保存

  1. if(rawScreen != null){  
  2.                 BufferedImage image = null;  
  3.                 int width2 = landscape ? rawScreen.height : rawScreen.width;  
  4.                 int height2 = landscape ? rawScreen.width : rawScreen.height;  
  5.                 if (image == null) {  
  6.                     image = new BufferedImage(width2, height2,  
  7.                             BufferedImage.TYPE_INT_RGB);  
  8.                 } else {  
  9.                     if (image.getHeight() != height2 || image.getWidth() != width2) {  
  10.                         image = new BufferedImage(width2, height2,  
  11.                                 BufferedImage.TYPE_INT_RGB);  
  12.                     }  
  13.                 }  
  14.                   
  15.                 int index = 0;  
  16.                 int indexInc = rawScreen.bpp >> 3;  
  17.                 for (int y = 0; y < rawScreen.height; y++) {  
  18.                     for (int x = 0; x < rawScreen.width; x++, index += indexInc) {  
  19.                         int value = rawScreen.getARGB(index);  
  20.                         if (landscape)  
  21.                             image.setRGB(y, rawScreen.width - x - 1, value);  
  22.                         else  
  23.                             image.setRGB(x, y, value);  
  24.                     }  
  25.                 }     
  26.                 ImageIO.write((RenderedImage)image,"PNG",new File("D:/temp.jpg"));  
  27.             }  


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值