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

Android 自定義相機界面的實現思路與代碼

編輯:關於 Android 程式設計

本文分享如何自定義一個相機界面,重點包括用 layer-list 實現圓形拍照按鈕,以及相機預覽界面的佈局與核心控件。

一、用 layer-list 實現圓形拍照按鈕

自定義相機界面時,圓形的拍照按鈕不必依賴美工切圖,用代碼就能實現。最常用的方式是用 layer-list 做圖層疊加。按照規範,這類自定義 drawable 應放在 drawable 文件夾中,我們將其命名為 btn_take_photo.xml。

提示:drawable 文件夾用來存放自定義的 drawable 資源,可以把它理解為自己寫的背景樣式。

思路是:先畫一個白色橢圓作為外圈,再疊一個內縮的藍色橢圓作為內芯,最後加一個描邊橢圓修飾邊緣。android:shape 可取 rectangle、oval、line、ring,默認為矩形。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="https://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="oval">
            <solid android:color="@color/white" />
        </shape>
    </item>
    <item
        android:bottom="6dp"
        android:left="6dp"
        android:right="6dp"
        android:top="6dp">
        <shape android:shape="oval">
            <solid android:color="@color/blue" />
        </shape>
    </item>
    <item>
        <shape android:shape="oval">
            <stroke
                android:width="1dp"
                android:color="@color/blue"
                android:dashWidth="0dp" />
        </shape>
    </item>
</layer-list>

二、相機界面的布局

界面代碼 activity_take_photo.xml 如下。這裡用 SurfaceView 承載相機預覽——凡是涉及相機或視頻預覽的場景都需要它:

<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff">

    <!-- 顯示預覽圖形 -->
    <SurfaceView
        android:id="@+id/surfaceView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <RelativeLayout
        android:id="@+id/buttonLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/pic">

        <RelativeLayout
            android:id="@+id/panel_take_photo"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@color/white"
            android:gravity="center_vertical"
            android:padding="2dp">

            <Button
                android:id="@+id/btn_take_photo"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_centerHorizontal="true"
                android:background="@drawable/btn_take_photo" />

            <ImageView
                android:id="@+id/iv_album"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:layout_marginLeft="20dp"
                android:padding="5dp"
                android:src="@drawable/camera_library" />

            <ImageView
                android:id="@+id/title_btn_black"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="20dp"
                android:padding="5dp"
                android:src="@drawable/camera_back" />
        </RelativeLayout>

        <!-- 自定義的標題欄 -->
        <RelativeLayout
            android:id="@+id/camera_top"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_alignParentTop="true"
            android:background="@color/black">

            <ImageView
                android:id="@+id/btn_black"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_alignParentLeft="true"
                android:padding="10dp"
                android:src="@drawable/back" />

            <ImageView
                android:id="@+id/btn_change"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_alignParentRight="true"
                android:padding="10dp"
                android:src="@drawable/camera_flip" />
        </RelativeLayout>

        <!-- 自定義的 CameraGrid(取景框輔助線)-->
        <org.personality.camera.ui.view.CameraGrid
            android:id="@+id/masking"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@id/panel_take_photo"
            android:layout_alignParentTop="true" />

        <View
            android:id="@+id/focus_index"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_above="@id/panel_take_photo"
            android:background="@drawable/cam_focus"
            android:visibility="invisible" />
    </RelativeLayout>
</FrameLayout>

三、自定義 CameraGrid(取景井字線)

下面這個自定義 View 用於繪製相機的九宮格輔助線:

public class CameraGrid extends View {
    private Paint mPaint;

    public CameraGrid(Context context) {
        this(context, null);
    }

    public CameraGrid(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setColor(Color.WHITE);
        mPaint.setAlpha(120);
        mPaint.setStrokeWidth(1f);
    }

    private boolean showGrid = true;

    public boolean isShowGrid() {
        return showGrid;
    }

    public void setShowGrid(boolean showGrid) {
        this.showGrid = showGrid;
    }
}

四、小結

本文重點演示了兩個可復用的技巧:用 layer-list 疊加實現圓形按鈕,以及用 SurfaceView + FrameLayout 搭建相機預覽界面。相機的實際拍照、自定義 CameraGrid 的 onDraw 繪製、焦點框動畫等,都可以在此基礎上繼續完善。真正的相機能力(Camera / Camera2)涉及大量生命周期與資源管理,項目中通常會再做一層封裝。

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