-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add animated rectangle on drag latency page
- Loading branch information
Showing
1 changed file
with
46 additions
and
4 deletions.
There are no files selected for viewing
50 changes: 46 additions & 4 deletions
50
android/WALT/app/src/main/java/org/chromium/latency/walt/TouchCatcherView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,62 @@ | ||
package org.chromium.latency.walt; | ||
|
||
import android.content.Context; | ||
import android.graphics.Canvas; | ||
import android.graphics.Color; | ||
import android.graphics.Paint; | ||
import android.util.AttributeSet; | ||
import android.view.View; | ||
|
||
/** | ||
* Created by kamrik on 7/5/16. | ||
*/ | ||
|
||
|
||
public class TouchCatcherView extends View { | ||
|
||
private Paint linePaint = new Paint(); | ||
private ClockManager clockManager; | ||
|
||
public TouchCatcherView(Context context, AttributeSet attrs) { | ||
super(context, attrs); | ||
|
||
clockManager = ClockManager.getInstance(context); | ||
initialisePaint(); | ||
} | ||
|
||
private void initialisePaint() { | ||
float density = getResources().getDisplayMetrics().density; | ||
float lineWidth = 10f * density; | ||
linePaint.setColor(Color.RED); | ||
linePaint.setStrokeWidth(lineWidth); | ||
} | ||
|
||
public static double markerPosition(long t_us) { | ||
int period = 1000000; // microseconds | ||
// Normalized time within period, goes from 0 to 1 | ||
double t = (t_us % period) / (double) period; | ||
// Triangular wave with amplitude 1 | ||
double x_tri = -1 + 4 * Math.abs(t - 0.5); | ||
|
||
// Apply some smoothing to get rid of the sharp edges of triangular wave. | ||
// beta is a dimensionless smoothing parameter used below, experimentally derived. | ||
// Higher value gives less smoothing. | ||
double beta = 4; | ||
// Multiply by the smoothing function 1/{1 + exp(b(|x|-1))/(b-1)} | ||
// This is a Fermi function, modified to have a zero derivative at x = 1. | ||
double x_smooth = x_tri / (1 + Math.exp(beta*(Math.abs(x_tri)-1))/(beta - 1)); | ||
return x_smooth; | ||
} | ||
|
||
@Override | ||
protected void onDraw(Canvas canvas) { | ||
super.onDraw(canvas); | ||
|
||
int h = getHeight(); | ||
double normPos = markerPosition(clockManager.micros()); | ||
double amplitude = 0.4; // Of View height | ||
int pos = (int) (h * (0.5 + amplitude * normPos)); | ||
// Log.i("AnimatedView", "Pos is " + pos); | ||
int w = getWidth(); | ||
canvas.drawLine(w/3, pos, w*2/3, pos, linePaint); | ||
|
||
// Run every frame | ||
invalidate(); | ||
} | ||
} |