简体   繁体   中英

How is the interface being implemented here?

This is a code snippet from the android developers website.

class Preview extends ViewGroup implements SurfaceHolder.Callback {

    SurfaceView mSurfaceView;
    SurfaceHolder mHolder;

    Preview(Context context) {
        super(context);

        mSurfaceView = new SurfaceView(context);
        addView(mSurfaceView);

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = mSurfaceView.getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }
...
}  

Here SurfaceHolder is an interface , SurfaceView is a class. mSurfaceView.getHolder(); returns SurfaceHolder . I know how to create annonymous classes that implement interfaces, and it is possible to do something like

SurfaceHolder mHolder = new SurfaceHolder{ ... }
to assign a reference to the annonymous class to a variable name. But how is the interface being implemented in the above code snippet?

The code snippet can be found here .

The key is further down the page:

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    // Now that the size is known, set up the camera parameters and begin
    // the preview.
    Camera.Parameters parameters = mCamera.getParameters();
    parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
    requestLayout();
    mCamera.setParameters(parameters);

    /*
      Important: Call startPreview() to start updating the preview surface. Preview must be
      started before you can take a picture.
    */
    mCamera.startPreview();
}

This is the method defined in the interface, thus the interface is implemented here. For readability, Google's example just splits the class into multiple code segments.

(In reality there's several methods in the interface, all of them defined on that page in separate sections.)

Here SurfaceHolder is an interface

SurfaceHolder is a interface, with a nested Interface Callback. And this Callback will be implemented in the Preview class.

With this line you get the Interface of the SurfaceView.

mHolder = mSurfaceView.getHolder();

SurfaceView is the class and SurfaceHolder is the Interface which provides the methods to work with the class. So SurfaceHolder is the Interface which is used like the List Interface in combination of an ArrayList.

The SurfaceHolder.Callback interface has three methods, what I can see from the docs. In the code only surfaceChanged() and surfaceDestroyed() is implemented.

This line

 mHolder.addCallback(this);

adds the Callback to the SurfaceHolder.

Google do that a lot on their examples. They don't really show the complete code and not always shows the @Override . If you to the actual interface definition: http://developer.android.com/reference/android/view/SurfaceHolder.Callback.html you'll see the 3 methods surfaceChanged , surfaceCreated and surfaceDestroyed

now if you search in the page (ctrl+F) of your original link for those methods, you'll see their implementations.

edit:

I'm very sorry, with your comment now I understood the question.

The trick is that that the SurfaceView (just like any class) have a bunch of private ___ objects inside it, one of them is an object that implements the SurfaceHolder interface. With the call mSurfaceView.getHolder(), you're just getting a reference to the object that implements the interface that is originally referenced inside surfaceview.

It's a normal Java thing to create objects of interfaces, but you need to explicity show that interface, for example runnable on thread:

Thread t = new Thread(new Runnable(){
    @Override
    run(){
       // do stuff
    }
}).start();

if you're curious you can see how it's done on the SurfaceView in here, line 694

https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/view/SurfaceView.java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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