如何在 Android 應用中實現 SurfaceView 透明化?
本例演示如何在 Android 應用程式中實現 SurfaceView 透明化。
步驟 1 − 在 Android Studio 中新建一個專案,轉至檔案 ⇒ 新建專案並填寫所有必需資訊以新建一個專案。
步驟 2 − 將以下程式碼新增到 res/layout/activity_main.xml。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://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="fill_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>
步驟 3 − 將以下程式碼新增到 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); } }
步驟 4 − 將以下程式碼新增到 Manifests/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://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>
我們嘗試執行你的應用程式。我假設你已經將一臺真正的安卓手機裝置與你的電腦相連線了。要從安卓工作室執行該應用,請開啟專案其中一個 activity 檔案並點選工具欄上的執行 圖示。選擇你的移動裝置作為選項,然後檢視你的移動裝置,其上將會顯示預設螢幕 −
點選 此處 下載專案程式碼。
廣告