android 手电筒
来源:互联网 发布:创业软件 上市时间 编辑:程序博客网 时间:2024/04/29 14:59
代码地址:http://download.csdn.net/detail/u011324501/9422876
简单的手电筒代码:
MainActivity.java
package com.example.flashlight;import android.hardware.Camera;import android.hardware.Camera.Parameters;import android.os.Bundle;import android.os.Handler;import android.app.Activity;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity { private boolean isopent = false; private Camera camera; private Button button; boolean is = true; boolean sosflag = true; Button sos; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); View view = View.inflate(this, R.layout.main, null); setContentView(view); button = (Button) findViewById(R.id.main_img); //手电筒开关按钮 button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub is=false; try{ if (!isopent) { on(); } else { off(); } }catch(Exception e){ e.printStackTrace(); } } }); //sos按钮 sos = (Button)findViewById(R.id.sos); sos.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubif(sosflag){ //开启线程handler.removeCallbacks(runnable);handler.postDelayed(runnable,1000);sosflag=false; }else{ //关闭线程 handler.removeCallbacks(runnable); } }}); } //sos线程 private Handler handler = new Handler(); private Runnable runnable = new Runnable() { public void run () { on(); delay(500); off(); handler.postDelayed(this,1000); } }; /** * 延时函数 * @param ms */ private void delay(int ms){ try { Thread.currentThread(); Thread.sleep(ms); } catch (InterruptedException e) { e.printStackTrace(); } } /* * 打开手电筒 * */ public void on(){ button.setBackgroundResource(R.drawable.on); // Toast.makeText(getApplicationContext(), "您已经打开了手电筒", 0).show(); camera = Camera.open(); Parameters params = camera.getParameters(); params.setFlashMode(Parameters.FLASH_MODE_TORCH); camera.setParameters(params); camera.startPreview(); // 开始亮灯 isopent = true; } //关闭手电筒 public void off(){ button.setBackgroundResource(R.drawable.off); // Toast.makeText(getApplicationContext(), "关闭了手电筒", Toast.LENGTH_SHORT).show(); camera.stopPreview(); // 关掉亮灯 camera.release(); // 关掉照相机 isopent = false; }}布局文件:main.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/main_img" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/off" > </Button> <Button android:id="@+id/sos" android:layout_width="50dip" android:layout_height="40dip" android:textSize="15sp" android:layout_alignBaseline="@+id/main_img" android:layout_alignBottom="@+id/main_img" android:layout_centerHorizontal="true" android:text="@string/sos" /> </RelativeLayout>用到的权限:
<!-- 摄像头、手电筒 --> <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" />
0 0
- android手电筒
- Android手电筒
- Android手电筒
- android 手电筒
- android手电筒
- android手电筒
- android 手电筒
- Android手电筒
- 初探android之手电筒
- android之简单手电筒
- android 手电筒实现
- android 手电筒的实现
- Android实现手机手电筒
- android 手电筒实现Demo
- Android 手电筒 FlashLight
- android 手电筒开发
- android 手电筒 代码
- Android 手电筒设计
- GPS坐标单位(度分秒)的换算方法
- Java POI 写入Excel
- 高并发二级缓存的简单实现
- 关于 eclipse项目 导入 android studio 中 出现的图片报错问题解决。
- 2677: 韩信点兵
- android 手电筒
- 音轨与声道区别
- Python篇----多线程1TB数据生成脚本
- 【高斯消元】[BZOJ3505]和谐矩阵
- 新生系统项目组长经验总结一之需求把握
- 使用命令行查看网络连接情况
- nyoj1170最大的数【贪心】
- 对session和cookie的一些理解
- lucene、solr、nutch三者的关系