Loading core/java/android/view/SurfaceView.java +27 −16 Original line number Original line Diff line number Diff line Loading @@ -142,6 +142,13 @@ public class SurfaceView extends View { final Rect mSurfaceFrame = new Rect(); final Rect mSurfaceFrame = new Rect(); private Translator mTranslator; private Translator mTranslator; // A flag to indicate that the Canvas has to be scaled private boolean mScaleCanvas = false; // A flag to indicate that the Canvas is in use and being scaled. // This may remain to be false even if mScaleCanvas is true if the applicatio // does not use the canvas (such as GLSurfaceView, VideoView). private boolean mCanvasScaled = false; public SurfaceView(Context context) { public SurfaceView(Context context) { super(context); super(context); setWillNotDraw(true); setWillNotDraw(true); Loading Loading @@ -255,18 +262,21 @@ public class SurfaceView extends View { @Override @Override public boolean dispatchTouchEvent(MotionEvent event) { public boolean dispatchTouchEvent(MotionEvent event) { // SurfaceView uses pre-scaled size unless fixed size is requested. This hook if (mTranslator == null || mCanvasScaled) { // scales the event back to the pre-scaled coordinates for such surface. // Use the event as is if no scaling is required, or the surface's canvas if (mScaled) { // is scaled too. return super.dispatchTouchEvent(event); } else { // The surface is in native size, so we need to scale the event // back to native location. MotionEvent scaledBack = MotionEvent.obtain(event); MotionEvent scaledBack = MotionEvent.obtain(event); mTranslator.translateEventInScreenToAppWindow(event); // scale back to original scaledBack.scale(mTranslator.applicationScale); try { try { return super.dispatchTouchEvent(scaledBack); return super.dispatchTouchEvent(scaledBack); } finally { } finally { scaledBack.recycle(); scaledBack.recycle(); } } } else { return super.dispatchTouchEvent(event); } } } } Loading @@ -292,8 +302,6 @@ public class SurfaceView extends View { mWindowType = type; mWindowType = type; } } boolean mScaled = false; private void updateWindow(boolean force) { private void updateWindow(boolean force) { if (!mHaveFrame) { if (!mHaveFrame) { return; return; Loading @@ -313,14 +321,15 @@ public class SurfaceView extends View { int myHeight = mRequestedHeight; int myHeight = mRequestedHeight; if (myHeight <= 0) myHeight = getHeight(); if (myHeight <= 0) myHeight = getHeight(); // Use original size if the app specified the size of the view, // Use requested size if the app specified the size of the view // and let the flinger to scale up. // and let the flinger to scale up. Otherwise, use the native size // (* appScale) and assume the application can handle it. if (mRequestedWidth <= 0 && mTranslator != null) { if (mRequestedWidth <= 0 && mTranslator != null) { myWidth = (int) (myWidth * appScale + 0.5f); myWidth = (int) (myWidth * appScale + 0.5f); myHeight = (int) (myHeight * appScale + 0.5f); myHeight = (int) (myHeight * appScale + 0.5f); mScaled = true; mScaleCanvas = true; } else { } else { mScaled = false; mScaleCanvas = false; } } getLocationInWindow(mLocation); getLocationInWindow(mLocation); Loading Loading @@ -642,7 +651,9 @@ public class SurfaceView extends View { if (localLOGV) Log.i(TAG, "Returned canvas: " + c); if (localLOGV) Log.i(TAG, "Returned canvas: " + c); if (c != null) { if (c != null) { mLastLockTime = SystemClock.uptimeMillis(); mLastLockTime = SystemClock.uptimeMillis(); if (mScaled) { if (mScaleCanvas) { // When the canvas is scaled, don't scale back the event's location. mCanvasScaled = true; mSaveCount = c.save(); mSaveCount = c.save(); mTranslator.translateCanvas(c); mTranslator.translateCanvas(c); } } Loading @@ -668,7 +679,7 @@ public class SurfaceView extends View { } } public void unlockCanvasAndPost(Canvas canvas) { public void unlockCanvasAndPost(Canvas canvas) { if (mScaled) { if (mCanvasScaled) { canvas.restoreToCount(mSaveCount); canvas.restoreToCount(mSaveCount); } } mSurface.unlockCanvasAndPost(canvas); mSurface.unlockCanvasAndPost(canvas); Loading Loading
core/java/android/view/SurfaceView.java +27 −16 Original line number Original line Diff line number Diff line Loading @@ -142,6 +142,13 @@ public class SurfaceView extends View { final Rect mSurfaceFrame = new Rect(); final Rect mSurfaceFrame = new Rect(); private Translator mTranslator; private Translator mTranslator; // A flag to indicate that the Canvas has to be scaled private boolean mScaleCanvas = false; // A flag to indicate that the Canvas is in use and being scaled. // This may remain to be false even if mScaleCanvas is true if the applicatio // does not use the canvas (such as GLSurfaceView, VideoView). private boolean mCanvasScaled = false; public SurfaceView(Context context) { public SurfaceView(Context context) { super(context); super(context); setWillNotDraw(true); setWillNotDraw(true); Loading Loading @@ -255,18 +262,21 @@ public class SurfaceView extends View { @Override @Override public boolean dispatchTouchEvent(MotionEvent event) { public boolean dispatchTouchEvent(MotionEvent event) { // SurfaceView uses pre-scaled size unless fixed size is requested. This hook if (mTranslator == null || mCanvasScaled) { // scales the event back to the pre-scaled coordinates for such surface. // Use the event as is if no scaling is required, or the surface's canvas if (mScaled) { // is scaled too. return super.dispatchTouchEvent(event); } else { // The surface is in native size, so we need to scale the event // back to native location. MotionEvent scaledBack = MotionEvent.obtain(event); MotionEvent scaledBack = MotionEvent.obtain(event); mTranslator.translateEventInScreenToAppWindow(event); // scale back to original scaledBack.scale(mTranslator.applicationScale); try { try { return super.dispatchTouchEvent(scaledBack); return super.dispatchTouchEvent(scaledBack); } finally { } finally { scaledBack.recycle(); scaledBack.recycle(); } } } else { return super.dispatchTouchEvent(event); } } } } Loading @@ -292,8 +302,6 @@ public class SurfaceView extends View { mWindowType = type; mWindowType = type; } } boolean mScaled = false; private void updateWindow(boolean force) { private void updateWindow(boolean force) { if (!mHaveFrame) { if (!mHaveFrame) { return; return; Loading @@ -313,14 +321,15 @@ public class SurfaceView extends View { int myHeight = mRequestedHeight; int myHeight = mRequestedHeight; if (myHeight <= 0) myHeight = getHeight(); if (myHeight <= 0) myHeight = getHeight(); // Use original size if the app specified the size of the view, // Use requested size if the app specified the size of the view // and let the flinger to scale up. // and let the flinger to scale up. Otherwise, use the native size // (* appScale) and assume the application can handle it. if (mRequestedWidth <= 0 && mTranslator != null) { if (mRequestedWidth <= 0 && mTranslator != null) { myWidth = (int) (myWidth * appScale + 0.5f); myWidth = (int) (myWidth * appScale + 0.5f); myHeight = (int) (myHeight * appScale + 0.5f); myHeight = (int) (myHeight * appScale + 0.5f); mScaled = true; mScaleCanvas = true; } else { } else { mScaled = false; mScaleCanvas = false; } } getLocationInWindow(mLocation); getLocationInWindow(mLocation); Loading Loading @@ -642,7 +651,9 @@ public class SurfaceView extends View { if (localLOGV) Log.i(TAG, "Returned canvas: " + c); if (localLOGV) Log.i(TAG, "Returned canvas: " + c); if (c != null) { if (c != null) { mLastLockTime = SystemClock.uptimeMillis(); mLastLockTime = SystemClock.uptimeMillis(); if (mScaled) { if (mScaleCanvas) { // When the canvas is scaled, don't scale back the event's location. mCanvasScaled = true; mSaveCount = c.save(); mSaveCount = c.save(); mTranslator.translateCanvas(c); mTranslator.translateCanvas(c); } } Loading @@ -668,7 +679,7 @@ public class SurfaceView extends View { } } public void unlockCanvasAndPost(Canvas canvas) { public void unlockCanvasAndPost(Canvas canvas) { if (mScaled) { if (mCanvasScaled) { canvas.restoreToCount(mSaveCount); canvas.restoreToCount(mSaveCount); } } mSurface.unlockCanvasAndPost(canvas); mSurface.unlockCanvasAndPost(canvas); Loading