410
穀歌
android平台手電筒開發源代碼
01
|
/**
|
02
|
*
name : 手電筒主界麵
|
03
|
*
author : ycgpp@126.com
|
04
|
*
date : 2012-12-21 15:50:17
|
05
|
*/
|
06
|
package com.android.app;
|
07
|
08
|
import android.app.Activity;
|
09
|
import android.hardware.Camera;
|
10
|
import android.hardware.Camera.Parameters;
|
11
|
import android.os.Bundle;
|
12
|
import android.view.View;
|
13
|
import android.widget.TextView;
|
14
|
import android.widget.Toast;
|
15
|
16
|
public class Main extends Activity
{
|
17
|
18
|
private boolean isopent
= false;
|
19
|
private Camera
camera;
|
20
|
21
|
@Override
|
22
|
protected void onCreate(Bundle
savedInstanceState) {
|
23
|
//
TODO Auto-generated method stub
|
24
|
super.onCreate(savedInstanceState);
|
25
|
View
view = View.inflate(this,
R.layout.main, null);
|
26
|
setContentView(view);
|
27
|
TextView
img_but = (TextView) findViewById(R.id.main_img);
|
28
|
29
|
img_but.setOnClickListener(new View.OnClickListener()
{
|
30
|
31
|
@Override
|
32
|
public void onClick(View
v) {
|
33
|
//
TODO Auto-generated method stub
|
34
|
if (!isopent)
{
|
35
|
Toast.makeText(getApplicationContext(), "您已經打開了手電筒", 0)
|
36
|
.show();
|
37
|
camera
= Camera.open();
|
38
|
Parameters
params = camera.getParameters();
|
39
|
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
|
40
|
camera.setParameters(params);
|
41
|
camera.startPreview(); //
開始亮燈
|
42
|
43
|
isopent
= true;
|
44
|
} else {
|
45
|
Toast.makeText(getApplicationContext(), "關閉了手電筒",
|
46
|
Toast.LENGTH_SHORT).show();
|
47
|
camera.stopPreview(); //
關掉亮燈
|
48
|
camera.release(); //
關掉照相機
|
49
|
isopent
= false;
|
50
|
}
|
51
|
}
|
52
|
});
|
53
|
}
|
54
|
55
|
}
|
[代碼] 布局文件代碼
01
|
<?xml
version="1.0" encoding="utf-8"?>
|
02
|
<LinearLayout
xmlns:android="https://schemas.android.com/apk/res/android"
|
03
|
android:layout_width="match_parent"
|
04
|
android:layout_height="match_parent"
|
05
|
android:orientation="vertical" >
|
06
|
07
|
<TextView
|
08
|
android:id="@+id/main_img"
|
09
|
android:layout_width="fill_parent"
|
10
|
android:layout_height="fill_parent"
|
11
|
android:background="@drawable/main_body">
|
12
|
</TextView>
|
13
|
14
|
</LinearLayout>
|
[代碼] AndroidManifest.xml文件
01
|
<manifest
xmlns:android="https://schemas.android.com/apk/res/android"
|
02
|
package="com.android.app"
|
03
|
android:versionCode="1"
|
04
|
android:versionName="1.0" >
|
05
|
06
|
<uses-sdk
|
07
|
android:minSdkVersion="8"
|
08
|
android:targetSdkVersion="15" />
|
09
|
10
|
<application
|
11
|
android:icon="@drawable/ic_launcher"
|
12
|
android:label="@string/app_name"
|
13
|
android:theme="@style/AppTheme" >
|
14
|
<activity
android:name=".AppStart" >
|
15
|
<intent-filter>
|
16
|
<action
android:name="android.intent.action.MAIN" />
|
17
|
18
|
<category
android:name="android.intent.category.LAUNCHER" />
|
19
|
</intent-filter>
|
20
|
</activity>
|
21
|
<activity
android:name=".Main" >
|
22
|
</activity>
|
23
|
</application>
|
24
|
<!--
攝像頭、手電筒 -->
|
25
|
<uses-permission
android:name="android.permission.CAMERA" />
|
26
|
<uses-permission
android:name="android.permission.FLASHLIGHT" />
|
27
|
28
|
<uses-feature
android:name="android.hardware.camera" />
|
29
|
<uses-feature
android:name="android.hardware.camera.autofocus" />
|
30
|
<uses-feature
android:name="android.hardware.camera.flash" />
|
31
|
32
|
</manifest>
|
最後更新:2017-04-03 22:15:47