码迷,mamicode.com
首页 > 移动开发 > 详细

Android 手电筒开发源代码

时间:2015-02-02 01:54:30      阅读:294      评论:0      收藏:0      [点我收藏+]

标签:

实现步骤

1. 修改布局文件代码activity_main.xml

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical">
 6 
 7    <Button 
 8        android:id="@+id/btn_flash"
 9        android:layout_width="match_parent"
10        android:layout_height="wrap_content"
11        android:text="手电筒已关闭!"/>
12 
13 </LinearLayout>

 

2. 在AndroidManifest.xml中增加需要使用的权限

1 <!-- 摄像头、手电筒 -->
2 <uses-permission android:name="android.permission.CAMERA" />
3 <uses-permission android:name="android.permission.FLASHLIGHT" />
4 <uses-feature android:name="android.hardware.camera" />
5 <uses-feature android:name="android.hardware.camera.autofocus" />
6 <uses-feature android:name="android.hardware.camera.flash" />
  • android.permission.CAMERA:Required to be able to access the camera device.
  • android.permission.FLASHLIGHT:Allows access to the flashlight

3. 修改MainActivity

 1 package com.example.test;
 2 
 3 import android.app.Activity;
 4 import android.hardware.Camera;
 5 import android.hardware.Camera.Parameters;
 6 import android.os.Bundle;
 7 import android.view.View;
 8 import android.view.View.OnClickListener;
 9 import android.widget.Button;
10 import android.widget.Toast;
11 
12 public class MainActivity extends Activity {
13 
14     private boolean isopen = false; //记录手电筒状态
15     private Camera camera; //
16     private Button btn_flash;
17 
18     @Override
19     protected void onCreate(Bundle savedInstanceState) {
20         // TODO Auto-generated method stub
21         super.onCreate(savedInstanceState);
22         setContentView(R.layout.activity_main);
23 
24         btn_flash = (Button) findViewById(R.id.btn_flash);
25         btn_flash.setOnClickListener(new OnClickListener() {
26             @Override
27             public void onClick(View v) {
28                 if (!isopen) { //如果手电筒已经开启
29                     Toast.makeText(getApplicationContext(), "手电筒已开启", 0).show();
30                     camera = Camera.open();
31                     Parameters params = camera.getParameters();
32                     params.setFlashMode(Parameters.FLASH_MODE_TORCH);
33                     camera.setParameters(params);
34                     camera.startPreview(); // 开始亮灯
35                     isopen = true;
36                     btn_flash.setText("手电筒已开启!");
37                 } else {
38                     Toast.makeText(getApplicationContext(), "关闭了手电筒",
39                             Toast.LENGTH_SHORT).show();
40                     camera.stopPreview(); // 关掉亮灯
41                     camera.release(); // 关掉照相机
42                     isopen = false;
43                     btn_flash.setText("手电筒已关闭!");
44                 }
45             }
46         });
47     }
48 
49 }

Android 手电筒开发源代码

标签:

原文地址:http://www.cnblogs.com/WangKangReg/p/4266657.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!