简体   繁体   English

了解Android中Camera2Basic中camera2 API中的按钮单击

[英]Understanding the button click in camera2 API in Camera2Basic in Android

I'm trying to understand how the camera2 api works in the Google Camera2Basic sample code. 我正在尝试了解camera2 api在Google Camera2Basic示例代码中的工作原理。 Specifically, how does the 'Picture' button register the shot? 具体来说,“图片”按钮如何注册镜头?

In onCreateViewCreated: 在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() registers the click? 所以setOnClickListener()注册了点击? But what does it go to? 但是它到底是什么? I see that this is passed in but I don't understand what's going on. 我看到是传递但我不明白发生了什么。

What I've normally done is do something like set up a button in onCreateView() and wire its setOnClickListener() to some action like so: 我通常做的是做一些事情,比如在onCreateView()设置一个按钮,并将其setOnClickListener()到某个动作,如下所示:

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

This same thing is happening in the example code. 在示例代码中也发生了同样的事情。 However, it looks a little different because the Camera2BasicFragment activity is implementing OnClickListener . 但是,它看起来有点不同,因为Camera2BasicFragment活动正在实现OnClickListener Therefore, when the onClickListener is set, this is given to say that this activity will override the onClick method. 因此,当onClickListener设置, this是考虑到说,本次活动将覆盖onClick方法。 So when the button is clicked the onClick method in the class is automatically called. 因此,当单击该按钮时,将自动调用类中的onClick方法。

@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;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM