Speaking of mobile phone flashlight function, many people are directly call the Flash, and this article to introduce you to the camera function to achieve, there is a need for small partners to refer to.
A few days ago this side of a community power, black Ma Ma, a cat broke into the dormitory, frighten me into tears, and then decisively wrote a flashlight, the next power outage, again, I want to frighten it.
I saw a lot of people are directly call the flash, in fact, most of the models are not supported, I this is to use the camera function to achieve, most of the models are supported.
Mainactivity.class
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5, 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 11 9 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148-149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166-167 |
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.dism ISS (); Finish (); ). Create (). Show (); } Private Boolean isflashsupported () {Packagemanager pm = Getpackagemanager (); return Pm.hassystemfeature (Package Manager.feature_camera_flash); } public static class FlashlightfraGment extends Fragment implements Surfaceholder.callback { public flashlightfragment () {} ImageButton BTNSW Itch; 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 (La Youtinflater 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 (); CA Mera = null; } @Override public void surfacecreated (Surfaceholder holder) {if (camera = = null) {camera = Camera.open (); para ms = Camera.getparameters (); try {camera.setpreviewdisplay (holder);} catch (IOException e) {camera.release (); camera = null;}} Turnonflash (); } } } |
Fragment_main_activity.xml
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27-28 |
<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
?
1 2 3 4 5 |
<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
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmln S: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> |
The above is the entire contents of this article, I hope you can enjoy.