Android(安卓)小应用之一个activity实现简易手电筒(内附免费源码)
16lz
2021-01-24
Android小应用之一个activity实现简易手电筒(内附免费源码)
- 1.activity代码
- 2.xml代码
- 3.资源文件代码
- 4.源码压缩包(免费)
今天爷爷说他手机更新之后找不到手电筒了,我心想是不是爷爷年纪大了的原因,然后拿过手机一看,竟然还真找不到,好吧,给爷爷到应用市场下载了一个手电筒,但是一个2.多mb的手电筒竟然还有广告,我想着还不如我自己写一个,虽然我是小小白,但好歹我也学了安卓,然后就搜博客找到一篇十分适合我的博客,最后成功制作出我的“玫瑰手电筒”(当然给我爷爷的会更加特别哈哈哈,在这里是面向广大用户,所以在有些地方做了一些修改)
参考博客
真机运行演示(模拟器没闪光灯的,点亮手电筒会报错)
手电图案灰色为关闭闪光灯,手电图案彩色为打开了闪光灯,然后下面的字也会切换
1.activity代码
public class MainActivity extends AppCompatActivity { ConstraintLayout mConstraintLayout; Button mButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mButton=findViewById(R.id.button); mConstraintLayout=findViewById(R.id.constraintLayout); final FlashUtils utils = new FlashUtils(this); mConstraintLayout.setSelected(false); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { utils.converse(); } }); } public class FlashUtils { private CameraManager manager; private Camera mCamera = null; private Context context; private boolean status = false;//记录手电筒状态 FlashUtils(Context context){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE); } this.context = context; } //打开手电筒 public void open() { if(status){//如果已经是打开状态,不需要打开 return; } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { try { manager.setTorchMode("0", true); } catch (Exception e) { e.printStackTrace(); } } else { PackageManager packageManager = context.getPackageManager(); FeatureInfo[] features = packageManager.getSystemAvailableFeatures(); for (FeatureInfo featureInfo : features) { if (PackageManager.FEATURE_CAMERA_FLASH.equals(featureInfo.name)) { // 判断设备是否支持闪光灯 if (null == mCamera) { mCamera = Camera.open(); } Camera.Parameters parameters = mCamera.getParameters(); parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH); mCamera.setParameters(parameters); mCamera.startPreview(); } } } status = true;//记录手电筒状态为打开 } //关闭手电筒 public void close() { if(!status){//如果已经是关闭状态,不需要打开 return; } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { try { manager.setTorchMode("0", false); } catch (Exception e) { e.printStackTrace(); } } else { if (mCamera != null) { mCamera.stopPreview(); mCamera.release(); mCamera = null; } } status = false;//记录手电筒状态为关闭 } //改变手电筒状态 public void converse(){ if(status){ close(); mConstraintLayout.setSelected(false); }else{ open(); mConstraintLayout.setSelected(true); } } }}
2.xml代码
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/constraintLayout" android:layout_height="match_parent" android:background="#000000" tools:context=".MainActivity"> <ImageView android:id="@+id/imageView3" android:layout_width="40dp" android:layout_height="40dp" android:layout_marginStart="20dp" android:layout_marginLeft="20dp" android:background="@drawable/love" app:layout_constraintBottom_toBottomOf="@+id/textView2" app:layout_constraintStart_toEndOf="@+id/textView2" app:layout_constraintTop_toTopOf="@+id/textView2" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:text="为你定制" android:textColor="#03A9F4" android:textSize="24sp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <ImageView android:layout_width="40dp" android:layout_height="40dp" android:layout_marginEnd="20dp" android:layout_marginRight="20dp" android:background="@drawable/love" app:layout_constraintBottom_toBottomOf="@+id/textView2" app:layout_constraintEnd_toStartOf="@+id/textView2" app:layout_constraintTop_toTopOf="@+id/textView2" /> <Button android:id="@+id/button" android:layout_width="300dp" android:layout_height="300dp" android:background="@drawable/flash_icon" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="'走路小心,注意安全'" android:textColor="@color/textcolor" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintBottom_toBottomOf="parent" android:layout_marginBottom="100dp" android:textSize="24sp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="'天天开心'" android:textColor="@color/text2color" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintBottom_toBottomOf="parent" android:layout_marginBottom="100dp" android:textSize="24sp"/></androidx.constraintlayout.widget.ConstraintLayout>
3.资源文件代码
text2color
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="#03A9F4" android:state_selected="false"/> <item android:color="#00000000" android:state_selected="true"/></selector>
textcolor
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="#000000" android:state_selected="false"/> <item android:color="#F44336" android:state_selected="true"/></selector>
flash_icon
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="true" android:drawable="@drawable/flash"/> <item android:state_selected="false" android:drawable="@drawable/unflash"/></selector>
4.源码压缩包(免费)
https://download.csdn.net/download/qq_46526828/12626160
突然觉得有时候想做一件事虽然是心血来潮,但是完成的感觉却是无与伦比的开心。
更多相关文章
- 在 Android(安卓)上使用 XML 和 JSON,第 2 部分: 交付混合了 JSON
- Android如果对APK进行加密,提高反编译难度(思路)
- Android(安卓)UI编程之自定义控件初步(上)——ImageButton
- 将android工程作为另一个工程的库
- Android沉浸式模式实现导航栏与状态栏的隐藏,上滑呼出导航栏与状
- android 自定义闪退Dialog 收集闪退信息
- android 一行代码,快速实现图片验证码(附android studio 版小demo)
- Android隐式启动Activity可能存在的坑
- Android(安卓)如何保证App切换到后台,或页面跳转后,重新打开APP、