Android Development-----------Flashlight Improved version

Source: Internet
Author: User

On the basis of the previous

Under the Res folder: Add a Drawable/local_me.xml

Localme_cml

<selector xmlns:android= "Http://schemas.android.com/apk/res/android" >    <item android:state_checked= " True "        android:drawable=" @drawable/on "/> <!--pressed-to    <item android:drawable=" @drawable/off "/> <!--default/unchecked--></selector>


Main program:


Package Com.example.handlightactivity;import android.app.Activity;  Import Android.hardware.Camera;  Import Android.hardware.Camera.Parameters;  Import Android.os.Bundle;  Import android.os.Process;  Import Android.view.View;  Import Android.view.View.OnClickListener;  Import Android.view.WindowManager;    Import Android.widget.ToggleButton; /** * * @author Love * * Principle: * is by clicking the button, * when the button is clicked to open the flash. * Turn off the flash when the button is off * * * */public class Mainactivity extends Activity implements Onclicklistener {private Toggl        Ebutton ToggleButton;      Private Camera camera = Camera.open (); /** called when the activity is first created.          */@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);            Setcontentview (R.layout.activity_main);          ToggleButton = (ToggleButton) This.findviewbyid (R.id.togglebutton1);                    Togglebutton.setonclicklistener (this); Keep the screen GetWindow (). AddfLags (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);          } @Override public void OnClick (View v) {ToggleButton TB = (ToggleButton) v;          Camera.parameters param = camera.getparameters ();            if (!tb.ischecked ()) {//Open the camera flash!              Param.setflashmode (Camera.Parameters.FLASH_MODE_TORCH);  Togglebutton.setbackgroundcolor (0X30FFFFFF);        On open on.jpg tb.setbackgrounddrawable (getresources (). getdrawable (R.drawable.on));              }else{Param.setflashmode (Camera.Parameters.FLASH_MODE_OFF); Togglebutton.setbackgroundcolor (0xFFFFFFFF);        Off Open the Off.jpg tb.setbackgrounddrawable (Getresources (). getdrawable (R.drawable.off));      } camera.setparameters (param);  } @Override protected void OnPause () {//Camera.release ();          Process.killprocess (Process.mypid ());      Super.onpause (); }  }


:




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.