拿到Samsung的7寸平板,是3.x系統(tǒng)忽肛,在狀態(tài)欄上多了個(gè)截屏按鈕,拿到Galaxy Notes 10.1烂斋,該功能更是不可刪除的添加在狀態(tài)欄屹逛,既然如此,我們今天在我們平板上也添加上該按鍵汛骂,實(shí)現(xiàn)簡(jiǎn)單的快捷截屏功能罕模,而不是按POWER和VOL-進(jìn)行。
在Android4.0源碼的frameworks/base/packages/SystemUI/res/drawable-mdpi/目錄下添加ic_sysbar_screenshot.png文件帘瞭,圖片如下:
該圖片的背景是透明的PNG文件淑掌。
接下來修改frameworks/base/packages/SystemUI/res/layout-sw600dp/status_bar.xml文件,在
android:layout_width="80dip"
android:layout_height="match_parent"
android:src="@drawable/ic_sysbar_screenshot"
android:contentDescription="@string/accessibility_screenshot"
systemui:glowBackground="@drawable/ic_sysbar_highlight"
/>
接下來修改frameworks/base/packages/SystemUI/res/values/strings.xml文件图张,添加如下內(nèi)容:
ScreenShot
至此锋拖,重新編譯燒錄源碼诈悍,在狀態(tài)欄上會(huì)多出截屏圖標(biāo)祸轮,接下來需要繼續(xù)實(shí)現(xiàn)截屏功能,查看了frameworks里的源碼侥钳,找到了frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java文件里有可參考的片斷适袜,故繼續(xù)修改frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/tablet/TabletStatusBar.java文件,相應(yīng)的添加如下內(nèi)容:
import android.os.Messenger;
import android.content.ComponentName;
import android.content.ServiceConnection;
在View mRecentButton;后面添加View mScreenshotButton;語(yǔ)句舷夺。
在mRecentButton.setOnClickListener(mOnClickListener);后添加如下內(nèi)容:
mScreenshotButton = mNavigationArea.findViewById(R.id.my_screenshot);
mScreenshotButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mHandler.postDelayed(mScreenshotChordLongPress,
ViewConfiguration.getGlobalActionKeyTimeout());
}
});
接下來在public int getStatusBarHeight() {前添加如下內(nèi)容:
private final Runnable mScreenshotChordLongPress = new Runnable() {
public void run() {
takeScreenshot();
}
};
final Object mScreenshotLock = new Object();
ServiceConnection mScreenshotConnection = null;
final Runnable mScreenshotTimeout = new Runnable() {
@Override public void run() {
synchronized (mScreenshotLock) {
if (mScreenshotConnection != null) {
mContext.unbindService(mScreenshotConnection);
mScreenshotConnection = null;
}
}
}
};
// Assume this is called from the Handler thread.
private void takeScreenshot() {
synchronized (mScreenshotLock) {
if (mScreenshotConnection != null) {
return;
}
ComponentName cn = new ComponentName("com.android.systemui",
"com.android.systemui.screenshot.TakeScreenshotService");
Intent intent = new Intent();
intent.setComponent(cn);
ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
synchronized (mScreenshotLock) {
if (mScreenshotConnection != this) {
return;
}
Messenger messenger = new Messenger(service);
Message msg = Message.obtain(null, 1);
final ServiceConnection myConn = this;
Handler h = new Handler(mHandler.getLooper()) {
@Override
public void handleMessage(Message msg) {
synchronized (mScreenshotLock) {
if (mScreenshotConnection == myConn) {
mContext.unbindService(mScreenshotConnection);
mScreenshotConnection = null;
mHandler.removeCallbacks(mScreenshotTimeout);
}
}
}
};
msg.replyTo = new Messenger(h);
msg.arg1 = msg.arg2 = 0;
try {
messenger.send(msg);
} catch (RemoteException e) {
}
}
}
@Override
public void onServiceDisconnected(ComponentName name) {}
};
if (mContext.bindService(intent, conn, Context.BIND_AUTO_CREATE)) {
mScreenshotConnection = conn;
mHandler.postDelayed(mScreenshotTimeout, 10000);
}
}
}
至此苦酱,再次編譯源碼并燒錄,截屏功能實(shí)現(xiàn)了给猾,現(xiàn)在還存有的是圖標(biāo)的狀態(tài)處理了疫萤,這部分就不說明了。
實(shí)現(xiàn)截屏功能時(shí)敢伸,4.0上既然已實(shí)現(xiàn)該功能扯饶,那么就直接找源碼,確認(rèn)哪兒有實(shí)現(xiàn),直接拿過來使用尾序,盡量拿來主義钓丰,但又不失學(xué)習(xí)性。