Android Camera開發(fā)手電筒功能
這是一個(gè)簡(jiǎn)單的運(yùn)用Android Camera開發(fā)手電筒功能,AndroidManifest.xml文件的入口是startapp,這個(gè)文件沒上傳上來,大家可以自己寫。
flashlight.java
package com.android.app;
import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class Main extends Activity {
private boolean isopent = false;
private Camera camera;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
View view = View.inflate(this, R.layout.main, null);
setContentView(view);
TextView img_but = (TextView) findViewById(R.id.main_img);
img_but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (!isopent) {
Toast.makeText(getApplicationContext(), "您已經(jīng)打開了手電筒", 0)
.show();
camera = Camera.open();
Parameters params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview(); // 開始亮燈
isopent = true;
} else {
Toast.makeText(getApplicationContext(), "關(guān)閉了手電筒",
Toast.LENGTH_SHORT).show();
camera.stopPreview(); // 關(guān)掉亮燈
camera.release(); // 關(guān)掉照相機(jī)
isopent = false;
}
}
});
}
}
布局文件代碼
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="<a rel="nofollow" target="_blank">http://schemas.android.com/apk/res/android"</a> android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/main_img" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/main_body"> </TextView> </LinearLayout>
AndroidManifest.xml文件
<manifest xmlns:android="<a rel="nofollow" target="_blank">http://schemas.android.com/apk/res/android"</a>
package="com.android.app"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name=".AppStart" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Main" >
</activity>
</application>
<!-- 攝像頭、手電筒 -->
<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" />
</manifest>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Kotlin中內(nèi)置函數(shù)的用法和區(qū)別總結(jié)
眾所周知相比Java, Kotlin提供了不少高級(jí)語(yǔ)法特性。對(duì)于一個(gè)Kotlin的初學(xué)者來說經(jīng)常會(huì)寫出一些不夠優(yōu)雅的代碼。下面這篇文章主要給大家介紹了關(guān)于Kotlin中內(nèi)置函數(shù)的用法和區(qū)別的相關(guān)資料,需要的朋友可以參考下2018-06-06
Android中關(guān)于Binder常見面試問題小結(jié)
這篇文章主要介紹了Android中關(guān)于Binder幾個(gè)面試問題,binder是一種進(jìn)程間通訊的機(jī)制,進(jìn)程間通訊需要了解用戶空間和內(nèi)核空間,本文通過示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06
解決android.support.v4.content.FileProvide找不到的問題
這篇文章主要介紹了解決android.support.v4.content.FileProvide找不到的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android 實(shí)現(xiàn)監(jiān)聽的四種方法詳解實(shí)例代碼
這篇文章主要介紹了Android 實(shí)現(xiàn)監(jiān)聽的方法詳解實(shí)例代碼的相關(guān)資料,這里整理了四種方法,需要的朋友可以參考下2016-10-10
Android自定義流式布局實(shí)現(xiàn)淘寶搜索記錄
這篇文章主要為大家詳細(xì)介紹了Android自定義流式布局實(shí)現(xiàn)淘寶搜索記錄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10
android view實(shí)現(xiàn)一張圖片的漸隱效果
這篇文章主要為大家詳細(xì)介紹了android view實(shí)現(xiàn)一張圖片的漸隱效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07

