Android 通用型手电筒代码
程序员文章站
2022-06-04 12:13:55
前几天这边一个小区断电,黑麻麻的,一只猫闯进寝室,把我吓哭,然后就果断写了个手电筒,下次断电,再来,我要吓死它。
我之前看到很多人都是直接调用闪光灯,其实大部分机型都不支...
前几天这边一个小区断电,黑麻麻的,一只猫闯进寝室,把我吓哭,然后就果断写了个手电筒,下次断电,再来,我要吓死它。
我之前看到很多人都是直接调用闪光灯,其实大部分机型都不支持,我这个是用相机功能来实现的,大部分机型都支持。
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实现隐藏状态栏和标题栏