手电筒源码
来源:互联网 发布:windows xp原版下载 编辑:程序博客网 时间:2024/06/02 23:56
一个简单的用闪光灯作为手电筒功能的实现。
布局文件main.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<!--背景图--> <ImageView android:id="@id/image_bg" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/close" />
<!--开关图片--> <ImageView android:id="@id/image_onoff" android:layout_width="80.0dip" android:layout_height="80.0dip" android:layout_marginBottom="46.0dip" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" /></RelativeLayout>
Activity类
package com.wellss.flashlight;import android.app.Activity;import android.app.KeyguardManager;import android.app.KeyguardManager.KeyguardLock;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.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.view.WindowManager;import android.widget.ImageView;public class FlashlightActivity extends Activity implements OnClickListener{private String TAG = "FlashlightAcitvity"; private Camera camera = null; private ImageView mImageBg; private ImageView mImageOnoff; private void updateBgImage() { if (IsFlashlightOn()){ this.mImageBg.setImageResource(R.drawable.open); } else{ this.mImageBg.setImageResource(R.drawable.close); } } private boolean IsFlashlightOn(){ boolean isOn=false; if(camera!=null) { Parameters parameters=camera.getParameters(); String flashLightModeString=parameters.getFlashMode(); if(flashLightModeString.equals(Parameters.FLASH_MODE_TORCH)) { isOn=true; } else { { isOn=false; }} } else { { isOn=false; }} return isOn; } private void SetFlashlightOn(){ if(camera!=null){ Parameters params = camera.getParameters(); params.setFlashMode(Parameters.FLASH_MODE_TORCH); camera.setParameters(params); } } private void SetFlashlightOff(){ if(camera!=null){ Parameters params=camera.getParameters(); params.setFlashMode(Parameters.FLASH_MODE_OFF); camera.setParameters(params); } } @Overridepublic void onClick(View paramView) {// TODO Auto-generated method stub if (paramView.getId() == R.id.image_onoff) { if(!IsFlashlightOn()){ SetFlashlightOn(); updateBgImage(); } else { SetFlashlightOff(); updateBgImage();} }}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//dislay full screen requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);setContentView(R.layout.main); this.mImageBg = ((ImageView)findViewById(R.id.image_bg)); this.mImageOnoff = ((ImageView)findViewById(R.id.image_onoff)); this.mImageOnoff.setOnClickListener(this);// this.mIsScreenLock = isLockScreen(); Log.d(TAG, "cly on create");}class OpenCameraThread implements Runnable{@Overridepublic void run() {// TODO Auto-generated method stub//耗时比较长,故放在另外一个线程中处理 if(camera==null){ camera=Camera.open(); camera.startPreview(); }}}@Overrideprotected void onStart() {// TODO Auto-generated method stubsuper.onStart();Thread openCamera=new Thread(new OpenCameraThread());openCamera.start(); Log.d(TAG, "cly on start");}@Overrideprotected void onStop() {// TODO Auto-generated method stubsuper.onStop();if(camera!=null){//update image this.mImageBg.setImageResource(R.drawable.close); camera.stopPreview(); // 关掉亮灯 camera.release(); // 关掉照相机 camera=null;} Log.d(TAG, "cly on stop");}@Overrideprotected void onResume() {// TODO Auto-generated method stubsuper.onResume(); Log.d(TAG, "cly on resume");}@Overrideprotected void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy(); Log.d(TAG, "cly on destroy");}}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest android:versionCode="1" android:versionName="1.0" package="com.wellss.flashlight" xmlns:android="http://schemas.android.com/apk/res/android"> <uses-permission android:name="android.permission.DISABLE_KEYGUARD" /> <uses-permission android:name="android.permission.FLASHLIGHT" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.autofocus"/> <uses-sdk android:minSdkVersion="10"/> <application android:label="@string/app_name" android:icon="@drawable/ic_launcher" android:debuggable="true"> <activity android:name=".FlashlightActivity" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
- 手电筒源码
- Android 手电筒源码
- android手电筒+闪光灯基本源码
- Android手电筒+闪光灯基本源码
- Android 手电筒控制源码分析
- 手电筒
- Android实战简易教程<五十七>(分享小米手电筒源码)
- Forms 手电筒
- LED手电筒
- android手电筒
- Android手电筒
- Android手电筒
- 手电筒功能
- iphone手电筒
- 手电筒开发
- android 手电筒
- android手电筒
- 制作手电筒
- 使用RSA1024算法防止软件被Keygen
- Android 读取MCC, MNC
- pdf 文档 在线预览
- socket编程:从send函数和sendto函数参数的不同看TCP和UDP的差别。
- 进制前缀
- 手电筒源码
- 将Launcher2项目导入eclipse中。
- Debian下编译安装网卡驱动AR8151
- InitInstance函数
- android proguard注意事项(json的解析请注意)
- Trident API概述
- mac os下如何安装blade
- 安装win和openocd还有oflash
- dsp开发中cmd问题小结