Android FlashLight教程
时间:2020-02-23 14:28:55 来源:igfitidea点击:
在本教程中,我们将在应用程序中实现FlashLight功能。
Android手电筒
如今,每部智能手机都可通过相机闪光灯使用手电筒功能/手电筒。
在下一部分中,我们将使用Camera2 API切换手电筒。
自Android API 25起,不推荐使用Camera API。
此外,我们还将介绍如何创建闪烁的手电筒。
让我们开始创建一个新的Android Studio项目。
代码
下面给出了activity_main.xml布局文件的代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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">
<Button
android:id="@+id/btnFlashLightToggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FLASHLIGHT OFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
<Button
android:id="@+id/btnBlinkFlashLight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="BLINK FLASHLIGHT"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnFlashLightToggle"
</android.support.constraint.ConstraintLayout>
将以下内容添加到列表标记中的AndroidManifest.xml文件中。
<uses-permission android:name="android.permission.CAMERA" <uses-feature android:name="android.hardware.camera"
MainActivity.java类的代码如下:
package com.theitroad.androidflashlight;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button btnFlashLight, btnBlinkFlashLight;
private static final int CAMERA_REQUEST = 123;
boolean hasCameraFlash = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.CAMERA}, CAMERA_REQUEST);
hasCameraFlash = getPackageManager().
hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
btnFlashLight = findViewById(R.id.btnFlashLightToggle);
btnBlinkFlashLight = findViewById(R.id.btnBlinkFlashLight);
btnFlashLight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (hasCameraFlash) {
if (btnFlashLight.getText().toString().contains("ON")) {
btnFlashLight.setText("FLASHLIGHT OFF");
btnBlinkFlashLight.setText("BLINK FLASHLIGHT OFF");
flashLightOff();
} else {
btnBlinkFlashLight.setText("BLINK FLASHLIGHT ON");
btnFlashLight.setText("FLASHLIGHT ON");
flashLightOn();
}
} else {
Toast.makeText(MainActivity.this, "No flash available on your device",
Toast.LENGTH_SHORT).show();
}
}
});
btnBlinkFlashLight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(btnFlashLight.getText().toString().contains("ON"))
{
blinkFlash();
}
else{
Toast.makeText(MainActivity.this, "Press the above button first.",
Toast.LENGTH_SHORT).show();
}
}
});
}
private void flashLightOn() {
CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
String cameraId = cameraManager.getCameraIdList()[0];
cameraManager.setTorchMode(cameraId, true);
} catch (CameraAccessException e) {
}
}
private void flashLightOff() {
CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
String cameraId = cameraManager.getCameraIdList()[0];
cameraManager.setTorchMode(cameraId, false);
} catch (CameraAccessException e) {
}
}
private void blinkFlash()
{
CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
String myString = "0101010101";
long blinkDelay = 50; //Delay in ms
for (int i = 0; i < myString.length(); i++) {
if (myString.charAt(i) == '0') {
try {
String cameraId = cameraManager.getCameraIdList()[0];
cameraManager.setTorchMode(cameraId, true);
} catch (CameraAccessException e) {
}
} else {
try {
String cameraId = cameraManager.getCameraIdList()[0];
cameraManager.setTorchMode(cameraId, false);
} catch (CameraAccessException e) {
}
}
try {
Thread.sleep(blinkDelay);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case CAMERA_REQUEST:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
hasCameraFlash = getPackageManager().
hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
} else {
btnFlashLight.setEnabled(false);
btnBlinkFlashLight.setEnabled(false);
Toast.makeText(MainActivity.this, "Permission Denied for the Camera", Toast.LENGTH_SHORT).show();
}
break;
}
}
}
在上面的代码中,我们首先要求相机权限。
完成后,我们将检查相机闪光灯功能是否存在。
btnFlashLight用作手电筒切换按钮。
我们通过从" CameraManager"上的相机ID列表中获取第一个元素来启动手电筒。
在该ID上,我们可以将手电筒模式设置为true以启用手电筒,或者将其设置为false禁用手电筒。
如何使手电筒闪烁?
我们使用由二进制数组成的字符串。
我们遍历字符串,并在每次迭代时切换手电筒,同时使线程休眠一会儿以带来闪烁效果。
为了使其不断闪烁,请使用无限循环,该循环在某种情况下停止。
以上应用程序的输出是所需的。
由于屏幕快照是硬件中的内容,因此我们已将其省略。

