Android studio编写简单的手电筒APP
很不错的手电筒APP,分享给大家,希望大家喜欢。
1.Java代码
packagecom.example.goo.gfire; importandroid.app.Activity; importandroid.hardware.Camera; importandroid.hardware.Camera.Parameters; importandroid.os.Bundle; importandroid.view.View; importandroid.widget.Button; publicclassFireActivityextendsActivity{ privateButtonbutton; privateCameracamera=Camera.open(); privateParametersparameters; privatebooleanislight=true; publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_fire); button=(Button)this.findViewById(R.id.bn); button.setOnClickListener(newView.OnClickListener(){ @Override publicvoidonClick(Viewv){ if(islight){ camera.startPreview(); parameters=camera.getParameters(); parameters.setFlashMode(Parameters.FLASH_MODE_TORCH); camera.setParameters(parameters); islight=false ; button.setText("关"); }else{ parameters=camera.getParameters(); parameters.setFlashMode(Parameters.FLASH_MODE_OFF); camera.setParameters(parameters); islight=true; button.setText("开"); } } }); } }
2.FireActivity.java
packagecom.example.goo.gfire; importandroid.app.Activity; importandroid.hardware.Camera; importandroid.hardware.Camera.Parameters; importandroid.os.Bundle; importandroid.view.View; importandroid.widget.Button; publicclassFireActivityextendsActivity{ privateButtonbutton; privateCameracamera=Camera.open(); privateParametersparameters; privatebooleanislight=true; publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_fire); button=(Button)this.findViewById(R.id.bn); button.setOnClickListener(newView.OnClickListener(){ @Override publicvoidonClick(Viewv){ if(islight){ camera.startPreview(); parameters=camera.getParameters(); parameters.setFlashMode(Parameters.FLASH_MODE_TORCH); camera.setParameters(parameters); islight=false ; button.setText("关"); }else{ parameters=camera.getParameters(); parameters.setFlashMode(Parameters.FLASH_MODE_OFF); camera.setParameters(parameters); islight=true; button.setText("开"); } } }); } }
3.content_fire.xml
<?xmlversion="1.0"encoding="utf-8"?> <RelativeLayoutxmlns: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="关" 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>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。