Android Studio to write a simple flashlight app_android

Source: Internet
Author: User

Very good flashlight app, share to everybody, hope everybody likes.

1. Java Code

Package com.example.goo.gfire;
Import android.app.Activity;
Import Android.hardware.Camera;
Import Android.hardware.Camera.Parameters;
Import Android.os.Bundle;
Import Android.view.View;
 
 
Import Android.widget.Button;
  public class Fireactivity extends activity {private button button;
  Private Camera Camera = Camera.open ();
  Private Parameters Parameters;
 
  Private Boolean islight=true;
   public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
   Setcontentview (R.layout.activity_fire);
   Button = (button) This.findviewbyid (R.ID.BN); Button.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {if islight
        ) {Camera.startpreview ();
        Parameters = Camera.getparameters ();
        Parameters.setflashmode (Parameters.flash_mode_torch);
        Camera.setparameters (parameters);
        Islight = false;
      Button.settext ("Off"); else {parameters = camera.GetParameters ();
        Parameters.setflashmode (Parameters.flash_mode_off);
        Camera.setparameters (parameters);
        Islight = true;
      Button.settext ("open");
  }
     }
 
   });
 }
}

2. Fireactivity.java

Package com.example.goo.gfire;
Import android.app.Activity;
Import Android.hardware.Camera;
Import Android.hardware.Camera.Parameters;
Import Android.os.Bundle;
Import Android.view.View;
 
 
Import Android.widget.Button;
  public class Fireactivity extends activity {private button button;
  Private Camera Camera = Camera.open ();
  Private Parameters Parameters;
 
  Private Boolean islight=true;
   public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
   Setcontentview (R.layout.activity_fire);
   Button = (button) This.findviewbyid (R.ID.BN); Button.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {if islight
        ) {Camera.startpreview ();
        Parameters = Camera.getparameters ();
        Parameters.setflashmode (Parameters.flash_mode_torch);
        Camera.setparameters (parameters);
        Islight = false;
      Button.settext ("Off"); else {parameters = camera.GetParameters ();
        Parameters.setflashmode (Parameters.flash_mode_off);
        Camera.setparameters (parameters);
        Islight = true;
      Button.settext ("open");
  }
     }
 
   });
 }
}

3. Content_fire.xml

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android= "http://schemas.android.com/apk/res/"
  Android "xmlns:app=" Http://schemas.android.com/apk/res-auto "xmlns:tools=" Http://schemas.android.com/tools " Android:layout_width= "Match_parent" android:layout_height= "match_parent" android:paddingbottom= "@dimen/activity_ Vertical_margin "android:paddingleft=" @dimen/activity_horizontal_margin "android:paddingright=" @dimen/activity_ Horizontal_margin "android:paddingtop=" @dimen/activity_vertical_margin "app:layout_behavior=" @string/appbar_ Scrolling_view_behavior "tools:context=" com.example.goo.gfire.FireActivity "tools:showin=" "@layout/activity_fire" > <textview android:layout_width= "match_parent" android:layout_height= "wrap_content" power Barrel mode "android:textsize=" 34SP "android:textstyle=" bold "android:gravity=" center "/> <button android:l
Ayout_width= "Match_parent" android:layout_height= "Wrap_content"    Android:id= "@+id/bn" android:textoff= "Off" android:texton= "open" android:height= "400DP" Android:layout_alig Nparentbottom= "true" android:layout_centerhorizontal= "true" android:layout_marginbottom= "59DP" Android:fontfea turesettings= "@string/auth_google_play_services_client_facebook_display_name" android:textcolor= "@color/common_ Signin_btn_dark_text_focused "android:textstyle=" bold "android:textsize=" 100sp "/> </relativelayout&
 Gt

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.