文章目錄
- MyTool 工具界面:
- ♦ 回顧
- ♦ 設定 activity_my_tool.xml 界面
- ♦ 編寫 MyToolActivity 實作功能界面跳轉
- ♦ 建立 activity_flash_light.xml 手電筒界面
- ♦ 編寫 FlashLightActivity 實作功能
- ♦ 修改AndroidManifest.xml檔案授權
- ♦ 測試
MyTool 工具界面:
♦ 回顧
通過上一節的學習,我們實作了工具界面百度、撥号功能的實作,如下圖所示。今天,要在這個界面中實作一個新的功能手電筒。
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiI0gTMx81dsQWZ4lmZf1GLlpXazVmcvwFciV2dsQXYtJ3bm9CX9s2RkBnVHFmb1clWvB3MaVnRtp1XlBXe0xCMy81dvRWYoNHLwEzX5xCMx8FesU2cfdGLwMzX0xiRGZkRGZ0Xy9GbvNGLpZTY1EmMZVDUSFTU4VFRR9Fd4VGdsYTMfVmepNHLrJXYtJXZ0F2dvwVZnFWbp1zczV2YvJHctM3cv1Ce-cmbw5yM0ADNxIGOwADZ2U2MiVjZyYzXyATO1kDMyEzLchDMyIDMy8CXn9Gbi9CXzV2Zh1WavwVbvNmLvR3YxUjLyM3Lc9CX6MHc0RHaiojIsJye.png)
傳回頂部
♦ 設定 activity_my_tool.xml 界面
- 添加一個新的
:手電筒按鈕元件
<Button
android:id="@+id/flashlight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="39dp"
android:layout_marginLeft="39dp"
android:layout_marginTop="250dp"
android:text="手電筒"
android:textSize="16dp"/>
傳回頂部
♦ 編寫 MyToolActivity 實作功能界面跳轉
public class MyToolActivity extends AppCompatActivity {
TextView welcome;
Button net, call, quick_call,flashlight;
//權限請求:載入界面、特定情況觸發
//定義權限 --- 動态權限,申請哪一個就啟動哪一個,通常為常量
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.CALL_PHONE
};
//請求狀态碼
private static int REQUEST_PERMISSION_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_tool);
.........................
// 擷取工具元件
net = findViewById(R.id.net);
call = findViewById(R.id.call);
quick_call = findViewById(R.id.quick_call);
flashlight = findViewById(R.id.flashlight);
.........................
// 手電筒界面跳轉
flashlight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 建立頁面跳轉
Intent intent = new Intent();
intent.setClass(MyToolActivity.this,FlashLightActivity.class);
startActivity(intent);
}
});
}
}
傳回頂部
♦ 建立 activity_flash_light.xml 手電筒界面
- 基本布局、背景
- 添加 ImageButton 元件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.myapplication.FlashLightActivity"
android:background="@drawable/main_page">
<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="76dp"
android:text="歡迎使用手電筒!"
android:textStyle="bold"
android:textColor="@android:color/white"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.511"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="@+id/imageButton"
android:layout_width="255dp"
android:layout_height="247dp"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:layout_marginTop="218dp"
android:scaleType="centerInside"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.644"
app:srcCompat="@drawable/off" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="195dp"
android:text="已關閉!"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="@android:color/white"
/>
<SurfaceView
android:id="@+id/surfaceView"
android:layout_width="149dp"
android:layout_height="109dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="124dp"
android:layout_marginBottom="7dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
-
。由于手電筒(閃光燈)常與照相機放在一起使用,不妨這裡我們添加一個SurfaceView用于模拟照相功能
傳回頂部
♦ 編寫 FlashLightActivity 實作功能
package com.example.myapplication;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.os.Build;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import java.io.IOException;
public class FlashLightActivity extends AppCompatActivity {
// 定義對象
ImageButton flashlight;
TextView showNow;
boolean isFlag = false;
// 建立相機對象
Camera cc;
Camera.Parameters pp;
SurfaceView sv;
SurfaceHolder sh;
//權限請求:載入界面、特定情況觸發
//定義權限 --- 動态權限,申請哪一個就啟動哪一個,通常為常量
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.CAMERA
};
//請求狀态碼
private static int REQUEST_PERMISSION_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flash_light);
if(Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP){
// 檢查目前的權限
if (ActivityCompat.checkSelfPermission(FlashLightActivity.this, Manifest.permission.CAMERA) !=
PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(FlashLightActivity.this,PERMISSIONS_STORAGE,REQUEST_PERMISSION_CODE);
}
}
flashlight = findViewById(R.id.imageButton);
showNow = findViewById(R.id.textView8);
sv = findViewById(R.id.surfaceView);
cc = Camera.open();
pp = cc.getParameters();
sh = sv.getHolder();
sh.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(@NonNull SurfaceHolder holder) {
try {
cc.setPreviewDisplay(sh);
cc.startPreview();
} catch (IOException e) {
e.printStackTrace();
cc.release();
}
}
@Override
public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
}
});
flashlight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isFlag==false){
flashlight.setImageDrawable(getResources().getDrawable(R.drawable.on));
showNow.setText("已開啟!");
pp.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
cc.setParameters(pp);
isFlag = true;
} else {
flashlight.setImageDrawable(getResources().getDrawable(R.drawable.off));
showNow.setText("已關閉!");
pp.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
cc.setParameters(pp);
isFlag = false;
}
}
});
}
}
傳回頂部
♦ 修改AndroidManifest.xml檔案授權
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication_test">
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.CAMERA" /> 添加攝像權限
<uses-permission android:name="android.permission.FLASHLIGHT" /> 添加電筒權限
<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="com.example.myapplication.MyToolActivity"></activity>
<activity android:name="com.example.myapplication.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
傳回頂部