3232import android .view .VelocityTracker ;
3333import android .view .View ;
3434import android .view .Window ;
35- import android .widget .Button ;
3635import android .widget .LinearLayout ;
3736import android .widget .ScrollView ;
3837
4140import androidx .appcompat .widget .AppCompatButton ;
4241import androidx .core .content .res .ResourcesCompat ;
4342
44- import com .google .android .material .button .MaterialButton ;
45-
4643
4744public class MainActivity extends AppCompatActivity {
48- int backgroundColor = 0xFF000000 | (200 * 256 + 250 )* 256 + 200 ;
49- static String [] sEasingNames = {
45+ int backgroundColor = 0xFF000000 | (200 * 256 + 250 ) * 256 + 200 ;
46+ static String [] sEasingNames = {
5047 "DECELERATE" ,
5148 "LINEAR" ,
5249 "OVERSHOOT" ,
@@ -76,6 +73,7 @@ public class MainActivity extends AppCompatActivity {
7673 MaterialEasing .EASE_OUT_ELASTIC ,
7774 MaterialEasing .EASE_OUT_BOUNCE
7875 };
76+
7977 @ Override
8078 protected void onCreate (Bundle savedInstanceState ) {
8179 super .onCreate (savedInstanceState );
@@ -91,11 +89,22 @@ protected void onCreate(Bundle savedInstanceState) {
9189 MaterialVelocity .Easing easing = sEasings [i ];
9290 b .setTextAlignment (View .TEXT_ALIGNMENT_CENTER );
9391
94- b .setPadding (1 ,1 , 5 , 5 );
92+ b .setPadding (1 , 1 , 5 , 5 );
9593 col .addView (b );
96- b .setOnClickListener (c ->{m .setEasing (easing );});
94+ b .setOnClickListener (c -> {
95+ m .setEasing (easing );
96+ });
9797
9898 }
99+
100+ AppCompatButton b = new AppCompatButton (this );
101+ b .setText ("graph mode" );
102+ b .setTextAlignment (View .TEXT_ALIGNMENT_CENTER );
103+ col .addView (b );
104+ b .setOnClickListener (c -> {
105+ m .mGraphMode = !m .mGraphMode ;
106+ });
107+
99108 col .setBackgroundColor (backgroundColor );
100109 scrollView .addView (col );
101110 row .addView (scrollView );
@@ -105,16 +114,19 @@ protected void onCreate(Bundle savedInstanceState) {
105114 }
106115
107116 static class BallMover extends View {
117+ public boolean mGraphMode = false ;
108118 Drawable ball ;
109119 VelocityTracker velocityTracker = VelocityTracker .obtain ();
110120 Velocity2D velocity2D = new Velocity2D ();
111121 int ballX ;
112122 int ballY ;
113123 int ballW = 128 ;
114124 int ballH = 128 ;
115- MaterialVelocity .Easing easing = null ;
125+ MaterialVelocity .Easing easing = null ;
116126 float [] points = new float [10000 ];
117127 Paint paint = new Paint ();
128+ Paint paintDot = new Paint ();
129+ private float mDuration ;
118130
119131
120132 public BallMover (Context context ) {
@@ -136,13 +148,16 @@ public BallMover(Context context, @Nullable AttributeSet attrs, int defStyleAttr
136148 void setup (Context context ) {
137149 ball = ResourcesCompat .getDrawable (context .getResources (), R .drawable .volleyball , null );
138150 paint .setStrokeWidth (3 );
151+ paintDot .setColor (Color .RED );
139152 }
153+
140154 public void setEasing (MaterialVelocity .Easing easing ) {
141155 this .easing = easing ;
142156 }
157+
143158 @ Override
144159 protected void onDraw (Canvas canvas ) {
145- canvas .drawRGB (200 ,250 ,200 );
160+ canvas .drawRGB (200 , 250 , 200 );
146161 if (startAnimationTime != 0 ) {
147162
148163 long timeMillis = SystemClock .uptimeMillis () - startAnimationTime ;
@@ -156,6 +171,14 @@ protected void onDraw(Canvas canvas) {
156171 startAnimationTime = 0 ;
157172 }
158173 canvas .drawLines (points , paint );
174+ int xPos = velocity2D .getPointOffsetX (points .length , time / mDuration );
175+ int yPos = velocity2D .getPointOffsetY (points .length , time / mDuration );
176+ float x = points [xPos ], y = points [xPos + 1 ];
177+ canvas .drawRoundRect (x - 10 , y - 10 , x + 10 , y + 10 , 20 , 20 , paintDot );
178+ x = points [yPos ];
179+ y = points [yPos + 1 ];
180+ canvas .drawRoundRect (x - 10 , y - 10 , x + 10 , y + 10 , 20 , 20 , paintDot );
181+
159182 }
160183
161184 ball .setBounds (getWidth () / 2 , getHeight () / 2 , ballW + getWidth () / 2 , ballH + getHeight () / 2 );
@@ -179,8 +202,6 @@ public boolean onTouchEvent(MotionEvent event) {
179202 switch (event .getAction ()) {
180203
181204 case MotionEvent .ACTION_DOWN :
182- System .out .println ("------- down ---------" );
183-
184205 startAnimationTime = 0 ;
185206 touchDownX = event .getX ();
186207 touchDownY = event .getY ();
@@ -189,8 +210,6 @@ public boolean onTouchEvent(MotionEvent event) {
189210 break ;
190211
191212 case MotionEvent .ACTION_MOVE :
192- System .out .println ("------- move ---------" );
193-
194213 touchDeltaX = event .getX () - touchDownX ;
195214 touchDeltaY = event .getY () - touchDownY ;
196215 ballX = (int ) (ballDownX + touchDeltaX );
@@ -199,19 +218,17 @@ public boolean onTouchEvent(MotionEvent event) {
199218
200219 break ;
201220 case MotionEvent .ACTION_UP :
202- System .out .println ("------- UP ---------" );
203-
204221 velocityTracker .computeCurrentVelocity (1000 );
205222 float velocityX = velocityTracker .getXVelocity ();
206223 float velocityY = velocityTracker .getYVelocity ();
207- System .out .println ("initial velocity " + velocityX + "," + velocityY );
208224 startAnimationTime = event .getEventTime ();
209225 velocity2D .configure (ballX , ballY ,
210226 velocityX , velocityY ,
211227 getWidth () / 2 , getHeight () / 2 ,
212228 4 , 1000 , 1000 ,
213229 easing );
214- velocity2D .getCurves (points , getWidth (), getHeight ());
230+ velocity2D .getCurves (points , getWidth (), getHeight (), mGraphMode );
231+ mDuration = velocity2D .getDuration ();
215232 invalidate ();
216233 break ;
217234
0 commit comments