您好,登錄后才能下訂單哦!
這篇文章主要介紹了Android如何實(shí)現(xiàn)手電筒功能,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
//Activity.xml package com.fq.flashlight; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.hardware.Camera; import android.hardware.Camera.Parameters; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class LightActivity extends Activity { Button button = null; //創(chuàng)建Button類型的變量 TextView textView = null; //創(chuàng)建TextView類型的變量 Camera camera = null; //創(chuàng)建Camera類型的變量 Parameters params = null; //創(chuàng)建Parameters類型的變量 Boolean flag = true; //創(chuàng)建Boolean類型的變量,用于做是否開啟的判斷 @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_light); button = (Button)findViewById(R.id.button); //獲取Button對象 textView = (TextView) findViewById(R.id.textView); //獲取TextView對象 button.setOnClickListener(new OnClickListener() { //點(diǎn)擊事件 @Override public void onClick(View v) { if(flag){ camera = Camera.open(); //獲取攝像頭 params = camera.getParameters(); params.setFlashMode(Parameters.FLASH_MODE_TORCH); camera.setParameters(params); camera.startPreview(); textView.setText(R.string.alert_on); button.setText(R.string.button_off); flag = false; }else{ params.setFlashMode(Parameters.FLASH_MODE_OFF); camera.setParameters(params); camera.stopPreview(); camera.release(); textView.setText(R.string.alert_off); button.setText(R.string.button_on); flag = true; } } }); } public void onBackPressed(){ //super.onBackPressed(); System.out.println("sdsfs"); new AlertDialog.Builder(this) .setTitle("確定退出嗎?") .setIcon(R.drawable.ic_launcher) .setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub LightActivity.this.finish(); System.exit(0); } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub } }) .show(); } }
<!--strings.xml--> <?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">BlueOcean手電筒</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="alert_on">手電筒當(dāng)前狀態(tài):開啟</string> <string name="alert_off">手電筒當(dāng)前狀態(tài):關(guān)閉</string> <string name="button_on">開啟手電</string> <string name="button_off">關(guān)閉手電</string> <string name="author">Author:BlueOcean</string> </resources>
<!--layout.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" > <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="34dp" android:text="@string/alert_off" android:textSize="22sp" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" android:layout_marginTop="26dp" android:text="@string/button_on" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/textView" android:layout_centerVertical="true" android:text="@string/author" android:textAppearance="?android:attr/textAppearanceSmall" /> </RelativeLayout>
<!-- manifest.xml--> <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.fq.flashlight" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <uses-permission android:name="android.permission.CAMERA"/> <uses-permission android:name="android.permission.FLASHLIGHT"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > </activity> <activity android:name=".LightActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Android如何實(shí)現(xiàn)手電筒功能”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。