surface用來處理一段單獨的緩沖區,這段緩沖區是由屏幕合成器來管理的。
surface 包含安全的內容,使用一些特殊的措施來阻止在其他的進程中復制surface內容。尤其是截屏和VNC服務。surface不能被硬件加速。它創建的緩沖區有幾種:BUFFERS,NORMAL,BLUR,DIM。
當surface不能被創建或更改大小時候,系統會拋出異常。surface創建過程中,創建了一個畫布,並且在之後的程序設計中我們可以使用。我們可以鎖定或解鎖一個surface,但解鎖時,只有調用post()或postAll() 是才會更新屏幕。
源碼:
package Android.view;
import android.graphics.*;
import android.os.Parcelable;
import android.os.Parcel;
import android.util.Log;
public class Surface implements Parcelable {
private static final String LOG_TAG = "Surface";
public static final int HIDDEN = 0x00000004;
public static final int HARDWARE = 0x00000010;
public static final int GPU = 0x00000028;
public static final int SECURE = 0x00000080;
public static final int NON_PREMULTIPLIED = 0x00000100;
public static final int PUSH_BUFFERS = 0x00000200;
public static final int FX_SURFACE_NORMAL = 0x00000000;
public static final int FX_SURFACE_BLUR = 0x00010000;
public static final int FX_SURFACE_DIM = 0x00020000;
public static final int FX_SURFACE_MASK = 0x000F0000;
public static final int SURFACE_HIDDEN = 0x01;
public static final int SURACE_FROZEN = 0x02;
public static final int SURFACE_DITHER = 0x04;
public static final int SURFACE_BLUR_FREEZE= 0x10;
public static final int ROTATION_0 = 0;
public static final int ROTATION_90 = 1;
public static final int ROTATION_180 = 2;
public static final int ROTATION_270 = 3;
public static final int FLAGS_ORIENTATION_ANIMATION_DISABLE = 0x000000001;
@SuppressWarnings("unused")
private int mSurface;
@SuppressWarnings("unused")
private int mSaveCount;
@SuppressWarnings("unused")
private Canvas mCanvas;
/**
* Exception thrown when a surface couldn't be created or resized
*/
public static class OutOfResourcesException extends Exception {
public OutOfResourcesException() {
}
public OutOfResourcesException(String name) {
super(name);
}
}
native private static void nativeClassInit();
static { nativeClassInit(); }
public Surface(SurfaceSession s,
int pid, int display, int w, int h, int format, int flags)
throws OutOfResourcesException {
mCanvas = new Canvas();
init(s,pid,display,w,h,format,flags);
}
public Surface() {
mCanvas = new Canvas();
}
public native void copyFrom(Surface o);
public native boolean isValid();
public native void clear();
public Canvas lockCanvas(Rect dirty) throws OutOfResourcesException {
return lockCanvasNative(dirty);
}
private native Canvas lockCanvasNative(Rect dirty);
public native void unlockCanvasAndPost(Canvas canvas);
public native void unlockCanvas(Canvas canvas);
public static native void openTransaction();
public static native void closeTransaction();
public static native void freezeDisplay(int display);
public static native void unfreezeDisplay(int display);
public static native void setOrientation(int display, int orientation, int flags);
public static void setOrientation(int display, int orientation) {
setOrientation(display, orientation, 0);
}
public native void setLayer(int zorder);
public native void setPosition(int x, int y);
public native void setSize(int w, int h);
public native void hide();
public native void show();
public native void setTransparentRegionHint(Region region);
public native void setAlpha(float alpha);
public native void setMatrix(float dsdx, float dtdx,
float dsdy, float dtdy);
public native void freeze();
public native void unfreeze();
public native void setFreezeTint(int tint);
public native void setFlags(int flags, int mask);
@Override
public String toString() {
return "Surface(native-token=" + mSurface + ")";
}
private Surface(Parcel source) throws OutOfResourcesException {
init(source);
}
public int describeContents() {
return 0;
}
public native void readFromParcel(Parcel source);
public native void writeToParcel(Parcel dest, int flags);
public static final Parcelable.Creator<Surface> CREATOR
= new Parcelable.Creator<Surface>()
{
public Surface createFromParcel(Parcel source) {
try {
return new Surface(source);
} catch (Exception e) {
Log.e(LOG_TAG, "Exception creating surface from parcel", e);
}
return null;
}
public Surface[] newArray(int size) {
return new Surface[size];
}
};
/* no user serviceable parts here ... */
@Override
protected void finalize() throws Throwable {
clear();
}
private native void init(SurfaceSession s,
int pid, int display, int w, int h, int format, int flags)
throws OutOfResourcesException;
private native void init(Parcel source);
}