Android 手电筒源码
2014-04-28 15:20
316 查看
最近由于公司需要,做了一个手电筒,其实手电筒原理很简单,就是调用照相机的闪光灯,控制闪光灯的开关,就可以实现手电筒的效果,
强调一下,代码中一定要注意在结束的时候对闪光灯进行释放,否则就会导致使用照相机的时候出现——无法连接到相机 这个问题
手电筒APK下载地址:点击打开下载链接
手电筒项目源码下载:点击打开下载链接
主要代码如下:
需要用到的权限如下:
界面布局如下:
实现的效果如下:
强调一下,代码中一定要注意在结束的时候对闪光灯进行释放,否则就会导致使用照相机的时候出现——无法连接到相机 这个问题
手电筒APK下载地址:点击打开下载链接
手电筒项目源码下载:点击打开下载链接
主要代码如下:
package com.techainsh.flashlight; import java.util.List; import android.hardware.Camera; import android.hardware.Camera.Parameters; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.os.PowerManager; import android.os.Vibrator; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener { public ImageView btnimageView = null; public RelativeLayout bgmageView = null; private Camera camera; private boolean isOpen = true; public final static int OPEN_CAMERA = 1011; public final static int OPEN_LIGHT = 1012; public final static int CLOSE_LIGHT = 1013; private Vibrator vibrator; long[] pattern = { 100, 200 }; PowerManager.WakeLock wakeLock; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); handler.sendEmptyMessage(OPEN_CAMERA); vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); btnimageView = (ImageView) findViewById(R.id.btn_image); bgmageView = (RelativeLayout) findViewById(R.id.bg_image); bgmageView.setBackgroundResource(R.drawable.bg_flashlight_off); btnimageView.setImageResource(R.drawable.btn_flash_light_off); btnimageView.setOnClickListener(this); bgmageView.setBackgroundResource(R.drawable.bg_flashlight_on); btnimageView.setImageResource(R.drawable.btn_flash_light_on); handler.sendEmptyMessage(OPEN_LIGHT); vibrator.vibrate(pattern, -1); } Handler handler = new Handler(Looper.getMainLooper()) { @Override public void handleMessage(Message msg) { switch (msg.what) { case OPEN_CAMERA: camera = Camera.open(); break; case OPEN_LIGHT: Parameters params = camera.getParameters(); List<String> list = params.getSupportedFlashModes(); if (list.contains(Parameters.FLASH_MODE_TORCH)) { params.setFlashMode(Parameters.FLASH_MODE_TORCH); } else { Toast.makeText(getApplicationContext(), "此设备不支持闪光灯模式", Toast.LENGTH_SHORT).show(); } camera.setParameters(params); camera.startPreview(); isOpen = true; break; case CLOSE_LIGHT: if (isOpen) { Parameters closepParameters = camera.getParameters(); closepParameters .setFlashMode(Camera.Parameters.FLASH_MODE_OFF); camera.setParameters(closepParameters); camera.stopPreview(); // camera.release(); isOpen = false; // sendEmptyMessage(OPEN_CAMERA); } break; default: break; } } }; @Override protected void onDestroy() { super.onDestroy(); if (null != camera) { camera.release(); camera = null; } } public void onStop() { super.onStop(); vibrator.cancel(); if (null != camera) { camera.release(); camera = null; } MainActivity.this.finish(); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { QuitAlert(); } return super.onKeyDown(keyCode, event); } public void QuitAlert() { new AlertDialog.Builder(MainActivity.this).setTitle("关闭手电筒提示") .setIcon(R.drawable.ic_launcher).setMessage("确定要关闭手电筒么?") .setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { MainActivity.this.finish(); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub } }).create().show(); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE); wakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "test"); wakeLock.acquire(); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); if (wakeLock != null) wakeLock.release(); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_image: if (isOpen) { bgmageView.setBackgroundResource(R.drawable.bg_flashlight_off); btnimageView.setImageResource(R.drawable.btn_flash_light_off); handler.sendEmptyMessage(CLOSE_LIGHT); vibrator.vibrate(pattern, -1); } else { bgmageView.setBackgroundResource(R.drawable.bg_flashlight_on); btnimageView.setImageResource(R.drawable.btn_flash_light_on); handler.sendEmptyMessage(OPEN_LIGHT); vibrator.vibrate(pattern, -1); } break; default: break; } } }
需要用到的权限如下:
<uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.FLASHLIGHT" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" /> <uses-feature android:name="android.hardware.camera.flash" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.WAKE_LOCK" />
界面布局如下:
<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/bg_flashlight" > <RelativeLayout android:id="@+id/bg_image" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bg_flashlight_off" > <ImageView android:id="@+id/btn_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_alignParentBottom="true" android:layout_marginBottom="100dip" android:src="@drawable/btn_flash_light_on" /> </RelativeLayout> </RelativeLayout>
实现的效果如下:
相关文章推荐
- Android实战简易教程<五十七>(分享小米手电筒源码)
- Android 开启闪光灯做手电筒 源码
- android手电筒+闪光灯基本源码
- Android 开启闪光灯做手电筒 源码
- Android 开启闪光灯做手电筒 源码
- Android 手电筒控制源码分析
- 一个简单的手电筒APP源码分享(支持Android O(8.0)及以下版本)
- Android实战简易教程-第五十七枪(分享小米手电筒源码)
- Android手电筒+闪光灯基本源码
- Android 开启闪光灯做手电筒 源码
- android源码下载方式
- 程序猿笔记Android客户端说明及项目源码下载!
- android App启动源码分析
- 下载Android源码(5.1.1)
- 继续推荐Android12个自测源码
- Android源码大放送(实战开发必备)
- Android中Canvas绘图基础详解(附源码下载)
- 配置Android源码编译环境所需包
- 深入Android内核——Android init.c源码深入分析
- Android4.2.2 动态显示隐藏屏幕底部的导航栏(对系统源码进行修改)