Android studio編寫簡單的手電筒APP
很不錯的手電筒APP,分享給大家,希望大家喜歡。
1. Java代碼
package com.example.goo.gfire; import android.app.Activity; import android.hardware.Camera; import android.hardware.Camera.Parameters; import android.os.Bundle; import android.view.View; import android.widget.Button; public class FireActivity extends Activity { private Button button; private Camera camera = Camera.open(); private Parameters parameters; private boolean islight=true; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fire); button = (Button) this.findViewById(R.id.bn); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (islight) { camera.startPreview(); parameters = camera.getParameters(); parameters.setFlashMode(Parameters.FLASH_MODE_TORCH); camera.setParameters(parameters); islight = false ; button.setText("關(guān)"); } else { parameters = camera.getParameters(); parameters.setFlashMode(Parameters.FLASH_MODE_OFF); camera.setParameters(parameters); islight = true; button.setText("開"); } } }); } }
2. FireActivity.java
package com.example.goo.gfire; import android.app.Activity; import android.hardware.Camera; import android.hardware.Camera.Parameters; import android.os.Bundle; import android.view.View; import android.widget.Button; public class FireActivity extends Activity { private Button button; private Camera camera = Camera.open(); private Parameters parameters; private boolean islight=true; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fire); button = (Button) this.findViewById(R.id.bn); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (islight) { camera.startPreview(); parameters = camera.getParameters(); parameters.setFlashMode(Parameters.FLASH_MODE_TORCH); camera.setParameters(parameters); islight = false ; button.setText("關(guān)"); } else { parameters = camera.getParameters(); parameters.setFlashMode(Parameters.FLASH_MODE_OFF); camera.setParameters(parameters); islight = true; button.setText("開"); } } }); } }
3. content_fire.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.example.goo.gfire.FireActivity" tools:showIn="@layout/activity_fire"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="電筒模式" android:textSize="34sp" android:textStyle="bold" android:gravity="center"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/bn" android:textOff="關(guān)" android:textOn="開" android:height="400dp" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="59dp" android:fontFeatureSettings="@string/auth_google_play_services_client_facebook_display_name" android:textColor="@color/common_signin_btn_dark_text_focused" android:textStyle="bold" android:textSize="100sp" /> </RelativeLayout>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
模仿美團點評的Android應(yīng)用中價格和購買欄懸浮固定的效果
這篇文章主要介紹了模仿美團點評的Android應(yīng)用中價格和購買欄懸浮固定的效果,文章后半還針對快速滑動操作給出了一個響應(yīng)優(yōu)化的方法,需要的朋友可以參考下2016-04-04Android開發(fā)ListView中下拉刷新上拉加載及帶列的橫向滾動實現(xiàn)方法
這篇文章主要介紹了Android開發(fā)ListView中下拉刷新上拉加載及帶列的橫向滾動實現(xiàn)方法的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07Android編程中TextView寬度過大導(dǎo)致Drawable無法居中問題解決方法
這篇文章主要介紹了Android編程中TextView寬度過大導(dǎo)致Drawable無法居中問題解決方法,以實例形式較為詳細的分析了TextView設(shè)置及xml布局與調(diào)用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10Flutter?SystemChrome控制應(yīng)用程序的系統(tǒng)級別行為
這篇文章主要為大家介紹了Flutter?SystemChrome用來控制應(yīng)用程序的系統(tǒng)級別行為步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05Android Caused by: java.lang.ClassNotFoundException解決辦法
這篇文章主要介紹了Android Caused by: java.lang.ClassNotFoundException解決辦法的相關(guān)資料,需要的朋友可以參考下2017-03-03淺談Android RecyclerView UI的滾動控件示例
本篇文章主要介紹了淺談Android RecyclerView UI的滾動控件示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02