前几天这边一个小区断电,黑麻麻的,一只猫闯进寝室,把我吓哭,然后就果断写了个手电筒,下次断电,再来,我要吓死它。
我之前看到很多人都是直接调用闪光灯,其实大部分机型都不支持,我这个是用相机功能来实现的,大部分机型都支持。
MainActivity.class
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (isFlashSupported()) {
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new FlashlightFragment()).commit();
}
} else {
showNoFlashAlert();
}
}
private void showNoFlashAlert() {
new AlertDialog.Builder(this)
.setMessage(R.string.device_not_support_msg)
.setIcon(android.R.drawable.ic_dialog_alert).setTitle(R.string.device_not_support_title)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
finish();
}
}).create().show();
}
private boolean isFlashSupported() {
PackageManager pm = getPackageManager();
return pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
}
public static class FlashlightFragment extends Fragment implements SurfaceHolder.Callback {
public FlashlightFragment() { }
ImageButton btnSwitch;
ImageView bg;
private boolean isLighOn = false;
private Camera camera;
Camera.Parameters params;
@Override
public void onStart() {
super.onStart();
SurfaceView preview = (SurfaceView) getView().findViewById(R.id.PREVIEW);
SurfaceHolder mHolder = preview.getHolder();
mHolder.addCallback(this);
}
@Override
public void onPause() {
super.onPause();
turnOffFlash();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main_activity, container, false);
// flash switch button
btnSwitch = (ImageButton) view.findViewById(R.id.flashlight_button);
bg = (ImageView) view.findViewById(R.id.flashlight_bg);
// displaying button image
toggleBgImage();
// Switch button click event to toggle flash on/off
btnSwitch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isLighOn) {
turnOffFlash();
} else {
turnOnFlash();
}
}
});
return view;
}
// Turning On flash
private void turnOnFlash() {
if (!isLighOn) {
if (camera == null || params == null) {
return;
}
params = camera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
isLighOn = true;
toggleBgImage();
}
}
// Turning Off flash
private void turnOffFlash() {
if (isLighOn) {
if (camera == null || params == null) {
return;
}
params = camera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.stopPreview();
isLighOn = false;
toggleBgImage();
}
}
private void toggleBgImage() {
if (isLighOn) {
bg.setImageResource(R.mipmap.f_on);
} else {
bg.setImageResource(R.mipmap.f_off);
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (camera != null) {
camera.stopPreview();
camera.setPreviewCallback(null);
camera.release();
camera = null;
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (camera == null) {
camera = Camera.open();
params = camera.getParameters();
try {
camera.setPreviewDisplay(holder);
} catch (IOException e) {
camera.release();
camera = null;
}
}
turnOnFlash();
}
}
}
fragment_main_activity.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<SurfaceView
android:id="@+id/PREVIEW"
android:layout_width="1dp"
android:layout_height="1dp"/>
<ImageView
android:id="@+id/flashlight_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/f_on"
android:scaleType="centerCrop"
/>
<ImageButton
android:id="@+id/flashlight_button"
android:layout_width="100dp"
android:layout_height="200dp"
android:layout_centerInParent="true"
android:background="@android:color/transparent"
android:text="fuck"/>
</RelativeLayout>
activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.liujinghuiuncleapp.app.tool.flashlight" >
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.flash" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:theme="@style/AppTheme"
android:label="@string/app_name" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
以上所述就是本文的全部内容了,希望大家能够喜欢。
您可能感兴趣的文章:Android 7.0 手电筒控制实现android通过led实现手电筒功能Android实现简单手电筒功能Android实现手电筒电源键关闭功能Android开启闪光灯的方法 Android打开手电筒功能Android studio编写简单的手电筒APPAndroid Camera开发手电筒功能Android中编写简单的手电筒小应用的实例教程Android 开启闪光灯做手电筒的详解Android手电筒兼容各个手机与版本