Learn how Android works in the simplest way to turn on the Flash (35)

Source: Internet
Author: User

We have one of 360 mobile phone assistants, called "the Most Beautiful Flashlight",:


To turn the flash on or off with the flashlight switch onthe diagram, OK, here's the code to implement this feature:

public class Mainactivity extends Activity implements Onclicklistener {private ImageView iv_off_on;private ImageView iv_l Ight;private Parameters parameter = null;private camera camera = null; @Overrideprotected void OnCreate (Bundle Savedinstan Cestate) {super.oncreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); SetContentView ( R.layout.activity_main); Initview (); Initlistener ();} private void Initlistener () {Iv_off_on.setonclicklistener (this);} private void Initview () {iv_off_on = (ImageView) Findviewbyid (r.id.iv_off_on); iv_light = (ImageView) Findviewbyid ( R.id.iv_light); iv_light.setvisibility (view.invisible);} @Overridepublic void OnClick (View v) {switch (V.getid ()) {Case r.id.iv_off_on:if (iv_off_on.isselected ()) {//Off iv_off_ On.setselected (false); Iv_light.setvisibility (view.invisible);p Arameter.setflashmode (Parameters.flash_mode_off) ; camera.setparameters (parameter); Camera.release ();} else {//Turn on iv_off_on.setselected (true); iv_light.setvisibility (view.visible); camera = CAmera.open (); Camera.startpreview ();p arameter = Camera.getparameters ();p Arameter.setflashmode (Parameters.FLASH_ Mode_torch); camera.setparameters (parameter);} Break;default:break;}}}


Finally, add the appropriate permissions in the Androidmanifest.xml:

<uses-permission android:name= "Android.permission.FLASHLIGHT"/>
<uses-permission android:name= "Android.permission.CAMERA"/>
<uses-feature android:name= "Android.hardware.camera"/>
<uses-feature android:name= "Android.hardware.autofocus"/>


Reprint Please specify source:http://blog.csdn.net/hai_qing_xu_kong/article/details/45504303 Emotional Control _

Learn how Android works in the simplest way to turn on the Flash (35)

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.