实现功能:
闪光灯有两种模式:
1.普通模式:点击按钮打开或者关闭;
2.sos模式:点击按钮闪光灯每次间隔500ms闪烁一下,再次点击按钮则关闭;
页面制作:
1: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"2: xmlns:android1="http://schemas.android.com/apk/res/android"3: android:layout_width="match_parent"4: android:layout_height="match_parent">5:6: <RadioGroup7: android1:id="@+id/LampRadioGroup"8: android1:layout_width="wrap_content"9: android1:layout_height="wrap_content"10: android1:layout_alignParentTop="true"11: android1:layout_centerHorizontal="true"12: android1:layout_marginTop="61dp"13: android1:orientation="horizontal" >14:15: <RadioButton16: android1:id="@+id/comRadio"17: android1:layout_width="wrap_content"18: android1:layout_height="wrap_content"19: android1:checked="true"20: android1:text="@string/comMode" />21:22: <RadioButton23: android1:id="@+id/sosRadio"24: android1:layout_width="wrap_content"25: android1:layout_height="wrap_content"26: android1:text="@string/sosMode" />27: </RadioGroup>28:29: <ImageButton30: android1:id="@+id/but_image"31: android1:layout_width="wrap_content"32: android1:layout_height="wrap_content"33: android1:layout_centerHorizontal="true"34: android1:layout_centerVertical="true"35: android1:src="@drawable/ic_launcher" android1:contentDescription="@string/todo"/>36:37: </RelativeLayout>
效果图:
在该页面中我们添加了RadioGroup,该组件可以确保在该group中的Radio可选中唯一的一个。
设置Oritention属性的horizontal值可以使两个Radio排在一排。
具体实现代码如下:
1: package net.csing.flashlamp;
2:3: import android.support.v7.app.ActionBarActivity;
4: import android.app.ActivityManager;
5: import android.content.Context;
6: import android.hardware.Camera;
7: import android.hardware.Camera.Parameters;
8: import android.os.Bundle;
9: import android.view.Menu;
10: import android.view.MenuItem;
11: import android.view.View;
12: import android.view.View.OnClickListener;
13: import android.widget.Button;
14: import android.widget.ImageButton;
15: import android.widget.RadioGroup;
16: import android.widget.RadioGroup.OnCheckedChangeListener;
17:18: public class MainActivity extends ActionBarActivity {19:20: ImageButton btn_lamp;21: RadioGroup rGroup;22: boolean isOn,flag,isFlash;
23: Parameters parameters;24: Camera camera;25: int Model;
26: @Override27: protected void onCreate(Bundle savedInstanceState) {28: super.onCreate(savedInstanceState);
29: setContentView(R.layout.activity_main);30: //打开摄像头;
31: camera=Camera.open();32: //查找控件;
33: findViews();34: //注册监听事件;
35: registerListener();36: }37: private void registerListener() {38: final Camera.Parameters parameters=camera.getParameters();
39: //注册Radio监听事件;
40: rGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
41:42: public void onCheckedChanged(RadioGroup group, int checkedId) {43: //如果选择普通模式
44: if(checkedId==R.id.comRadio){
45: Model=0;46: }47: //如果选择sos模式;
48: if(checkedId==R.id.sosRadio){
49: Model=1;50: }51: //选择模式后,重置闪光灯为关闭;
52: parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);53: camera.setParameters(parameters);54: isOn=false;
55: flag=false;
56: isFlash=false;
57: }58: });59: //设置按钮监听事件;
60: btn_lamp.setOnClickListener(new OnClickListener() {
61:62: @Override63: public void onClick(View v) {64: //如果在普通模式下,则启动普通模式;
65: if(Model==0){
66: flashComModel(parameters);67: }68: else if(Model==1){69: flashSosModel(parameters);70: }71: }72: });73: }74:75: //闪烁模式(sos模式);
76: protected void flashSosModel(final Parameters parasos) {77: //如果闪光灯没有闪烁;
78: if(!isFlash){
79: //线程运行标识;
80: flag=true;
81: //开启闪烁标识;
82: isFlash=true;
83: //启动线程,控制闪光灯闪烁;
84: Thread thread=new Thread(){
85: public void run(){86: while(flag){
87: //启动闪光灯闪烁;
88: parasos.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);89: camera.setParameters(parasos);90: try {
91: sleep(500);92: //关闭闪光等闪烁;
93: parasos.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);94: camera.setParameters(parasos);95: sleep(500);96: } catch (InterruptedException e) {
97: e.printStackTrace();98: }99: }100: }101: };102: thread.start();103: }104: //如果是闪烁状态则关闭闪烁;
105: else if(isFlash){106: flag=false;
107: isFlash=false;
108: parasos.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);109: camera.setParameters(parasos);110: }111: }112: //按钮按下进入闪光灯普通模式;
113: protected void flashComModel(Parameters para) {114: //如果闪光灯不是关闭的,则开启;
115: if(!isOn){
116: para.setFlashMode(Parameters.FLASH_MODE_TORCH);117: camera.setParameters(para);118: //camera.startPreview();
119: isOn=true;
120: }121: //如果闪光灯是开启的则关闭闪光灯;
122: else if(isOn){123: para.setFlashMode(Parameters.FLASH_MODE_OFF);124: camera.setParameters(para);125: //camera.stopPreview();
126: isOn=false;
127: }128: }129: //查找相关组件名称;
130: public void findViews(){131: btn_lamp=(ImageButton)findViewById(R.id.but_image);132: rGroup=(RadioGroup)findViewById(R.id.LampRadioGroup);133: }134: @Override135: public boolean onCreateOptionsMenu(Menu menu) {136: // Inflate the menu; this adds items to the action bar if it is present.
137: getMenuInflater().inflate(R.menu.main, menu);138: return true;139: }140:141: @Override142: public boolean onOptionsItemSelected(MenuItem item) {143: // Handle action bar item clicks here. The action bar will
144: // automatically handle clicks on the Home/Up button, so long
145: // as you specify a parent activity in AndroidManifest.xml.
146: int id = item.getItemId();
147: if (id == R.id.action_settings) {
148: return true;149: }150: return super.onOptionsItemSelected(item);151: }152:153: }154: