summaryrefslogtreecommitdiff
path: root/core/java/android/view/TextureView.java
diff options
context:
space:
mode:
authorRomain Guy <romainguy@google.com>2011-06-14 16:45:55 -0700
committerRomain Guy <romainguy@google.com>2011-06-14 17:06:53 -0700
commit77a811610f99e21da7f88dafef60d09f345d0506 (patch)
treef9342ace8ae4541f75c17a3f8531d3c6090de474 /core/java/android/view/TextureView.java
parent113543c909baadb911cfda6acef056137e16f191 (diff)
Add TextureView.getBitmap()
This API can be used to get a Bitmap copy of the content of a TextureView. Change-Id: I07522216c353720fba5cab333174f58f484eb911
Diffstat (limited to 'core/java/android/view/TextureView.java')
-rw-r--r--core/java/android/view/TextureView.java94
1 files changed, 92 insertions, 2 deletions
diff --git a/core/java/android/view/TextureView.java b/core/java/android/view/TextureView.java
index bc1ad3c8e92c..4daa892d3e35 100644
--- a/core/java/android/view/TextureView.java
+++ b/core/java/android/view/TextureView.java
@@ -17,6 +17,7 @@
package android.view;
import android.content.Context;
+import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.SurfaceTexture;
@@ -89,6 +90,8 @@ import android.util.Log;
* @see SurfaceTexture
*/
public class TextureView extends View {
+ private static final String LOG_TAG = "TextureView";
+
private HardwareLayer mLayer;
private SurfaceTexture mSurface;
private SurfaceTextureListener mListener;
@@ -148,7 +151,7 @@ public class TextureView extends View {
super.onAttachedToWindow();
if (!isHardwareAccelerated()) {
- Log.w("TextureView", "A TextureView or a subclass can only be "
+ Log.w(LOG_TAG, "A TextureView or a subclass can only be "
+ "used with hardware acceleration enabled.");
}
}
@@ -293,8 +296,95 @@ public class TextureView extends View {
}
/**
+ * <p>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.</p>
+ *
+ * <p>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.</p>
+ *
+ * <p><strong>Do not</strong> invoke this method from a drawing method
+ * ({@link #onDraw(android.graphics.Canvas)} for instance).</p>
+ *
+ * @return A valid {@link Bitmap.Config#ARGB_8888} bitmap, or null if the surface
+ * texture is not available or the width &lt;= 0 or the height &lt;= 0
+ *
+ * @see #isAvailable()
+ * @see #getBitmap(android.graphics.Bitmap)
+ * @see #getBitmap(int, int)
+ */
+ public Bitmap getBitmap() {
+ return getBitmap(getWidth(), getHeight());
+ }
+
+ /**
+ * <p>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.</p>
+ *
+ * <p>The bitmap returned by this method uses the {@link Bitmap.Config#ARGB_8888}
+ * pixel format.</p>
+ *
+ * <p><strong>Do not</strong> invoke this method from a drawing method
+ * ({@link #onDraw(android.graphics.Canvas)} for instance).</p>
+ *
+ * @param width The width of the bitmap to create
+ * @param height The height of the bitmap to create
+ *
+ * @return A valid {@link Bitmap.Config#ARGB_8888} bitmap, or null if the surface
+ * texture is not available or width is &lt;= 0 or height is &lt;= 0
+ *
+ * @see #isAvailable()
+ * @see #getBitmap(android.graphics.Bitmap)
+ * @see #getBitmap()
+ */
+ public Bitmap getBitmap(int width, int height) {
+ if (isAvailable() && width > 0 && height > 0) {
+ return getBitmap(Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888));
+ }
+ return null;
+ }
+
+ /**
+ * <p>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.</p>
+ *
+ * <p><strong>Do not</strong> invoke this method from a drawing method
+ * ({@link #onDraw(android.graphics.Canvas)} for instance).</p>
+ *
+ * @param bitmap The bitmap to copy the content of the surface texture into,
+ * cannot be null, all configurations are supported
+ *
+ * @return The bitmap specified as a parameter
+ *
+ * @see #isAvailable()
+ * @see #getBitmap(int, int)
+ * @see #getBitmap()
+ */
+ public Bitmap getBitmap(Bitmap bitmap) {
+ if (bitmap != null && isAvailable()) {
+ mAttachInfo.mHardwareRenderer.copyLayer(mLayer, bitmap);
+ }
+ return bitmap;
+ }
+
+ /**
+ * 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.
+ */
+ public boolean isAvailable() {
+ return mSurface != null;
+ }
+
+ /**
* Returns the {@link SurfaceTexture} used by this view. This method
- * may return null if the view is not attached to a window.
+ * may return null if the view is not attached to a window or if the surface
+ * texture has not been initialized yet.
+ *
+ * @see #isAvailable()
*/
public SurfaceTexture getSurfaceTexture() {
return mSurface;