| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.prefabulated.touchlatency; |
| 18 |
|
| 19 |
import android.app.Activity; |
| 20 |
import android.content.Context; |
| 21 |
import android.graphics.Canvas; |
| 22 |
import android.graphics.Paint; |
| 23 |
import android.graphics.Paint.Align; |
| 24 |
import android.os.Bundle; |
| 25 |
import android.util.AttributeSet; |
| 26 |
import android.util.Log; |
| 27 |
import android.view.Display; |
| 28 |
import android.view.Display.Mode; |
| 29 |
import android.view.Menu; |
| 30 |
import android.view.MenuItem; |
| 31 |
import android.view.MotionEvent; |
| 32 |
import android.view.View; |
| 33 |
import android.os.Trace; |
| 34 |
import android.view.Window; |
| 35 |
import android.view.WindowManager; |
| 36 |
import java.math.RoundingMode; |
| 37 |
import java.text.DecimalFormat; |
| 38 |
|
| 39 |
class TouchLatencyView extends View implements View.OnTouchListener { |
| 40 |
private static final String LOG_TAG = "TouchLatency"; |
| 41 |
private static final int BACKGROUND_COLOR = 0xFF400080; |
| 42 |
private static final int INNER_RADIUS = 70; |
| 43 |
private static final int BALL_DIAMETER = 200; |
| 44 |
private static final int SEC_TO_NANOS = 1000000000; |
| 45 |
private static final float FPS_UPDATE_THRESHOLD = 20; |
| 46 |
private static final long BALL_VELOCITY = 420; |
| 47 |
|
| 48 |
public TouchLatencyView(Context context, AttributeSet attrs) { |
| 49 |
super(context, attrs); |
| 50 |
Trace.beginSection("TouchLatencyView constructor"); |
| 51 |
setOnTouchListener(this); |
| 52 |
setWillNotDraw(false); |
| 53 |
mBluePaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 54 |
mBluePaint.setColor(0xFF0000FF); |
| 55 |
mBluePaint.setStyle(Paint.Style.FILL); |
| 56 |
mGreenPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 57 |
mGreenPaint.setColor(0xFF00FF00); |
| 58 |
mGreenPaint.setStyle(Paint.Style.FILL); |
| 59 |
mYellowPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 60 |
mYellowPaint.setColor(0xFFFFFF00); |
| 61 |
mYellowPaint.setStyle(Paint.Style.FILL); |
| 62 |
mRedPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 63 |
mRedPaint.setColor(0xFFFF0000); |
| 64 |
mRedPaint.setStyle(Paint.Style.FILL); |
| 65 |
mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 66 |
mTextPaint.setColor(0xFFFFFFFF); |
| 67 |
mTextPaint.setTextSize(100); |
| 68 |
mTextPaint.setTextAlign(Align.RIGHT); |
| 69 |
|
| 70 |
mTouching = false; |
| 71 |
|
| 72 |
mLastDrawNano = 0; |
| 73 |
mFps = 0; |
| 74 |
mLastFpsUpdate = 0; |
| 75 |
mFrameCount = 0; |
| 76 |
|
| 77 |
mDf = new DecimalFormat("fps: #.##"); |
| 78 |
mDf.setRoundingMode(RoundingMode.HALF_UP); |
| 79 |
|
| 80 |
Trace.endSection(); |
| 81 |
} |
| 82 |
|
| 83 |
@Override |
| 84 |
public boolean onTouch(View view, MotionEvent event) { |
| 85 |
Trace.beginSection("TouchLatencyView onTouch"); |
| 86 |
int action = event.getActionMasked(); |
| 87 |
if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_MOVE) { |
| 88 |
mTouching = true; |
| 89 |
invalidate(); |
| 90 |
|
| 91 |
mTouchX = event.getX(); |
| 92 |
mTouchY = event.getY(); |
| 93 |
} else if (action == MotionEvent.ACTION_UP) { |
| 94 |
mTouching = false; |
| 95 |
invalidate(); |
| 96 |
} |
| 97 |
Trace.endSection(); |
| 98 |
return true; |
| 99 |
} |
| 100 |
|
| 101 |
private void drawTouch(Canvas canvas) { |
| 102 |
Trace.beginSection("TouchLatencyView drawTouch"); |
| 103 |
|
| 104 |
try { |
| 105 |
if (!mTouching) { |
| 106 |
Log.d(LOG_TAG, "Filling background"); |
| 107 |
canvas.drawColor(BACKGROUND_COLOR); |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
float deltaX = (mTouchX - mLastDrawnX); |
| 112 |
float deltaY = (mTouchY - mLastDrawnY); |
| 113 |
float scaleFactor = (float) Math.sqrt(deltaX * deltaX + deltaY * deltaY) * 1.5f; |
| 114 |
|
| 115 |
mLastDrawnX = mTouchX; |
| 116 |
mLastDrawnY = mTouchY; |
| 117 |
|
| 118 |
canvas.drawColor(BACKGROUND_COLOR); |
| 119 |
canvas.drawCircle(mTouchX, mTouchY, INNER_RADIUS + 3 * scaleFactor, mRedPaint); |
| 120 |
canvas.drawCircle(mTouchX, mTouchY, INNER_RADIUS + 2 * scaleFactor, mYellowPaint); |
| 121 |
canvas.drawCircle(mTouchX, mTouchY, INNER_RADIUS + scaleFactor, mGreenPaint); |
| 122 |
canvas.drawCircle(mTouchX, mTouchY, INNER_RADIUS, mBluePaint); |
| 123 |
} finally { |
| 124 |
Trace.endSection(); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
private Paint getBallColor() { |
| 129 |
if (mFps > 75) |
| 130 |
return mGreenPaint; |
| 131 |
else if (mFps > 45) |
| 132 |
return mYellowPaint; |
| 133 |
else |
| 134 |
return mRedPaint; |
| 135 |
} |
| 136 |
|
| 137 |
private void drawBall(Canvas canvas) { |
| 138 |
Trace.beginSection("TouchLatencyView drawBall"); |
| 139 |
int width = canvas.getWidth(); |
| 140 |
int height = canvas.getHeight(); |
| 141 |
float fps = 0f; |
| 142 |
|
| 143 |
long t = System.nanoTime(); |
| 144 |
long tDiff = t - mLastDrawNano; |
| 145 |
mLastDrawNano = t; |
| 146 |
mFrameCount++; |
| 147 |
|
| 148 |
if (tDiff < SEC_TO_NANOS) { |
| 149 |
fps = 1f * SEC_TO_NANOS / tDiff; |
| 150 |
} |
| 151 |
|
| 152 |
long fDiff = t - mLastFpsUpdate; |
| 153 |
if (Math.abs(mFps - fps) > FPS_UPDATE_THRESHOLD) { |
| 154 |
mFps = fps; |
| 155 |
mLastFpsUpdate = t; |
| 156 |
mFrameCount = 0; |
| 157 |
} else if (fDiff > SEC_TO_NANOS) { |
| 158 |
mFps = 1f * mFrameCount * SEC_TO_NANOS / fDiff; |
| 159 |
mLastFpsUpdate = t; |
| 160 |
mFrameCount = 0; |
| 161 |
} |
| 162 |
|
| 163 |
final long pos = t * BALL_VELOCITY / SEC_TO_NANOS; |
| 164 |
final long xMax = width - BALL_DIAMETER; |
| 165 |
final long yMax = height - BALL_DIAMETER; |
| 166 |
long xOffset = pos % xMax; |
| 167 |
long yOffset = pos % yMax; |
| 168 |
|
| 169 |
float left, right, top, bottom; |
| 170 |
|
| 171 |
if (((pos / xMax) & 1) == 0) { |
| 172 |
left = xMax - xOffset; |
| 173 |
} else { |
| 174 |
left = xOffset; |
| 175 |
} |
| 176 |
right = left + BALL_DIAMETER; |
| 177 |
|
| 178 |
if (((pos / yMax) & 1) == 0) { |
| 179 |
top = yMax - yOffset; |
| 180 |
} else { |
| 181 |
top = yOffset; |
| 182 |
} |
| 183 |
bottom = top + BALL_DIAMETER; |
| 184 |
|
| 185 |
|
| 186 |
canvas.drawColor(BACKGROUND_COLOR); |
| 187 |
canvas.drawOval(left, top, right, bottom, getBallColor()); |
| 188 |
canvas.drawText(mDf.format(mFps), width, 100, mTextPaint); |
| 189 |
|
| 190 |
invalidate(); |
| 191 |
Trace.endSection(); |
| 192 |
} |
| 193 |
|
| 194 |
@Override |
| 195 |
protected void onDraw(Canvas canvas) { |
| 196 |
super.onDraw(canvas); |
| 197 |
Trace.beginSection("TouchLatencyView onDraw"); |
| 198 |
if (mMode == 0) { |
| 199 |
drawTouch(canvas); |
| 200 |
} else { |
| 201 |
drawBall(canvas); |
| 202 |
} |
| 203 |
Trace.endSection(); |
| 204 |
} |
| 205 |
|
| 206 |
public void changeMode(MenuItem item) { |
| 207 |
Trace.beginSection("TouchLatencyView changeMode"); |
| 208 |
final int NUM_MODES = 2; |
| 209 |
final String modes[] = {"Touch", "Ball"}; |
| 210 |
mMode = (mMode + 1) % NUM_MODES; |
| 211 |
invalidate(); |
| 212 |
item.setTitle(modes[mMode]); |
| 213 |
Trace.endSection(); |
| 214 |
} |
| 215 |
|
| 216 |
private final Paint mBluePaint, mGreenPaint, mYellowPaint, mRedPaint, mTextPaint; |
| 217 |
private int mMode; |
| 218 |
|
| 219 |
private boolean mTouching; |
| 220 |
private float mTouchX, mTouchY; |
| 221 |
private float mLastDrawnX, mLastDrawnY; |
| 222 |
|
| 223 |
private long mLastDrawNano, mLastFpsUpdate, mFrameCount; |
| 224 |
private float mFps; |
| 225 |
private DecimalFormat mDf; |
| 226 |
} |
| 227 |
|
| 228 |
public class TouchLatencyActivity extends Activity { |
| 229 |
private Mode mDisplayModes[]; |
| 230 |
private int mCurrentModeIndex; |
| 231 |
|
| 232 |
@Override |
| 233 |
protected void onCreate(Bundle savedInstanceState) { |
| 234 |
super.onCreate(savedInstanceState); |
| 235 |
|
| 236 |
Trace.beginSection("TouchLatencyActivity onCreate"); |
| 237 |
setContentView(R.layout.activity_touch_latency); |
| 238 |
|
| 239 |
mTouchView = findViewById(R.id.canvasView); |
| 240 |
|
| 241 |
WindowManager wm = getWindowManager(); |
| 242 |
Display display = wm.getDefaultDisplay(); |
| 243 |
mDisplayModes = display.getSupportedModes(); |
| 244 |
Mode currentMode = getWindowManager().getDefaultDisplay().getMode(); |
| 245 |
|
| 246 |
for (int i = 0; i < mDisplayModes.length; i++) { |
| 247 |
if (currentMode.getModeId() == mDisplayModes[i].getModeId()) { |
| 248 |
mCurrentModeIndex = i; |
| 249 |
break; |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
Trace.endSection(); |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
@Override |
| 258 |
public boolean onCreateOptionsMenu(Menu menu) { |
| 259 |
Trace.beginSection("TouchLatencyActivity onCreateOptionsMenu"); |
| 260 |
|
| 261 |
getMenuInflater().inflate(R.menu.menu_touch_latency, menu); |
| 262 |
if (mDisplayModes.length > 1) { |
| 263 |
MenuItem menuItem = menu.findItem(R.id.display_mode); |
| 264 |
Mode currentMode = getWindowManager().getDefaultDisplay().getMode(); |
| 265 |
updateDisplayMode(menuItem, currentMode); |
| 266 |
} |
| 267 |
Trace.endSection(); |
| 268 |
return true; |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
private void updateDisplayMode(MenuItem menuItem, Mode displayMode) { |
| 273 |
int fps = (int) displayMode.getRefreshRate(); |
| 274 |
menuItem.setTitle(fps + "hz"); |
| 275 |
menuItem.setVisible(true); |
| 276 |
} |
| 277 |
|
| 278 |
public void changeDisplayMode(MenuItem item) { |
| 279 |
Window w = getWindow(); |
| 280 |
WindowManager.LayoutParams params = w.getAttributes(); |
| 281 |
|
| 282 |
int modeIndex = (mCurrentModeIndex + 1) % mDisplayModes.length; |
| 283 |
params.preferredDisplayModeId = mDisplayModes[modeIndex].getModeId(); |
| 284 |
w.setAttributes(params); |
| 285 |
|
| 286 |
updateDisplayMode(item, mDisplayModes[modeIndex]); |
| 287 |
mCurrentModeIndex = modeIndex; |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
@Override |
| 292 |
public boolean onOptionsItemSelected(MenuItem item) { |
| 293 |
Trace.beginSection("TouchLatencyActivity onOptionsItemSelected"); |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
int id = item.getItemId(); |
| 298 |
|
| 299 |
|
| 300 |
if (id == R.id.action_settings) { |
| 301 |
mTouchView.changeMode(item); |
| 302 |
} else if (id == R.id.display_mode) { |
| 303 |
changeDisplayMode(item); |
| 304 |
} |
| 305 |
|
| 306 |
Trace.endSection(); |
| 307 |
return super.onOptionsItemSelected(item); |
| 308 |
} |
| 309 |
|
| 310 |
private TouchLatencyView mTouchView; |
| 311 |
} |