Skip to content
This repository was archived by the owner on Dec 27, 2024. It is now read-only.

Commit b0b142b

Browse files
authored
add velocity display and path dots (#806)
1 parent 2cb8914 commit b0b142b

File tree

3 files changed

+171
-77
lines changed

3 files changed

+171
-77
lines changed

demoProjects/Drag2D/app/src/main/java/android/support/drag2d/MainActivity.java

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import android.view.VelocityTracker;
3333
import android.view.View;
3434
import android.view.Window;
35-
import android.widget.Button;
3635
import android.widget.LinearLayout;
3736
import android.widget.ScrollView;
3837

@@ -41,12 +40,10 @@
4140
import androidx.appcompat.widget.AppCompatButton;
4241
import androidx.core.content.res.ResourcesCompat;
4342

44-
import com.google.android.material.button.MaterialButton;
45-
4643

4744
public 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

demoProjects/Drag2D/app/src/main/java/android/support/drag2d/lib/MaterialVelocity.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ public float getEndPos() {
4141
public float getStartPos() {
4242
return mStartPos;
4343
}
44-
44+
public float getStartV() {
45+
return mStartVel;
46+
}
4547
protected static class Stage {
4648
float mStartV;
4749
float mStartPos;
@@ -101,7 +103,7 @@ public float getV(float t) {
101103
}
102104
return 0;
103105
}
104-
int lastStages = mNumberOfStages;
106+
int lastStages = mNumberOfStages-1;
105107
for (int i = 0; i < lastStages; i++) {
106108
if (mStage[i].mEndTime > t) {
107109
return mStage[i].getVel(t);
@@ -130,10 +132,7 @@ public float getPos(float t) {
130132
}
131133

132134
float ret = (float) getEasing(t - mStage[lastStages].mStartTime);
133-
System.out.println(">>>>>>>>>>>>>>> easing= " + ret);
134-
135135
ret += mStage[lastStages].mStartPos;
136-
System.out.println(">>>>>>>>>>>>>>> offset " + ret);
137136
return ret;
138137

139138
}
@@ -272,7 +271,7 @@ private double getEasingDiff(double t) {
272271
if (gx > 1) {
273272
return 0;
274273
}
275-
return mEasing.getDiff(gx) * mEasingAdapterDistance;
274+
return mEasing.getDiff(gx) * mEasingAdapterDistance*(t*mEasingAdapterA+mEasingAdapterB);
276275
}
277276

278277

0 commit comments

Comments
 (0)