手电筒源码

来源:互联网 发布: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>




原创粉丝点击