01
|
/**
|
02
|
*
|
03
|
*
|
04
|
*
|
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
|
20
|
21
|
@Override
|
22
|
protected void onCreate(Bundle
|
23
|
//
|
24
|
super .onCreate(savedInstanceState);
|
25
|
View this , null );
|
26
|
setContentView(view);
|
27
|
TextView
|
28
|
29
|
img_but.setOnClickListener( new View.OnClickListener()
|
30
|
31
|
@Override
|
32
|
public void onClick(View
|
33
|
//
|
34
|
if (!isopent)
|
35
|
Toast.makeText(getApplicationContext(), "您已经打开了手电筒" , 0 )
|
36
|
.show();
|
37
|
camera
|
38
|
Parameters
|
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 "1.0" encoding= "utf-8" ?>
|
02
|
<LinearLayout "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 "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 ".AppStart" >
|
15
|
<intent-filter>
|
16
|
<action "android.intent.action.MAIN" />
|
17
|
18
|
<category "android.intent.category.LAUNCHER" />
|
19
|
</intent-filter>
|
20
|
</activity>
|
21
|
<activity ".Main" >
|
22
|
</activity>
|
23
|
</application>
|
24
|
<!--
|
25
|
<uses-permission "android.permission.CAMERA" />
|
26
|
<uses-permission "android.permission.FLASHLIGHT" />
|
27
|
28
|
<uses-feature "android.hardware.camera" />
|
29
|
<uses-feature "android.hardware.camera.autofocus" />
|
30
|
<uses-feature "android.hardware.camera.flash" />
|
31
|
32
|
</manifest>
|
时间: 2024-10-21 13:15:53