Methods Summary |
---|
private void | applyTransformMatrix()
if (mMatrixChanged && mLayer != null) {
mLayer.setTransform(mMatrix);
mMatrixChanged = false;
}
|
private void | applyUpdate()
if (mLayer == null) {
return;
}
synchronized (mLock) {
if (mUpdateLayer) {
mUpdateLayer = false;
} else {
return;
}
}
mLayer.prepare(getWidth(), getHeight(), mOpaque);
mLayer.updateSurfaceTexture();
if (mListener != null) {
mListener.onSurfaceTextureUpdated(mSurface);
}
|
public void | buildLayer()Calling this method has no effect.
|
protected void | destroyHardwareResources()
super.destroyHardwareResources();
destroySurface();
invalidateParentCaches();
invalidate(true);
|
private void | destroySurface()
if (mLayer != null) {
mLayer.detachSurfaceTexture();
boolean shouldRelease = true;
if (mListener != null) {
shouldRelease = mListener.onSurfaceTextureDestroyed(mSurface);
}
synchronized (mNativeWindowLock) {
nDestroyNativeWindow();
}
mLayer.destroy();
if (shouldRelease) mSurface.release();
mSurface = null;
mLayer = null;
mHadSurface = true;
}
|
public final void | draw(android.graphics.Canvas canvas)Subclasses of TextureView cannot do their own rendering
with the {@link Canvas} object.
// NOTE: Maintain this carefully (see View.java)
mPrivateFlags = (mPrivateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN;
applyUpdate();
applyTransformMatrix();
|
public android.graphics.Bitmap | getBitmap()Returns a {@link android.graphics.Bitmap} representation of the content
of the associated surface texture. If the surface texture is not available,
this method returns null.
The bitmap returned by this method uses the {@link Bitmap.Config#ARGB_8888}
pixel format and its dimensions are the same as this view's.
Do not invoke this method from a drawing method
({@link #onDraw(android.graphics.Canvas)} for instance).
If an error occurs during the copy, an empty bitmap will be returned.
return getBitmap(getWidth(), getHeight());
|
public android.graphics.Bitmap | getBitmap(int width, int height)Returns a {@link android.graphics.Bitmap} representation of the content
of the associated surface texture. If the surface texture is not available,
this method returns null.
The bitmap returned by this method uses the {@link Bitmap.Config#ARGB_8888}
pixel format.
Do not invoke this method from a drawing method
({@link #onDraw(android.graphics.Canvas)} for instance).
If an error occurs during the copy, an empty bitmap will be returned.
if (isAvailable() && width > 0 && height > 0) {
return getBitmap(Bitmap.createBitmap(getResources().getDisplayMetrics(),
width, height, Bitmap.Config.ARGB_8888));
}
return null;
|
public android.graphics.Bitmap | getBitmap(android.graphics.Bitmap bitmap)Copies the content of this view's surface texture into the specified
bitmap. If the surface texture is not available, the copy is not executed.
The content of the surface texture will be scaled to fit exactly inside
the specified bitmap.
Do not invoke this method from a drawing method
({@link #onDraw(android.graphics.Canvas)} for instance).
If an error occurs, the bitmap is left unchanged.
if (bitmap != null && isAvailable()) {
applyUpdate();
applyTransformMatrix();
// This case can happen if the app invokes setSurfaceTexture() before
// we are able to create the hardware layer. We can safely initialize
// the layer here thanks to the validate() call at the beginning of
// this method
if (mLayer == null && mUpdateSurface) {
getHardwareLayer();
}
if (mLayer != null) {
mLayer.copyInto(bitmap);
}
}
return bitmap;
|
HardwareLayer | getHardwareLayer()
// NOTE: Maintain these two lines very carefully (see View.java)
mPrivateFlags |= PFLAG_DRAWN | PFLAG_DRAWING_CACHE_VALID;
mPrivateFlags &= ~PFLAG_DIRTY_MASK;
if (mLayer == null) {
if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) {
return null;
}
mLayer = mAttachInfo.mHardwareRenderer.createTextureLayer();
if (!mUpdateSurface) {
// Create a new SurfaceTexture for the layer.
mSurface = new SurfaceTexture(false);
mLayer.setSurfaceTexture(mSurface);
}
mSurface.setDefaultBufferSize(getWidth(), getHeight());
nCreateNativeWindow(mSurface);
mSurface.setOnFrameAvailableListener(mUpdateListener, mAttachInfo.mHandler);
if (mListener != null && !mUpdateSurface) {
mListener.onSurfaceTextureAvailable(mSurface, getWidth(), getHeight());
}
mLayer.setLayerPaint(mLayerPaint);
}
if (mUpdateSurface) {
// Someone has requested that we use a specific SurfaceTexture, so
// tell mLayer about it and set the SurfaceTexture to use the
// current view size.
mUpdateSurface = false;
// Since we are updating the layer, force an update to ensure its
// parameters are correct (width, height, transform, etc.)
updateLayer();
mMatrixChanged = true;
mLayer.setSurfaceTexture(mSurface);
mSurface.setDefaultBufferSize(getWidth(), getHeight());
}
applyUpdate();
applyTransformMatrix();
return mLayer;
|
public int | getLayerType()Always returns {@link #LAYER_TYPE_HARDWARE}.
return LAYER_TYPE_HARDWARE;
|
public android.graphics.SurfaceTexture | getSurfaceTexture()Returns the {@link SurfaceTexture} used by this view. This method
may return null if the view is not attached to a window or if the surface
texture has not been initialized yet.
return mSurface;
|
public android.view.TextureView$SurfaceTextureListener | getSurfaceTextureListener()Returns the {@link SurfaceTextureListener} currently associated with this
texture view.
return mListener;
|
public android.graphics.Matrix | getTransform(android.graphics.Matrix transform)Returns the transform associated with this texture view.
if (transform == null) {
transform = new Matrix();
}
transform.set(mMatrix);
return transform;
|
boolean | hasStaticLayer()
return true;
|
private void | init()
mLayerPaint = new Paint();
|
public boolean | isAvailable()Returns true if the {@link SurfaceTexture} associated with this
TextureView is available for rendering. When this method returns
true, {@link #getSurfaceTexture()} returns a valid surface texture.
return mSurface != null;
|
public boolean | isOpaque(){@inheritDoc}
return mOpaque;
|
public android.graphics.Canvas | lockCanvas()Start editing the pixels in the surface. The returned Canvas can be used
to draw into the surface's bitmap. A null is returned if the surface has
not been created or otherwise cannot be edited. You will usually need
to implement
{@link SurfaceTextureListener#onSurfaceTextureAvailable(android.graphics.SurfaceTexture, int, int)}
to find out when the Surface is available for use.
The content of the Surface is never preserved between unlockCanvas()
and lockCanvas(), for this reason, every pixel within the Surface area
must be written. The only exception to this rule is when a dirty
rectangle is specified, in which case, non-dirty pixels will be
preserved.
This method can only be used if the underlying surface is not already
owned by another producer. For instance, if the TextureView is being used
to render the camera's preview you cannot invoke this method.
return lockCanvas(null);
|
public android.graphics.Canvas | lockCanvas(android.graphics.Rect dirty)Just like {@link #lockCanvas()} but allows specification of a dirty
rectangle. Every pixel within that rectangle must be written; however
pixels outside the dirty rectangle will be preserved by the next call
to lockCanvas().
This method can return null if the underlying surface texture is not
available (see {@link #isAvailable()} or if the surface texture is
already connected to an image producer (for instance: the camera,
OpenGL, a media player, etc.)
if (!isAvailable()) return null;
if (mCanvas == null) {
mCanvas = new Canvas();
}
synchronized (mNativeWindowLock) {
if (!nLockCanvas(mNativeWindow, mCanvas, dirty)) {
return null;
}
}
mSaveCount = mCanvas.save();
return mCanvas;
|
private native void | nCreateNativeWindow(android.graphics.SurfaceTexture surface)
|
private native void | nDestroyNativeWindow()
|
private static native boolean | nLockCanvas(long nativeWindow, android.graphics.Canvas canvas, android.graphics.Rect dirty)
|
private static native void | nUnlockCanvasAndPost(long nativeWindow, android.graphics.Canvas canvas)
|
protected void | onAttachedToWindow()
super.onAttachedToWindow();
if (!isHardwareAccelerated()) {
Log.w(LOG_TAG, "A TextureView or a subclass can only be "
+ "used with hardware acceleration enabled.");
}
if (mHadSurface) {
invalidate(true);
mHadSurface = false;
}
|
protected void | onDetachedFromWindowInternal()
destroySurface();
super.onDetachedFromWindowInternal();
|
protected final void | onDraw(android.graphics.Canvas canvas)Subclasses of TextureView cannot do their own rendering
with the {@link Canvas} object.
|
protected void | onSizeChanged(int w, int h, int oldw, int oldh)
super.onSizeChanged(w, h, oldw, oldh);
if (mSurface != null) {
mSurface.setDefaultBufferSize(getWidth(), getHeight());
updateLayer();
if (mListener != null) {
mListener.onSurfaceTextureSizeChanged(mSurface, getWidth(), getHeight());
}
}
|
protected void | onVisibilityChanged(View changedView, int visibility)
super.onVisibilityChanged(changedView, visibility);
if (mSurface != null) {
// When the view becomes invisible, stop updating it, it's a waste of CPU
// To cancel updates, the easiest thing to do is simply to remove the
// updates listener
if (visibility == VISIBLE) {
if (mLayer != null) {
mSurface.setOnFrameAvailableListener(mUpdateListener, mAttachInfo.mHandler);
}
updateLayerAndInvalidate();
} else {
mSurface.setOnFrameAvailableListener(null);
}
}
|
public void | setLayerPaint(android.graphics.Paint paint)
setLayerType(/* ignored */ 0, paint);
|
public void | setLayerType(int layerType, android.graphics.Paint paint)The layer type of a TextureView is ignored since a TextureView is always
considered to act as a hardware layer. The optional paint supplied to this
method will however be taken into account when rendering the content of
this TextureView.
if (paint != mLayerPaint) {
mLayerPaint = paint == null ? new Paint() : paint;
invalidate();
}
|
public void | setOpaque(boolean opaque)Indicates whether the content of this TextureView is opaque. The
content is assumed to be opaque by default.
if (opaque != mOpaque) {
mOpaque = opaque;
if (mLayer != null) {
updateLayerAndInvalidate();
}
}
|
public void | setSurfaceTexture(android.graphics.SurfaceTexture surfaceTexture)Set the {@link SurfaceTexture} for this view to use. If a {@link
SurfaceTexture} is already being used by this view, it is immediately
released and not be usable any more. The {@link
SurfaceTextureListener#onSurfaceTextureDestroyed} callback is not
called for the previous {@link SurfaceTexture}. Similarly, the {@link
SurfaceTextureListener#onSurfaceTextureAvailable} callback is not
called for the {@link SurfaceTexture} passed to setSurfaceTexture.
The {@link SurfaceTexture} object must be detached from all OpenGL ES
contexts prior to calling this method.
if (surfaceTexture == null) {
throw new NullPointerException("surfaceTexture must not be null");
}
if (mSurface != null) {
mSurface.release();
}
mSurface = surfaceTexture;
mUpdateSurface = true;
invalidateParentIfNeeded();
|
public void | setSurfaceTextureListener(android.view.TextureView$SurfaceTextureListener listener)Sets the {@link SurfaceTextureListener} used to listen to surface
texture events.
mListener = listener;
|
public void | setTransform(android.graphics.Matrix transform)Sets the transform to associate with this texture view.
The specified transform applies to the underlying surface
texture and does not affect the size or position of the view
itself, only of its content.
Some transforms might prevent the content from drawing
all the pixels contained within this view's bounds. In such
situations, make sure this texture view is not marked opaque.
mMatrix.set(transform);
mMatrixChanged = true;
invalidateParentIfNeeded();
|
public void | unlockCanvasAndPost(android.graphics.Canvas canvas)Finish editing pixels in the surface. After this call, the surface's
current pixels will be shown on the screen, but its content is lost,
in particular there is no guarantee that the content of the Surface
will remain unchanged when lockCanvas() is called again.
if (mCanvas != null && canvas == mCanvas) {
canvas.restoreToCount(mSaveCount);
mSaveCount = 0;
synchronized (mNativeWindowLock) {
nUnlockCanvasAndPost(mNativeWindow, mCanvas);
}
}
|
private void | updateLayer()
synchronized (mLock) {
mUpdateLayer = true;
}
|
private void | updateLayerAndInvalidate()
synchronized (mLock) {
mUpdateLayer = true;
}
invalidate();
|