Android 手電筒源碼

來源:互聯網
上載者:User

標籤:android   des   style   blog   http   java   

最近由於公司需要,做了一個手電筒,其實手電筒原理很簡單,就是調用照相機的閃光燈,控制閃光燈的開關,就可以實現手電筒的效果,

強調一下,代碼中一定要注意在結束的時候對閃光燈進行釋放,否則就會導致使用照相機的時候出現——無法串連到相機 這個問題

手電筒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.content.Context;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;@Overrideprotected 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()) {@Overridepublic 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;}}};@Overrideprotected 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();}@Overrideprotected void onResume() {// TODO Auto-generated method stubsuper.onResume();PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);wakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK| PowerManager.ON_AFTER_RELEASE, "test");wakeLock.acquire();}@Overrideprotected void onPause() {// TODO Auto-generated method stubsuper.onPause();if (wakeLock != null)wakeLock.release();}@Overridepublic 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>

實現的效果如下:



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.