Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 自訂截圖功能:打造類似 QQ 的截圖體驗

編輯:關於 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 中實作,有興趣的朋友可以下載範例直接執行觀看效果。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved