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= "http://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= "http://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>
|