編輯:關於 Android 程式設計
由於公司業務需求,需要對螢幕進行截圖,但系統自帶的截圖功能遠遠無法滿足專案的功能需求。我們正在開發一款畫板軟體,希望做到像 QQ 那樣,截圖之後還能看到自訂的工具列,例如畫筆、按鈕等等。Android 自帶的功能非常簡單,透過 Intent 隱式調用即可,但它是系統應用,介面固定、無法定制修改。
實作方式有很多種,以下記錄我採用的思路:重寫一個 View 元件,在 onDraw 中只負責繪製圖形(包括半透明的四個矩形、亮框矩形,以及亮框上的四個小圓點),而 onTouch 方法則不斷修改亮框的座標點,然後重新繪製。
效果圖:

我把這個圖形分解成下面這個形狀,在 onTouch 中不斷繪製矩形與圓點。

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas); // 不重寫圖形無法出現
if (mBitmap != null) {
canvas.drawBitmap(mBitmap, iconLeft, iconTop, p_picture); // 畫高亮的邊界
drawRect(canvas);
if (isDown) drawCircle(canvas);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
float x = event.getX();
float y = event.getY();
switch (action) {
case MotionEvent.ACTION_DOWN:
startX = x;
startY = y;
// 判斷是在矩形的外邊還是裡面(判斷是移動還是縮放)
if (x > lightRect.left + OFFSET && x < lightRect.right - OFFSET
&& y > lightRect.top + OFFSET && y < lightRect.bottom - OFFSET) {
isMove = true; // 移動的狀態
isScale = false;
} else if (x < lightRect.left - OFFSET || y < lightRect.top - OFFSET
|| x > lightRect.right + OFFSET || y > lightRect.bottom + OFFSET) {
isMove = false;
isScale = false;
} else {
isMove = false;
isScale = true; // 縮放
point = getScalePoint(startX, startY);
}
if (!isScale) isDown = false;
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
isDown = true;
break;
case MotionEvent.ACTION_MOVE:
if (isMove) {
float dx = x - startX;
float dy = y - startY;
moveLightRect(dx, dy);
startX = x;
startY = y;
isDown = false;
}
if (isScale) {
float dx = x - startX;
float dy = y - startY;
resetLightRect(dx, dy);
startX = x;
startY = y;
}
break;
default:
break;
}
invalidate();
return true;
}
public Bitmap getBitmap() {
int x = (int) (lightRect.left - iconLeft);
int y = (int) (lightRect.top - iconTop);
int w = lightRect.right - lightRect.left;
int h = lightRect.bottom - lightRect.top;
Bitmap bitmap = Bitmap.createBitmap(mBitmap, x, y, w, h);
return bitmap;
}
補充:以上只是一個能實現圖形擷取的 View。若要加入自訂按鈕,可再搭配一個版面檔把按鈕佈置進去,簡單範例如下:
<?xml version="1.0" encoding="utf-8"?>
<frameLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<com.example.imagedemo.ImageTailor
android:id="@+id/tailor"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_gravity="bottom" >
<Button
android:id="@+id/complete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="完成" />
<Button
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="取消" />
</LinearLayout>
</frameLayout>
關鍵類別與方法主要在 ImageTailor.java 中實作,有興趣的朋友可以下載範例直接執行觀看效果。

Android 源碼系列(三):從安全視角深入理解 BroadcastReceiver(下)
在前一篇文章中,我們結合實驗討論了 BroadcastReceiver 在實際開發中可能面臨的安全問題,並給出了相應的優化方案。其中,官方 v4 包提供的 LocalBr
Android Studio 專案提交到 GitHub 的完整流程
Git 與 GitHub 的關係 Git 是分散式版本控制系統,適合管理從小型到大型專案的歷史變更。GitHub 則是提供 Git 服務的網站,讓開發者不必自行架設伺服器
Jenkins + Gradle 實踐:Android 持續集成與多渠道打包自動化
為什麼要談持續集成 先看一個常見場景:Android 工程師小李打出十幾個渠道包,丟給運維小胡,小胡再一個一個上傳到各應用市場送審。 仔細想,小李這份工有任何價值嗎?它打
Android TabActivity 用法詳解:標籤頁布局與切換
TabActivity 是什麼TabActivity 繼承自 Activity,主要用來在同一畫面中容納多個分頁。透過頂部的標籤欄,使用者可以在不同頁面之間切換。它適合用