Understanding the click in camera2 API in Camera2Basic on Android

I am trying to understand how the camera2 api works in the Google Camera2Basic sample code. Specifically, how does the Image button register a snapshot?

In onCreateViewCreated:

@Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
    view.findViewById(R.id.picture).setOnClickListener(this);
    view.findViewById(R.id.info).setOnClickListener(this);
    mTextureView = (AutoFitTextureView) view.findViewById(R.id.texture);
}

      

So setOnClickListener()

registering a click? But where does this go? I can see that this one has been passed, but I don't understand what's going on.

What I used to do was do something like customize a button in onCreateView()

and wire it setOnClickListener()

to some action like this:

photoButton = (Button)v.findViewById(R.id.picture);
photoButton.setOnClickListener(new View.onSetClickListener() {
    @Override
    public void onClick(View v) {
        //some action
    }
});

      

+3


source to share


1 answer


The same thing happens in the sample code. However, this looks slightly different because it Camera2BasicFragment

implements OnClickListener

. Therefore, when the onClickListener is set, it is this

indicated that this activity will override the method onClick

. Therefore, when the button is clicked, the method onClick

in the class is automatically called .



@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.picture: {
            takePicture();
            break;
        }
        case R.id.info: {
            Activity activity = getActivity();
            if (null != activity) {
                new AlertDialog.Builder(activity)
                        .setMessage(R.string.intro_message)
                        .setPositiveButton(android.R.string.ok, null)
                        .show();
            }
            break;
        }
    }
}

      

+3


source







All Articles