欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android studio编写简单的手电筒APP

程序员文章站 2024-03-06 19:59:02
很不错的手电筒app,分享给大家,希望大家喜欢。 1. java代码     package com.exampl...

很不错的手电筒app,分享给大家,希望大家喜欢。

1. 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("关");
      } else {
        parameters = camera.getparameters();
        parameters.setflashmode(parameters.flash_mode_off);
        camera.setparameters(parameters);
        islight = true;
        button.settext("开");
      }
     }
 
   });
  }
}

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("关");
      } else {
        parameters = camera.getparameters();
        parameters.setflashmode(parameters.flash_mode_off);
        camera.setparameters(parameters);
        islight = true;
        button.settext("开");
      }
     }
 
   });
  }
}

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"
    android:text="电筒模式"
    android:textsize="34sp"
    android:textstyle="bold"
    android:gravity="center"/>
  <button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/bn"
    android:textoff="关"
    android:texton="开"
    android:height="400dp"
    android:layout_alignparentbottom="true"
    android:layout_centerhorizontal="true"
    android:layout_marginbottom="59dp"
    android:fontfeaturesettings="@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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。