To create a Torch Flashlight application for Android with Sketchware follow the steps given below.
1. Create a new project in Sketchware. In VIEW area add an ImageView imageview1. Set it's width and height to 100, and scale type to FIT_XY.
2. Using Image Manager add two images ic_flash_on_black and ic_flash_off_black.
3. Set ic_flash_off_black as the image of imageview1.
4. In Library manager switch on AppCompat and Design.
5. Add a Camera component.
6. Add two Boolean variables: flashLightStatus and hasCameraFlash.
7. Add two More Blocks: flashLightOn and flashLightOff.
8. In onCreate event, use add source directly block and put following code:
hasCameraFlash = getPackageManager(). hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
9. In More Block flashLightOn, use add source directly block and put following code:
android.hardware.camera2.CameraManager cameraManager = (android.hardware.camera2.CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
String cameraId = cameraManager.getCameraIdList()[0]; cameraManager.setTorchMode(cameraId, true);
flashLightStatus = true; imageview1.setImageResource(R.drawable.ic_flash_on_black); } catch (android.hardware.camera2.CameraAccessException e) { }
10. In More Block flashLightOff, use add source directly block and put following code:
android.hardware.camera2.CameraManager cameraManager = (android.hardware.camera2.CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
String cameraId = cameraManager.getCameraIdList()[0]; cameraManager.setTorchMode(cameraId, false);
flashLightStatus = false; imageview1.setImageResource(R.drawable.ic_flash_off_black); } catch (android.hardware.camera2.CameraAccessException e) { }
11. Add imageview1 onClick event. Here use blocks as shown in image below.
12. Save and run the project.
Komentar