Android手電筒案例

來源:互聯網
上載者:User

標籤:android手電筒案例

最近在學習Android的開發,寫了個手電筒的案例,分享一下!

//Activity.xmlpackage 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;//建立Button類型的變數TextView textView = null;//建立TextView類型的變數Camera camera = null;//建立Camera類型的變數Parameters params = null;//建立Parameters類型的變數Boolean flag = true;//建立Boolean類型的變數,用於做是否開啟的判斷@Overrideprotected 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() { //點擊事件@Overridepublic 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() {@Overridepublic void onClick(DialogInterface arg0, int arg1) {// TODO Auto-generated method stubLightActivity.this.finish();System.exit(0);}}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {@Overridepublic 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">手電筒目前狀態:開啟</string><string name="alert_off">手電筒目前狀態:關閉</string><string name="button_on">開啟手電</string><string name="button_off">關閉手電</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>


本文出自 “BlueOcean” 部落格,請務必保留此出處http://bluocean.blog.51cto.com/8199259/1554839

Android手電筒案例

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.