月黑风高,伸手不见五指,多么容易发生什么恐怖袭击啊!在网上下载的应用又有很烦人的各种各样的广告,不用担心,这里手把手叫你怎么做一个Android的手电筒应用程序,用苹果和WP的请绕道,如果手机不带闪光灯的可以先看看,下次买个新手机再体验一下,有图有真相,先上图:
准备:带闪光灯的手机一台,android开发环境
一、首先我们新建一个工程,命名FlashLight,给布局文件main.xml添加内容:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/cio" >
<ToggleButton
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="13dp"
android:layout_marginRight="60dp"
android:textOff="闪光关"
android:textOn="闪光开" />
<ToggleButton
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button2"
android:layout_alignBottom="@+id/button2"
android:layout_alignParentLeft="true"
android:layout_marginLeft="52dp"
android:textOff="手电关"
android:textOn="手电开" />
</RelativeLayout>
在这个工程里面我们简单的用ToggleButton来实现功能,你可以选择自己喜欢的组件来完成。
二、在activity中实现功能,简单介绍一下,android的手电筒功能实际上就是控制闪光灯的开与关,不断闪动则是通过一个线程不断的开、关来实现的,很简单,上代码:
public class MainActivity extends Activity
{
private static final String TAG = "MainActivity";
private Camera mCamera;
private Camera.Parameters parameters;
private long mExitTime = 0;
private ToggleButton mTbtnLight,mTbtnFlashlight;
public static final int OPEN_LIGHT = 0x0010;
public static final int CLOSE_LIGHT = 0x0020;
private FlightThread flightThread;
private OnCheckedChangeListener mChangeListener = new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (isChecked)
{
openLight();
}
else
{
closeLight();
}
}
};
private OnCheckedChangeListener mFlashListener = new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (isChecked)
{
// 开始循环
isStart = true;
}
else
{
isStart = false;
closeLight();
}
}
};
public Handler mHandler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
switch (msg.what)
{
case OPEN_LIGHT:
openLight();
break;
case CLOSE_LIGHT:
closeLight();
break;
case 1000:
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
initValue();
flightThread = new FlightThread();
flightThread.start();
}
private void initValue()
{
mCamera = Camera.open();
mTbtnLight = (ToggleButton)findViewById(R.id.button1);
mTbtnFlashlight = (ToggleButton)findViewById(R.id.button2);
parameters = mCamera.getParameters();
mTbtnLight.setSelected(false);
mTbtnFlashlight.setSelected(false);
mTbtnLight.setOnCheckedChangeListener(mChangeListener);
mTbtnFlashlight.setOnCheckedChangeListener(mFlashListener);
}
/**
* 打开手电
* @author jetsen-ZhongHeliang 2014年1月21日 下午6:03:01
*/
private void openLight()
{
parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(parameters);
mCamera.startPreview();
}
/**
* 关闭手电
* @author jetsen-ZhongHeliang 2014年1月21日 下午6:03:22
*/
private void closeLight()
{
parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
mCamera.setParameters(parameters);
}
boolean isStart = false;
int i = 0;
class FlightThread extends Thread
{
@Override
public void run()
{
while (true)
{
while (isStart)
{
if (i++ %2 == 0)
{
mHandler.obtainMessage(OPEN_LIGHT).sendToTarget();
}
else
{
mHandler.obtainMessage(CLOSE_LIGHT).sendToTarget();
}
try
{
Thread.sleep(200);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_BACK:
// 这里穿插一个很简单的双击退出的功能,有点意思
if ((System.currentTimeMillis() - mExitTime) > 2000)
{
Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
mExitTime = System.currentTimeMillis();
}
else
{
finish();
}
return true;