Android studio 간단 한 손전등 앱 만 들 기

좋 은 손전등 앱 입 니 다.여러분 께 공유 하 겠 습 니 다.좋아해 주시 기 바 랍 니 다.
1.자바 코드    

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>
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기