原文地址:http://blog.csdn.net/hk_256/article/details/7306590 ,轉載請註明出處
一、基本介紹
在Android 4.0 之前,Android手機上如果要使用截屏功能,只能通過Root手機,且使用第3方軟體來實現截屏功能。
Android4.0中,系統內建了截屏功能,使用方法是音量下(VOLUME_DOWN)鍵+電源(Power)鍵。
在同步選取2鍵並保持0.5s左右後,會聽到哢嚓一聲響聲,並彈出如下的一個浮動動畫,顯示效果。
二、代碼調用流程
以模組來劃分的話,功能的代碼會依次調用Policy,SystemUI,Surface相關的代碼,具體流程如下流程圖所示
Policy(PhoneWindowManager.java):在此處完成Key的捕獲,當VOLUME_DOWN和Power鍵被幾乎同步選取後,向SystemUI發送Message開始。
SystemUI(TakeScreenshotService.java和GlobalScreenshot.java):收到來自Client端的截屏請求後,開始調用Surface的API截屏,並將截取到的圖片通過WindowManager以浮動視窗的形式顯示給使用者查看。
Surface(Surface.java和android_view_Surface.cpp):Framework層的Surface.java只是提供一個native方法,實際實現在JNI處的android_view_Surface.cpp中的doScreenshot(...)方法。
三、App端如何使用截屏功能
以目前代碼情況看,Surface.java中的screenshot方法是有@hide標記的,即在預設的SDK中是沒有此方法的,暫不支援App端直接使用。
因為只是@hide標記,如果App要使用,當然也是有方法的,但會和手機ROM有依賴性。我所使用的方法是,在Android源碼環境下進行編譯,為app賦予system的share uid和platform的簽名,然後就可以在4.0的手機中使用App來截屏了。
關鍵步驟:
1) 在AndroidManifest.xml中加入android:sharedUserId="android.uid.system" 屬性
2)在Android.mk中加入platform簽名屬性,並在源碼環境下編譯。或者將相關jar包引入到Eclipse中做第3方庫引用,並將產生的apk重新打上platform簽名
註:
在SurfceFlinger.cpp的onTransact方法中,有對截屏的操作進行許可權認證,所以需要為app使用system的shareUserId。
另:從此處代碼看,使用android.permission.READ_FRAME_BUFFER的permission應該也可以使用,但測試時通過這種方式未實現,可能哪裡操作不對,也有可能Google還只是預留給後續改進而已。
部落格地址:http://blog.csdn.net/hk_256 ,轉載請註明出處
附錄:App的原始碼檔案:
1. Activity檔案
package com.arvinhe.testscreenshot;import android.app.Activity;import android.content.Context;import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.Matrix;import android.os.Bundle;import android.util.DisplayMetrics;import android.view.Display;import android.view.Surface;import android.view.View;import android.view.WindowManager;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;public class TestScreenShotActivity extends Activity implements OnClickListener{ private ImageView img_display;private Button bt_screenshot; private Display mDisplay; private DisplayMetrics mDisplayMetrics; private Matrix mDisplayMatrix; private Bitmap mScreenBitmap;private WindowManager mWindowManager; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); bt_screenshot = (Button)findViewById(R.id.bt_screenshot); img_display = (ImageView)findViewById(R.id.img_display); bt_screenshot.setOnClickListener(this); mDisplayMatrix = new Matrix(); mWindowManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE); mDisplay = mWindowManager.getDefaultDisplay(); mDisplayMetrics = new DisplayMetrics(); mDisplay.getRealMetrics(mDisplayMetrics); }@Overridepublic void onClick(View v) {if(v.equals(bt_screenshot)){mDisplay.getRealMetrics(mDisplayMetrics); float[] dims = {mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels}; float degrees = getDegreesForRotation(mDisplay.getRotation()); boolean requiresRotation = (degrees > 0); if (requiresRotation) { // Get the dimensions of the device in its native orientation mDisplayMatrix.reset(); mDisplayMatrix.preRotate(-degrees); mDisplayMatrix.mapPoints(dims); dims[0] = Math.abs(dims[0]); dims[1] = Math.abs(dims[1]); } mScreenBitmap = Surface.screenshot((int) dims[0], (int) dims[1]); if (requiresRotation) { // Rotate the screenshot to the current orientation Bitmap ss = Bitmap.createBitmap(mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(ss); c.translate(ss.getWidth() / 2, ss.getHeight() / 2); c.rotate(degrees); c.translate(-dims[0] / 2, -dims[1] / 2); c.drawBitmap(mScreenBitmap, 0, 0, null); c.setBitmap(null); mScreenBitmap = ss; } // If we couldn't take the screenshot, notify the user if (mScreenBitmap == null) { return; } // Optimizations mScreenBitmap.setHasAlpha(false); mScreenBitmap.prepareToDraw(); img_display.setImageBitmap(mScreenBitmap);}}/** * @return the current display rotation in degrees */ private float getDegreesForRotation(int value) { switch (value) { case Surface.ROTATION_90: return 360f - 90f; case Surface.ROTATION_180: return 360f - 180f; case Surface.ROTATION_270: return 360f - 270f; } return 0f; }}
2. AndroidManifest.xml檔案
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.arvinhe.testscreenshot" android:versionCode="1" android:versionName="1.0" android:sharedUserId="android.uid.system"> <uses-sdk android:minSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".TestScreenShotActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
3. Layout檔案
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/bt_screenshot" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Screen Shot" /> <ImageView android:id="@+id/img_display" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/ic_launcher"/></LinearLayout>
附錄:App運行效果