Focus:
1, Surfaceview, OpenGL ES texture------camera capture image stream texture
2, style.xml configuration file. Set up information such as Untitled
<!--application theme. --<style name= "Apptheme" parent= "Appbasetheme" > <item name= "Android:windownotitle" >true</item& Gt <item name= "Android:windowfullscreen" >true</item> <item name= "Android:windowbackground" >@ Drawable/bg</item> </style>
3. Use framelayout layout,<include/> label to introduce sub-layout.
Main.xml
<p><framelayout xmlns:android= "<a target=_blank href=" Http://schemas.android.com/apk/res/android "> Http://schemas.android.com/apk/res/android</a> "xmlns:tools=" <a target=_blank href= "/http/ Schemas.android.com/tools ">http://schemas.android.com/tools</a>" android:layout_width= "Match_parent" android:layout_height= "Match_parent" android:background= "@drawable/bg" ><include layout= "@layout/mui_ Flashlight "/></p><p></framelayout></p>
Ui_flashlight.xml
<?xml version= "1.0" encoding= "Utf-8"? ><framelayout xmlns:android= "http// Schemas.android.com/apk/res/android " android:layout_width=" Match_parent " android:layout_height= "Match_parent" android:id= "@+id/mui_flashlight" > <ImageView android:id= "@+id/img_flashlight" android:layout_ Width= "Match_parent" android:layout_height= "Match_parent" android:src= "@drawable/flashlight" /> <imageview android:id= "@+id/img_flashlight_controller" andRoid:layout_width= "Match_parent" android:layout_height= "Match_ Parent " android:layout_gravity=" Bottom|center_horizontal " android:onclick= "Onclick_flashlight" /></FrameLayout>
4, the use of flash.xml file to achieve the gradient effect, and then the use of android:src= "@drawable/flashlight", introduced.
Flash.xml
<?xml version= "1.0" encoding= "Utf-8"? ><transition xmlns:android= "http://schemas.android.com/apk/res/ Android "> <item android:drawable=" @drawable/off "/> <item android:drawable=" @drawable/on "/></tran Sition>
5. Function code
Flashlight.java
public class myflashlight extends mybaseactivity { @Override protected void oncreate (bundle savedinstancestate) { super.oncreate (savedInstanceState); iview.settag (false); //gets the size of the screen point point = new point (); getwindowmanager (). Getdefaultdisplay (). GetSize (point); LayoutParams layoutParams = (Layoutparams) iviewcontroller.getlayoutparams (); //setting the range of hotspot areas layoutParams.height = point.y * 3/4; Layoutparams.width = point.x /3; iviewcontroller.setlayoutparams (LayoutParams); } public void onclick_flashlight (View view) { if (! Getpackagemanager (). Hassystemfeature (Packagemanager.feature_camera_flash)) { toast.maketext (this, "Current device No flash", toaSt. Length_short). Show (); return; } if (((Boolean) Iview.gettag ()) == false) { openflashlight (); } else { Closeflashlight (); } } private void openflashlight () { // Transitiondrawable is a special drawable object that enables the fading effect between two drawable resources transitiondrawable tdrawable = (transitiondrawable) iview.getdrawable () //switching time 200 ms Tdrawable.starttransition (+); iview.settag (True); try { //Open Camera mcamera = camera.open (); int texturid = 0; //Preview texture defaults to a value //camera gets the image stream texture mcamera.setpreviewtexture (new surfacetexture (Texturid)); //start preview mcamera.startpreview (); //using GetParameters () to get the default configuration parameters for camera mparameters = mcamera.getparameters (); //set Flash mode mparameters.setflashmode (Mparameters.flash_mode_torch); mcamera.setparameters ( mparameters); } catch (ioexception e) { e.printstacktrace (); } } private void Closeflashlight () { TransitionDrawable tDrawable2 = (transitiondrawable) Iview.getdrawable (); if ((Boolean) iview.gettag ()) { //Invert time Toggle tdrawable2.reversetransition (+); iview.settag (false); if (mcamera != null) { //use GetParameters () to get the default configuration parameters for camera mparameters = mcamera.getparameters (); //set Flash mode mparameters.setflashmode (ParametERs. Flash_mode_off); mcamera.setparameters (mparameters); //Stop preview mcamera.stoppreview (); //release Camera Object Mcamera.release (); //to recycle the garbage collector as soon as possible mCamera = null; } } } //exit program turn off Flash @Override protected void onpause () { super.onpause (); closeflashlight (); }}
Complete code See Attachment (Enter my profile to download): Myflashlight.zip
Useful Code Reference:
http://blog.csdn.net/dclchj/article/details/7421778
Http://www.cnblogs.com/over140/archive/2012/09/26/2611999.html
Flashlight Project Development A Flash