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

Commit 20a3dc5

Browse files
authored
Drag2d cleanup (#815)
* clean up drag2d * fine tune 2d
1 parent 950b3fc commit 20a3dc5

File tree

6 files changed

+286
-97
lines changed

6 files changed

+286
-97
lines changed

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

+40-4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import android.support.drag2d.lib.MaterialVelocity;
2929
import android.support.drag2d.lib.Velocity2D;
3030
import android.util.AttributeSet;
31+
import android.util.Log;
32+
3133
import android.view.MotionEvent;
3234
import android.view.VelocityTracker;
3335
import android.view.View;
@@ -84,6 +86,9 @@ public class DragCardActivity extends AppCompatActivity {
8486
@Override
8587
protected void onCreate(Bundle savedInstanceState) {
8688
super.onCreate(savedInstanceState);
89+
int orientation = getResources().getConfiguration().orientation;
90+
Log.v("MAIN","orientation = "+orientation);
91+
8792
requestWindowFeature(Window.FEATURE_ACTION_BAR);
8893
LinearLayout topCol = new LinearLayout(this);
8994
LinearLayout row = new LinearLayout(this);
@@ -92,7 +97,8 @@ protected void onCreate(Bundle savedInstanceState) {
9297
col.setOrientation(LinearLayout.VERTICAL);
9398
ScrollView scrollView = new ScrollView(this);
9499
CardMover m = new CardMover(this);
95-
AppCompatButton[]buttons = new AppCompatButton[sEasingNames.length];
100+
AppCompatButton[] buttons = new AppCompatButton[sEasingNames.length];
101+
96102
for (int i = 0; i < sEasingNames.length; i++) {
97103
AppCompatButton b = new AppCompatButton(this);
98104
buttons[i] = b;
@@ -108,7 +114,8 @@ protected void onCreate(Bundle savedInstanceState) {
108114
for (int j = 0; j < buttons.length; j++) {
109115
Drawable draw = buttons[j].getBackground();
110116
draw = draw.mutate();
111-
draw.setTint((mode == j)?0xff328855:0xffAA88AA);
117+
draw.setTint((mode == j) ? 0xff328855 : 0xffAA88AA);
118+
112119
buttons[j].setBackgroundDrawable(draw);
113120
}
114121

@@ -179,6 +186,9 @@ static class CardMover extends View {
179186
Paint mCardPaint = new Paint();
180187
MaterialVelocity.Easing easing = null;
181188
float[] points = new float[10000];
189+
float[] mSegTime1 = new float[4];
190+
float[] mSegTime2 = new float[4];
191+
182192
Paint paint = new Paint();
183193
Paint paintDot = new Paint();
184194
private float mDuration;
@@ -230,6 +240,29 @@ protected void onDraw(Canvas canvas) {
230240
startAnimationTime = 0;
231241
}
232242
canvas.drawLines(points, paint);
243+
244+
245+
for (int i = 0; i < mSegTime1.length; i++) {
246+
float t = mSegTime1[i];
247+
if (Float.isNaN(t)) {
248+
break;
249+
}
250+
int mark = velocity2D.getPointOffsetX(points.length, t / mDuration);
251+
float x = points[mark], y = points[mark + 1];
252+
canvas.drawRoundRect(x - 10, y - 10, x + 10, y + 10, 20, 20, paint);
253+
}
254+
255+
for (int i = 0; i < mSegTime2.length; i++) {
256+
float t = mSegTime2[i];
257+
if (Float.isNaN(t)) {
258+
break;
259+
}
260+
261+
int mark = velocity2D.getPointOffsetY(points.length, t / mDuration);
262+
float x = points[mark], y = points[mark + 1];
263+
canvas.drawRoundRect(x - 10, y - 10, x + 10, y + 10, 20, 20, paint);
264+
}
265+
233266
int xPos = velocity2D.getPointOffsetX(points.length, time / mDuration);
234267
int yPos = velocity2D.getPointOffsetY(points.length, time / mDuration);
235268
float x = points[xPos], y = points[xPos + 1];
@@ -242,8 +275,10 @@ protected void onDraw(Canvas canvas) {
242275

243276
canvas.drawRoundRect(mCardX, mCardY, mCardX + getWidth() / 2, getHeight() + mCardY, rounding, rounding, mCardPaint);
244277
int scale = 128;
245-
int ballX = mCardX + getWidth() / 4 - scale/2;
246-
int ballY = mCardY + getHeight() / 2 - scale/2;
278+
279+
int ballX = mCardX + getWidth() / 4 - scale / 2;
280+
int ballY = mCardY + getHeight() / 2 - scale / 2;
281+
247282
ball.setBounds(ballX, ballY, ballX + scale, ballY + scale);
248283
ball.setTint(Color.CYAN);
249284
ball.draw(canvas);
@@ -294,6 +329,7 @@ public boolean onTouchEvent(MotionEvent event) {
294329
duration, maxV, maxA,
295330
easing);
296331
velocity2D.getCurves(points, getWidth(), getHeight(), mGraphMode);
332+
velocity2D.getCurvesSegments(mSegTime1, mSegTime2);
297333
mDuration = velocity2D.getDuration();
298334
invalidate();
299335
break;

0 commit comments

Comments
 (0)