Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Cách làm SurfaceView trong suốt (transparent) trong ứng dụng Android

Giới thiệu

Ví dụ này minh họa cách làm SurfaceView trong suốt để có thể hiển thị nội dung đè lên trên (overlay) như hình ảnh, text, hoặc điều khiển camera preview tùy chỉnh.

Các bước thực hiện

Bước 1: Tạo dự án mới

Mở Android Studio, chọn File → New Project.

Bước 2: Thiết kế giao diện (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <SurfaceView
        android:id="@+id/cameraSurface"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/IV_MASK"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/mask"
        android:background="@android:color/transparent" />

</RelativeLayout>

Bước 3: Viết code logic (src/MainActivity.java)

package com.app.sample;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Quan trọng: Set SurfaceView trong suốt
        SurfaceView surfaceView = findViewById(R.id.cameraSurface);
        surfaceView.setZOrderOnTop(true); // Đặt поверх các view khác
        surfaceView.getHolder().setFormat(PixelFormat.TRANSPARENT); // Định dạng trong suốt
    }
}

Bước 4: Cấu hình Manifest (Manifests/AndroidManifest.xml)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.app.sample">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

Chạy ứng dụng

Kết nối thiết bị, nhấn Run. SurfaceView sẽ trong suốt, cho thấy ImageView đè lên trên.

Cách làm SurfaceView trong suốt (transparent) trong ứng dụng Android

Giải thích kỹ thuật

Hai dòng code quan trọng trong onCreate:

  1. surfaceView.setZOrderOnTop(true): Đặt SurfaceView ở tầng Z cao nhất, поверх cửa sổ Activity.
  2. surfaceView.getHolder().setFormat(PixelFormat.TRANSPARENT): Yêu cầu bộ đệm (buffer) của SurfaceView có kênh alpha (trong suốt).

Lưu ý quan trọng

  • SurfaceView trong suốt không hoạt động tốt trên một số thiết bị cũ hoặc một số GPU driver.
  • Khi dùng Camera2 API, nên dùng TextureView thay vì SurfaceView vì TextureView hỗ trợ trong suốt, xoay, scale tự nhiên hơn.
  • Nếu chỉ cần overlay UI đơn giản, dùng FrameLayout hoặc ConstraintLayout đặt View thường lên trên SurfaceView (SurfaceView không cần transparent).