在 Android 中如何將檢視轉換為 Bitmap 而不顯示它?
介紹
在 Android 應用程式中,我們經常需要從檢視獲取 Bitmap 並將其用於應用程式中。Bitmap 通常是透過將檢視傳遞給它來建立的。本文將介紹如何在 Android 中將檢視轉換為 Bitmap 而不顯示它。
實現
我們將建立一個簡單的應用程式,其中我們將顯示一個 TextView、一個 Button 和一個 ImageView。在 TextView 中,我們將顯示應用程式的標題。然後,我們將新增一個 Button,單擊此 Button 將從檢視生成 Bitmap。在 ImageView 中,我們將顯示生成的 Bitmap。我們將從 TextView 生成 Bitmap。
步驟 1:在 Android Studio 中建立一個新專案
導航到 Android Studio,如下面的螢幕截圖所示。在下面的螢幕截圖中,單擊“新建專案”以建立一個新的 Android Studio 專案。
單擊“新建專案”後,您將看到下面的螢幕。
在此螢幕中,我們只需選擇“空活動”並單擊“下一步”。單擊“下一步”後,您將看到下面的螢幕。
在此螢幕中,我們只需指定專案名稱。然後包名將自動生成。
注意 - 請確保選擇 Java 作為語言。
指定所有詳細資訊後,單擊“完成”以建立一個新的 Android Studio 專案。
專案建立完成後,我們將看到兩個開啟的檔案,即 activity_main.xml 和 MainActivity.java 檔案。
步驟 3:使用 activity_main.xml
導航到 activity_main.xml。如果此檔案不可見,則要開啟此檔案。在左側窗格中導航到 app>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:id="@+id/idRLLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<!-- on below line creating a text view for displaying a heading-->
<TextView
android:id="@+id/idTVHeading"
android:layout_width="400dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:layout_marginStart="10dp"
android:layout_marginTop="50dp"
android:layout_marginEnd="10dp"
android:gravity="center"
android:padding="4dp"
android:text="Converting a view to Bitmap without displaying it in Android"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold" />
<!-- on below line creating a button to display a bitmap -->
<Button
android:id="@+id/idBtnLoadBitmap"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/idTVHeading"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:text="Load Bitmap from Image"
android:textAllCaps="false" />
<!-- on below line creating a button to check wifi status-->
<ImageView
android:id="@+id/idIVImage"
android:layout_width="400dp"
android:layout_height="400dp"
android:layout_below="@id/idBtnLoadBitmap"
android:layout_centerHorizontal="true"
android:layout_margin="10dp" />
</RelativeLayout>
說明 - 在上面的程式碼中,我們建立了一個 RelativeLayout 作為根佈局,並在其中建立了一個簡單的 TextView,用於顯示應用程式的標題。在此 TextView 之後,我們建立了一個 Button,我們將使用它從標題 TextView 生成 Bitmap。在此 Button 之後,我們建立了一個 ImageView。在此 ImageView 中,我們將顯示從 TextView 生成的 Bitmap。
步驟 4:使用 MainActivity.java 檔案
導航到 MainActivity.java。如果此檔案不可見,則要開啟此檔案。在左側窗格中導航到 app>res>layout>MainActivity.java 以開啟此檔案。開啟此檔案後,將以下程式碼新增到其中。程式碼中添加了註釋以詳細瞭解。
package com.example.androidjavaapp;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// on below line displaying toast message
Toast.makeText(this, "On Create Method called.", Toast.LENGTH_SHORT).show();
}
// on below line calling on start method
@Override
protected void onStart() {
super.onStart();
// displaying toast message on below line.
Toast.makeText(this, "On Start Method called.", Toast.LENGTH_SHORT).show();
}
// on below line calling on start method
@Override
protected void onStop() {
super.onStop();
// displaying toast message on below line.
Toast.makeText(this, "On Stop Method called.", Toast.LENGTH_SHORT).show();
}
// on below line calling on start method
@Override
protected void onRestart() {
super.onRestart();
// displaying toast message on below line.
Toast.makeText(this, "On Restart Method called.", Toast.LENGTH_SHORT).show();
}
// on below line calling on start method
@Override
protected void onResume() {
super.onResume();
// displaying toast message on below line.
Toast.makeText(this, "On Resume Method called.", Toast.LENGTH_SHORT).show();
}
// on below line calling on start method
@Override
protected void onPause() {
super.onPause();
// displaying toast message on below line.
Toast.makeText(this, "On Pause Method called.", Toast.LENGTH_SHORT).show();
}
// on below line calling on start method
@Override
protected void onDestroy() {
super.onDestroy();
// displaying toast message on below line.
Toast.makeText(this, "On Destory Method called.", Toast.LENGTH_SHORT).show();
}
}
說明 - 在上面的程式碼中,我們為 ImageView、Button 和 TextView 建立了一個變數。然後,我們將看到一個 onCreate 方法。此方法用於顯示我們建立的佈局檔案。在此 onCreate 方法中,我們正在初始化我們建立的變數。然後,我們為建立的 Button 新增點選監聽器。在 Button 的點選事件中,我們呼叫一個方法,該方法用於從標題 TextView 生成 Bitmap。我們從 loadBitmapFromView 方法生成此 Bitmap。此方法將獲取一個檢視,然後從中生成 Bitmap。我們將生成的此 Bitmap 顯示在 ImageView 中。
新增上述程式碼後,我們只需單擊頂部的綠色圖示即可在移動裝置上執行我們的應用程式。
注意 - 請確保已連線到您的真實裝置或模擬器。
輸出
在上面的示例應用程式中,單擊 Button 時,我們將透過傳遞 TextView 生成 Bitmap。我們將為標題 TextView 生成 Bitmap。現在,要檢查 Bitmap 是否已生成,我們將此 Bitmap 設定為 ImageView 以顯示它,以便我們可以驗證 Bitmap 是否已成功生成。
結論
在本文中,我們介紹瞭如何在 Android 應用程式中將檢視轉換為 Bitmap,而無需在 Android 應用程式中顯示它。
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP