Android development ----------- improved flashlight version, android -----------

Source: Internet
Author: User

Android development ----------- improved flashlight version, android -----------

Based on the previous

Add a drawable/local_me.xml file under the res folder.

Localme_cmc

<selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_checked="true"        android:drawable="@drawable/on" /> <!-- pressed -->    <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: * click the button to open the flashlight when you click the button. * Turn off the flashlight when you close the button. ***/public class MainActivity extends Activity implements OnClickListener {private ToggleButton toggleButton; private Camera 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. findView ById (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 (0 xffffffff); // 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 ();}}


:





Android for flashlight Development

Set listening for open and close respectively, and move the last and second curly brackets before closeListener.
Public class MainActivity extends Activity {
Private Camera camera = null;
Private Parameters parameters = null;
Private Button open = null;
Private Button close = null;
Private TextView title = null;

@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );

Open = (Button) findViewById (R. id. open );
Close = (Button) findViewById (R. id. close );
Title = (TextView) findViewById (R. id. title );

Title. setText (R. string. title );
Open. setText (R. string. open );
Close. setText (R. string. close );
Open. setOnClickListener (openListener );
Close. setOnClickListener (closeListener );

}

Public class openListener implements OnClickListener {

@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
Camera = Camera. open ();
Parameters = camera. getParameters ();
Parameters. setFlashMode (Parameters. FLASH_MODE_TORCH); // enable
Camera. setParameters (parameters );
}
}
Public class closeListener implements OnClickListener {

@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
Parameters. setFlashMode (Parameters. FLASH_MODE_OFF); // close
Camera. setParameters (parameters );
Camera. release ();
}

}

Public boolean onCreateOption... the remaining full text>

Where is the flashlight in Android 412?

You download a flashlight in the Android Market,
Generally, you use the flashlight that comes with your mobile phone.
Full Screen white lighting without flashing
Or if you download the program management software, some software comes with the flashlight function.

Related Article

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.