【Android源码 栏目提醒】:本文主要为网学会员提供“[Android项目-Gallery3D][media-RenderView java]]源码 - 讲义教程”,希望对需要[Android项目-Gallery3D][media-RenderView java]]源码 - 讲义教程网友有所帮助,学习一下!
package com.cooliris.media import java.lang.ref.ReferenceQueue import java.lang.ref.WeakReference import java.util.ArrayList import javax.microedition.khronos.egl.EGLConfig import javax.microedition.khronos.opengles.GL10 import javax.microedition.khronos.opengles.GL11 import javax.microedition.khronos.opengles.GL11Ext import android.content.Context import android.graphics.Bitmap import android.graphics.Canvas import android.graphics.Rect import android.hardware.Sensor import android.hardware.SensorEvent import android.hardware.SensorEventListener import android.hardware.SensorManager import android.opengl.GLU import android.opengl.GLUtils import android.opengl.GLSurfaceView import android.os.Process import android.os.SystemClock import android.util.Log import android.util.SparseArray import android.view.KeyEvent import android.view.MotionEvent import android.view.SurfaceHolder public final class RenderView extends GLSurfaceView implements GLSurfaceView.Renderer SensorEventListener private static final String TAG quotRenderViewquot private static final int NUM_TEXTURE_LOAD_THREADS 4 private static final int MAX_LOADING_COUNT 8 private static final int EVENT_NONE 0 // private static final int EVENT_TOUCH 1 private static final int EVENT_KEY 2 private static final int EVENT_FOCUS 3 private SensorManager mSensorManager private GL11 mGL null private int mViewWidth 0 private int mViewHeight 0 private RootLayer mRootLayer null private boolean mListsDirty false private static final Lists sLists new Lists private Layer mTouchEventTarget null private int mCurrentEventType EVENT_NONE private KeyEvent mCurrentKeyEvent null private boolean mCurrentKeyEventResult false private volatile boolean mPendingSensorEvent false private int mLoadingCount 0 private static final DequeltTexturegt sLoadInputQueue new DequeltTexturegt private static final DequeltTexturegt sLoadInputQueueCached new DequeltTexturegt private static final DequeltTexturegt sLoadInputQueueVideo new DequeltTexturegt private static final DequeltTexturegt sLoadOutputQueue new DequeltTexturegt private static TextureLoadThread sCachedTextureLoadThread null private static TextureLoadThread sVideoTextureLoadThread null private static final TextureLoadThread sTextureLoadThreads new TextureLoadThreadNUM_TEXTURE_LOAD_THREADS private final DequeltMotionEventgt mTouchEventQueue new DequeltMotionEventgt private final DirectLinkedListltTextureReferencegt mActiveTextureList new DirectLinkedListltTextureReferencegt SuppressWarningsquotuncheckedquot private final ReferenceQueue mUnreferencedTextureQueue new ReferenceQueue // Frame time in milliseconds and delta since last frame in seconds. Uses // SystemClock.getUptimeMillis. private long mFrameTime 0 private float mFrameInterval 0.0f private float mAlpha private long mLoadingExpensiveTexturesStartTime 0 private final SparseArrayltResourceTexturegt sCacheScaled new SparseArrayltResourceTexturegt private final SparseArrayltResourceTexturegt sCacheUnscaled new SparseArrayltResourceTexturegt private boolean mFirstDraw // The cached texture that is bound to Texture Unit 0. // We need to reset this to null whenever the active texture unit changes. private Texture mBoundTexture // Weak reference to a texture that stores the associated texture ID. private static final class TextureReference extends WeakReferenceltTexturegt SuppressWarningsquotuncheckedquot public TextureReferenceTexture texture GL11 gl ReferenceQueue referenceQueue int textureId supertexture referenceQueue this.textureId textureId this.gl gl public final int textureId public final GL11 gl public final DirectLinkedList.EntryltTextureReferencegt activeListEntry new DirectLinkedList.EntryltTextureReferencegtthis public static final class Lists public final ArrayListltLayergt updateList new ArrayListltLayergt public final ArrayListltLayergt opaqueList new ArrayListltLayergt public final ArrayListltLayergt blendedList new ArrayListltLayergt public final ArrayListltLayergt hitTestList new ArrayListltLayergt public final ArrayListltLayergt systemList new ArrayListltLayergt void clear updateList.clear opaqueList.clear blendedList.clear hitTestList.clear systemList.clear public RenderViewfinal Context context supercontext setBackgroundDrawablenull setFocusabletrue setEGLConfigChoosertrue setRendererthis mSensorManager SensorManager context.getSystemServiceContext.SENSOR_SERVICE if sCachedTextureLoadThread null for int i 0 i NUM_TEXTURE_LOAD_THREADS i TextureLoadThread thread new TextureLoadThread if i 0 sCachedTextureLoadThread thread if i 1 sVideoTextureLoadThread thread sTextureLoadThreadsi thread thread.start public void setRootLayerRootLayer layer if mRootLayer layer mRootLayer layer mListsDirty true if layer null mRootLayer.setSizemViewWidth mViewHeight public ResourceTexture getResourceint resourceId return getResourceInternalresourceId true public ResourceTexture getResourceint resourceId boolean scaled return getResourceInternalresourceId scaled private ResourceTexture getResourceInternalint resourceId boolean scaled final SparseArrayltResourceTexturegt cache scaled sCacheScaled : sCacheUnscaled ResourceTexture texture cache.getresourceId if texture null ampamp resourceId 0 texture new ResourceTextureresourceId scaled cache.putresourceId texture return texture public void clearCache clearTextureArraysCacheScaled clearTextureArraysCacheUnscaled private void clearTextureArraySparseArrayltResourceTexturegt array / final int size array.size for int i 0 i lt size i ResourceTexture texture array.getarray.keyAti if texture null texture.clear / array.clear / Render API / public long getFrameTime return mFrameTime public float getFrameInterval return mFrameInterval public void primeTexture texture boolean highPriority if texture null ampamp texture.mState Texture.STATE_UNLOADED ampamp highPriority mLoadingCount lt MAX_LOADING_COUNT queueLoadtexture highPriority public void loadTextureTexture texture if texture null switch texture.mState case Texture.STATE_UNLOADED: case Texture.STATE_QUEUED: int textureId new int1 texture.mState Texture.STATE_LOADING loadTextureAsynctexture uploadTexturetexture textureId break private void loadTextureAsyncTexture texture try Bitmap bitmap texture.loadthis if bitmap null bitmap Utils.resizeBitmapbitmap 1024 int width bitmap.getWidth int height bitmap.getHeight texture.mWidth width texture.mHeight height // Create a padded bitmap if the natural size is not a power of // 2. if Shared.isPowerOf2width Shared.isPowerOf2height int paddedWidth Shared.nextPowerOf2width int paddedHeight Shared.nextPowerOf2height Bitmap.Config config bitmap.getConfig if config null config Bitmap.Config.RGB_565 if width height gt 512 512 config Bitmap.Config.RGB_565 Bitmap padded Bitmap.createBitmappaddedWidth paddedHeight config Canvas canvas new Canvaspadded canvas.drawBitmapbitmap 0 0 null bitmap.recycle bitmap padded // Store normalized width and height for use in texture // coordinates. texture.mNormalizedWidth float width / float paddedWidth texture.mNormalizedHeight float height / float paddedHeight else texture.mNormalizedWidth 1.0f texture.mNormalizedHeight 1.0f texture.mBitmap bitmap catch Exception e texture.mBitmap null catch OutOfMemoryError eMem Log.iTAG quotBitmap power of 2 creation fail outofmemoryquot handleLowMemory public boolean bindTexture texture if texture null if texture mBoundTexture return true switch texture.mState case Texture.STATE_UNLOADED: if texture.getClass.equalsResourceTexture.class loadTexturetexture return false if mLoadingCount lt MAX_LOADING_COUNT queueLoadtexture false break case Texture.STATE_LOADED: mGL.glBindTextureGL11.GL_TEXTURE_2D texture.mId mBoundTexture texture return true default: break return false public void setAlphafloat alpha GL11 gl mGL gl.glTexEnvfGL11.GL_TEXTURE_ENV GL11.GL_TEXTURE_ENV_MODE GL11.GL_MODULATE gl.glColor4falpha alpha alpha alpha mAlpha alpha public float getAlpha return mAlpha public void setColorfloat red float green float blue float alpha GL11 gl mGL gl.glTexEnvfGL11.GL_TEXTURE_ENV GL11.GL_TEXTURE_ENV_MODE GL11.GL_MODULATE gl.glColor4fred green blue alpha mAlpha alpha public void resetColor GL11 gl mGL gl.glTexEnvfGL11.GL_TEXTURE_ENV GL11.GL_TEXTURE_ENV_MODE GL11.GL_REPLACE gl.glColor4f1 1 1 1 public boolean isLoadingExpensiveTextures return mLoadingExpensiveTexturesStartTime 0 public long elapsedLoadingExpensiveTextures long startTime mLoadingExpensiveTexturesStartTime if startTime 0 return SystemClock.uptimeMillis - startTime else return -1 private void queueLoadfinal Texture texture boolean highPriority // Allow the texture to defer queuing. if texture.shouldQueue return // Change the texture state to loading. texture.mState Texture.STATE_LOADING // Push the texture onto the load input queue. DequeltTexturegt inputQueue texture.isUncachedVideo sLoadInputQueueVideo : texture.isCached sLoadInputQueueCached : sLoadInputQueue synchronized inputQueue if highPriority inputQueue.addFirsttexture else inputQueue.addLasttexture inputQueue.notify mLoadingCount public void draw2DTexture texture float x float y if bindtexture final float width texture.getWidth final float height texture.getHeight GL11Ext mGL.glDrawTexfOESx mViewHeight - y - height 0f width height public void draw2DTexture texture float x float y float width float height if bindtexture GL11Ext mGL.glDrawTexfOESx mViewHeight - y - height 0f width height public void draw2DTexture texture int x int y int width int height if bindtexture GL11Ext mGL.glDrawTexiOESx int mViewHeight - y - height 0 width height public void draw2Dfloat x float y float z float width float height GL11Ext mGL.glDrawTexfOESx mViewHeight - y - height z width height public boolean bindMixedTexture from Texture to float ratio // Bind quotfromquot and quottoquot to TEXTURE0 and TEXTURE1 respectively. final GL11 gl mGL boolean bind true bind amp bindfrom gl.glActiveTextureGL11.GL_TEXTURE1 mBoundTexture null bind amp bindto if bind return false // Enable TEXTURE1. gl.glEnableGL11.GL_TEXTURE_2D // Interpolate the RGB and alpha values between both textures. gl.glTexEnvfGL11.GL_TEXTURE_ENV GL11.GL_TEXTURE_ENV_MODE GL11.GL_COMBINE gl.glTexEnvfGL11.GL_TEXTURE_ENV GL11.GL_COMBINE_RGB GL11.GL_INTERPOLATE gl.glTexEnvfGL11.GL_TEXTURE_ENV GL11.GL_COMBINE_ALPHA GL11.GL_INTERPOLATE // Specify the interpolation factor via the alpha component of // GL_TEXTURE_ENV_COLOR. final float color 1f 1f 1f ratio gl.glTexEnvfvGL11.GL_TEXTURE_ENV GL11.GL_TEXTURE_ENV_COLOR color 0 // Wire up the interpolation factor for RGB. gl.glTexEnvfGL11.GL_TEXTURE_ENV GL11.GL_SRC2_RGB GL11.GL_CONSTANT gl.glTexEnvfGL11.GL_TEXTURE_ENV GL11.GL_OPERAND2_RGB GL11.GL_SRC_ALPHA // Wire up the interpolation factor for alpha. gl.glTexEnvfGL11.GL_TEXTURE_ENV GL11.GL_SRC2_ALPHA GL11.GL_CONSTANT gl.glTexEnvfGL11.GL_TEXTURE_ENV GL11.GL_OPERAND2_ALPHA GL11.GL_SRC_ALPHA return true public void unbindMixed // Disable TEXTURE1. final GL11 gl mGL gl.glDisableGL11.GL_TEXTURE_2D // Switch back to the default texture unit. gl.glActiveTextureGL11.GL_TEXTURE0 mBoundTexture null public void drawMixed2DTexture from Texture to float ratio float x float y float z float width float height final GL11 gl mGL // Bind quotfromquot and quottoquot to TEXTURE0 and TEXTURE1 respectively. if bindfrom gl.glActiveTextureGL11.GL_TEXTURE1 mBoundTexture null if bindto // Enable TEXTURE1. gl.glEnableGL11.GL_TEXTURE_2D // Interpolate the RGB and alpha values between both textures. gl.glTexEnvfGL11.GL_TEXTURE_ENV GL11.GL_TEXTURE_ENV_MODE GL11.GL_COMBINE gl.glTexEnvfGL11.GL_TEXTURE_ENV GL11.GL_COMBINE_RGB GL11.GL_INTERPOLATE gl.glTexEnvfGL11.GL_TEXTURE_ENV GL11.GL_COMBINE_ALPHA GL11.GL_INTERPOLATE // Specify the interpolation factor via the alpha component of // GL_TEXTURE_ENV_COLOR. final float color 1f 1f 1f ratio gl.glTexEnvfvGL11.GL_TEXTURE_ENV GL11.GL_TEXTURE_ENV_COLOR color 0 // Wire up the interpolation factor for RGB. gl.glTexEnvfGL11.GL_TEXTURE_ENV GL11.GL_SRC2_RG.