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

Commit a6c5649

Browse files
authored
Advanced touch support (#803)
* Drag2D demo * Drag2D demo * fix velocity mode * working 2D system
1 parent 70216b2 commit a6c5649

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1908
-0
lines changed

demoProjects/Drag2D/.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/caches
5+
/.idea/libraries
6+
/.idea/modules.xml
7+
/.idea/workspace.xml
8+
/.idea/navEditor.xml
9+
/.idea/assetWizardSettings.xml
10+
.DS_Store
11+
/build
12+
/captures
13+
.externalNativeBuild
14+
.cxx
15+
local.properties

demoProjects/Drag2D/.idea/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demoProjects/Drag2D/app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
plugins {
2+
id 'com.android.application'
3+
}
4+
5+
android {
6+
namespace 'android.support.drag2d'
7+
compileSdk 33
8+
9+
defaultConfig {
10+
applicationId "android.support.drag2d"
11+
minSdk 25
12+
targetSdk 33
13+
versionCode 1
14+
versionName "1.0"
15+
16+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
17+
}
18+
19+
buildTypes {
20+
release {
21+
minifyEnabled false
22+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
23+
}
24+
}
25+
compileOptions {
26+
sourceCompatibility JavaVersion.VERSION_1_8
27+
targetCompatibility JavaVersion.VERSION_1_8
28+
}
29+
}
30+
31+
dependencies {
32+
33+
implementation 'androidx.appcompat:appcompat:1.6.1'
34+
implementation 'com.google.android.material:material:1.8.0'
35+
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
36+
testImplementation 'junit:junit:4.13.2'
37+
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
38+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
39+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (C) 2023 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package android.support.drag2d;
17+
18+
import android.content.Context;
19+
20+
import androidx.test.platform.app.InstrumentationRegistry;
21+
import androidx.test.ext.junit.runners.AndroidJUnit4;
22+
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
26+
import static org.junit.Assert.*;
27+
28+
/**
29+
* Instrumented test, which will execute on an Android device.
30+
*
31+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
32+
*/
33+
@RunWith(AndroidJUnit4.class)
34+
public class ExampleInstrumentedTest {
35+
@Test
36+
public void useAppContext() {
37+
// Context of the app under test.
38+
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
39+
assertEquals("android.support.drag2d", appContext.getPackageName());
40+
}
41+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:dataExtractionRules="@xml/data_extraction_rules"
8+
android:fullBackupContent="@xml/backup_rules"
9+
android:icon="@mipmap/ic_launcher"
10+
android:label="@string/app_name"
11+
android:supportsRtl="true"
12+
android:theme="@style/Theme.Drag2D"
13+
tools:targetApi="31">
14+
<activity
15+
android:name=".MainActivity"
16+
android:exported="true">
17+
<intent-filter>
18+
<action android:name="android.intent.action.MAIN" />
19+
20+
<category android:name="android.intent.category.LAUNCHER" />
21+
</intent-filter>
22+
</activity>
23+
</application>
24+
25+
</manifest>
Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
/*
2+
* Copyright (C) 2023 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package android.support.drag2d;
18+
19+
20+
import android.content.Context;
21+
import android.graphics.Canvas;
22+
import android.graphics.Color;
23+
import android.graphics.Paint;
24+
import android.graphics.drawable.Drawable;
25+
import android.os.Bundle;
26+
import android.os.SystemClock;
27+
import android.support.drag2d.lib.MaterialEasing;
28+
import android.support.drag2d.lib.MaterialVelocity;
29+
import android.support.drag2d.lib.Velocity2D;
30+
import android.util.AttributeSet;
31+
import android.view.MotionEvent;
32+
import android.view.VelocityTracker;
33+
import android.view.View;
34+
import android.view.Window;
35+
import android.widget.Button;
36+
import android.widget.LinearLayout;
37+
import android.widget.ScrollView;
38+
39+
import androidx.annotation.Nullable;
40+
import androidx.appcompat.app.AppCompatActivity;
41+
import androidx.appcompat.widget.AppCompatButton;
42+
import androidx.core.content.res.ResourcesCompat;
43+
44+
import com.google.android.material.button.MaterialButton;
45+
46+
47+
public class MainActivity extends AppCompatActivity {
48+
int backgroundColor = 0xFF000000|(200*256+250)*256+200;
49+
static String []sEasingNames = {
50+
"DECELERATE",
51+
"LINEAR",
52+
"OVERSHOOT",
53+
"EASE_OUT_SINE",
54+
"EASE_OUT_CUBIC",
55+
"EASE_OUT_QUINT",
56+
"EASE_OUT_CIRC",
57+
"EASE_OUT_QUAD",
58+
"EASE_OUT_QUART",
59+
"EASE_OUT_EXPO",
60+
"EASE_OUT_BACK",
61+
"EASE_OUT_ELASTIC",
62+
"EASE_OUT_BOUNCE"
63+
};
64+
static MaterialVelocity.Easing[] sEasings = {
65+
MaterialEasing.DECELERATE,
66+
MaterialEasing.LINEAR,
67+
MaterialEasing.OVERSHOOT,
68+
MaterialEasing.EASE_OUT_SINE,
69+
MaterialEasing.EASE_OUT_CUBIC,
70+
MaterialEasing.EASE_OUT_QUINT,
71+
MaterialEasing.EASE_OUT_CIRC,
72+
MaterialEasing.EASE_OUT_QUAD,
73+
MaterialEasing.EASE_OUT_QUART,
74+
MaterialEasing.EASE_OUT_EXPO,
75+
MaterialEasing.EASE_OUT_BACK,
76+
MaterialEasing.EASE_OUT_ELASTIC,
77+
MaterialEasing.EASE_OUT_BOUNCE
78+
};
79+
@Override
80+
protected void onCreate(Bundle savedInstanceState) {
81+
super.onCreate(savedInstanceState);
82+
requestWindowFeature(Window.FEATURE_ACTION_BAR);
83+
LinearLayout row = new LinearLayout(this);
84+
LinearLayout col = new LinearLayout(this);
85+
col.setOrientation(LinearLayout.VERTICAL);
86+
ScrollView scrollView = new ScrollView(this);
87+
BallMover m = new BallMover(this);
88+
for (int i = 0; i < sEasingNames.length; i++) {
89+
AppCompatButton b = new AppCompatButton(this);
90+
b.setText(sEasingNames[i]);
91+
MaterialVelocity.Easing easing = sEasings[i];
92+
b.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
93+
94+
b.setPadding(1,1,5,5);
95+
col.addView(b);
96+
b.setOnClickListener(c->{m.setEasing(easing);});
97+
98+
}
99+
col.setBackgroundColor(backgroundColor);
100+
scrollView.addView(col);
101+
row.addView(scrollView);
102+
row.addView(m);
103+
setContentView(row);
104+
105+
}
106+
107+
static class BallMover extends View {
108+
Drawable ball;
109+
VelocityTracker velocityTracker = VelocityTracker.obtain();
110+
Velocity2D velocity2D = new Velocity2D();
111+
int ballX;
112+
int ballY;
113+
int ballW = 128;
114+
int ballH = 128;
115+
MaterialVelocity.Easing easing = null;
116+
float[] points = new float[10000];
117+
Paint paint = new Paint();
118+
119+
120+
public BallMover(Context context) {
121+
super(context);
122+
setup(context);
123+
}
124+
125+
public BallMover(Context context, @Nullable AttributeSet attrs) {
126+
super(context, attrs);
127+
setup(context);
128+
}
129+
130+
public BallMover(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
131+
super(context, attrs, defStyleAttr);
132+
setup(context);
133+
}
134+
135+
136+
void setup(Context context) {
137+
ball = ResourcesCompat.getDrawable(context.getResources(), R.drawable.volleyball, null);
138+
paint.setStrokeWidth(3);
139+
}
140+
public void setEasing(MaterialVelocity.Easing easing) {
141+
this.easing = easing;
142+
}
143+
@Override
144+
protected void onDraw(Canvas canvas) {
145+
canvas.drawRGB(200,250,200);
146+
if (startAnimationTime != 0) {
147+
148+
long timeMillis = SystemClock.uptimeMillis() - startAnimationTime;
149+
float time = timeMillis / 1000f;
150+
ballX = (int) velocity2D.getX(time);
151+
ballY = (int) velocity2D.getY(time);
152+
153+
if (velocity2D.isStillMoving(time)) {
154+
invalidate();
155+
} else {
156+
startAnimationTime = 0;
157+
}
158+
canvas.drawLines(points, paint);
159+
}
160+
161+
ball.setBounds(getWidth() / 2, getHeight() / 2, ballW + getWidth() / 2, ballH + getHeight() / 2);
162+
ball.setTint(Color.CYAN);
163+
ball.draw(canvas);
164+
ball.setBounds(ballX, ballY, ballW + ballX, ballH + ballY);
165+
ball.setTint(Color.BLACK);
166+
ball.draw(canvas);
167+
}
168+
169+
float touchDownX;
170+
float touchDownY;
171+
float touchDeltaX, touchDeltaY;
172+
int ballDownX, ballDownY;
173+
long startAnimationTime;
174+
175+
176+
@Override
177+
public boolean onTouchEvent(MotionEvent event) {
178+
velocityTracker.addMovement(event);
179+
switch (event.getAction()) {
180+
181+
case MotionEvent.ACTION_DOWN:
182+
System.out.println("------- down ---------");
183+
184+
startAnimationTime = 0;
185+
touchDownX = event.getX();
186+
touchDownY = event.getY();
187+
ballDownX = ballX;
188+
ballDownY = ballY;
189+
break;
190+
191+
case MotionEvent.ACTION_MOVE:
192+
System.out.println("------- move ---------");
193+
194+
touchDeltaX = event.getX() - touchDownX;
195+
touchDeltaY = event.getY() - touchDownY;
196+
ballX = (int) (ballDownX + touchDeltaX);
197+
ballY = (int) (ballDownY + touchDeltaY);
198+
invalidate();
199+
200+
break;
201+
case MotionEvent.ACTION_UP:
202+
System.out.println("------- UP ---------");
203+
204+
velocityTracker.computeCurrentVelocity(1000);
205+
float velocityX = velocityTracker.getXVelocity();
206+
float velocityY = velocityTracker.getYVelocity();
207+
System.out.println("initial velocity " + velocityX + "," + velocityY);
208+
startAnimationTime = event.getEventTime();
209+
velocity2D.configure(ballX, ballY,
210+
velocityX, velocityY,
211+
getWidth() / 2, getHeight() / 2,
212+
4, 1000, 1000,
213+
easing);
214+
velocity2D.getCurves(points, getWidth(), getHeight());
215+
invalidate();
216+
break;
217+
218+
}
219+
return true;
220+
}
221+
}
222+
}

0 commit comments

Comments
 (0)