This is the 27th day of my participation in the August Genwen Challenge.More challenges in August

JetPack literacy article

I’ve been in the bottom of the business for a few years, so I don’t know much about some of the popular technologies, and certainly not much about JetPack, but the ubiquitous word JetPack is a sign of how hot it is, and this article is trying to figure out what it is and what it does.

What is JetPack

JetPack is a kit collection

Jetpack is a suite of libraries that help developers follow best practices, reduce boilerplate code, and write code that runs consistently across Android versions and devices, allowing developers to focus on the coding that really matters.

What this means is that the official wheels are more stable and compatible than the outside wheels, so you don’t have to worry about implementation and focus on business logic.

Why JetPack

I started to work on Android app in 2014. At that time, as long as I could write a Demo, I could find a job, which is not like now. With the development of Android, more and more Android developers, Android began to appear a variety of schools, a variety of technology selection, the project began to support MVC, MVP, MVVM, a variety of wheels began to appear.

I remember a Service passing values to an Activity when quite a few people chose to broadcast.

Android developers also have no choice but to choose from a variety of technologies, resulting in uneven quality applications. Google finally unveiled the new Android Jetpack at Google I/O 2018.

JetPack is the Android app development toolset, a collection of libraries, tools, and tutorials designed to make it easier for developers to write great apps. Jetpack is actually quite large, consisting of basic components, architectural components, behavioral components, and UI components. So from now on, people can use JetPack to meet a lot of needs.

What does JetPack have to do with AndroidX

Jetpack is a generic, advertised code name, and AndroidX is the location of Jetpack's included support library

What does JetPack include

The official figure

Folk finishing

There are four JetPack categories: Architecture, Foundationy, Behavior, and UI.

5. JetPack CameraX practice

According to the previous way, how complicated it is to write a Camera, we need to create a SurfaceView and render the preview data to the SurfaceView before it can be displayed. The code takes at least hundreds of lines, and the more difficult thing is the compatibility problem, which may be mirrored or reversed.

So let’s see how CameraX does that.

Let’s see what happens

Implementation code:

JAVA 1.8 or above is required

JDK 8 compileOptions {sourceCompatibility JavUncomfortable.VERSION_1_8 targetCompatibility javUncomfortable.VERSION_1_8 }Copy the code

Layout file

<? The 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" tools:context=".MainActivity"> <androidx.camera.view.PreviewView android:id="@+id/camera_view" android:layout_width="match_parent" android:layout_height="match_parent" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" Android :layout_alignParentEnd="true" Android :layout_alignParentBottom="true" Android :onClick="Switch" Android :text=" toggle"  /> </RelativeLayout>Copy the code

Activity

public class MainActivity extends AppCompatActivity { private PreviewView cameraView; // 0 for rear facing and 1 for front facing int currentFacingSide; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PermissionChecker.PERMISSION_GRANTED) { startCamera(); } } private void startCamera() { cameraView = findViewById(R.id.camera_view); final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(this); cameraProviderFuture.addListener(new Runnable() { @Override public void run() { try { ProcessCameraProvider cameraProvider = cameraProviderFuture.get(); bindPreview(cameraProvider); } catch (ExecutionException | InterruptedException e) { e.printStackTrace(); } } }, ContextCompat.getMainExecutor(this)); } private void bindPreview(ProcessCameraProvider cameraProvider) { CameraSelector cameraSelector; Preview preview = new Preview.Builder().build(); ImageCapture imageCapture = new ImageCapture.Builder().build(); preview.setSurfaceProvider(cameraView.createSurfaceProvider()); if (currentFacingSide == 0) { cameraSelector = new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_FRONT).build(); currentFacingSide = 1; } else { cameraSelector = new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build(); currentFacingSide = 0; } cameraProvider.unbindAll(); Camera camera = cameraProvider.bindToLifecycle(this, cameraSelector, preview, imageCapture); } public void Switch(View view) { startCamera(); }}Copy the code