├── .gitignore
├── README.md
├── TouchExamples
├── AndroidManifest.xml
├── assets
│ └── android.jpg
├── build.gradle
├── project.properties
├── res
│ ├── drawable-hdpi
│ │ └── ic_launcher.png
│ ├── drawable-ldpi
│ │ └── ic_launcher.png
│ ├── drawable-mdpi
│ │ └── ic_launcher.png
│ ├── layout
│ │ ├── gesture_scroll.xml
│ │ ├── intercept_row.xml
│ │ ├── move_logger.xml
│ │ ├── touch_intercept.xml
│ │ └── touch_listener.xml
│ └── values
│ │ ├── dimens.xml
│ │ └── styles.xml
└── src
│ └── com
│ └── examples
│ └── customtouch
│ ├── MainActivity.java
│ ├── MoveLoggerActivity.java
│ ├── MultitouchActivity.java
│ ├── TouchDelegateActivity.java
│ ├── TouchForwardActivity.java
│ ├── TouchInterceptActivity.java
│ ├── TouchListenerActivity.java
│ ├── TwoDimensionGestureScrollActivity.java
│ ├── TwoDimensionScrollActivity.java
│ └── widget
│ ├── RotateZoomImageView.java
│ ├── TouchDelegateLayout.java
│ ├── TouchForwardLayout.java
│ ├── TwoDimensionGestureScrollView.java
│ └── TwoDimensionScrollView.java
├── build.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── license.txt
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | #OSX Noise
2 | .DS_Store
3 |
4 | #Gradle Noise
5 | .gradle/
6 |
7 | #Build output
8 | bin/
9 | gen/
10 | out/
11 | build/
12 |
13 | #Project Files
14 | .idea/
15 | *.iml
16 | .classpath
17 | .project
18 |
19 | #Ant config files
20 | local.properties
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ### Mastering Android Touch Examples ###
2 |
3 | This repository contains a sample application demonstrating many of the techniques for doing custom touch event handling discussed in "Mastering the Android Touch System".
4 |
5 | Examples include:
6 |
7 | - Overriding onTouchEvent()
8 | - Overriding onInterceptTouchEvent()
9 | - GestureDetector
10 | - ScaleGestureDetector
11 | - TouchDelegate
12 |
13 | The sample code is licensed under the MIT Open Source License
--------------------------------------------------------------------------------
/TouchExamples/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
9 |
12 |
13 |
14 |
15 |
16 |
17 |
20 |
23 |
26 |
29 |
32 |
35 |
38 |
41 |
42 |
43 |
--------------------------------------------------------------------------------
/TouchExamples/assets/android.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/devunwired/custom-touch-examples/14a58187b329f72d2cd26a7224b0a440a0ec7d91/TouchExamples/assets/android.jpg
--------------------------------------------------------------------------------
/TouchExamples/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 25
5 | buildToolsVersion "25.0.2"
6 |
7 | defaultConfig {
8 | minSdkVersion 16
9 | targetSdkVersion 25
10 | versionName "1.0"
11 | versionCode 1
12 | }
13 |
14 | sourceSets {
15 | main {
16 | assets.srcDirs = ['assets']
17 | res.srcDirs = ['res']
18 | aidl.srcDirs = ['src']
19 | resources.srcDirs = ['src']
20 | renderscript.srcDirs = ['src']
21 | java.srcDirs = ['src']
22 | manifest.srcFile 'AndroidManifest.xml'
23 | }
24 | }
25 |
26 | dependencies {
27 | compile 'com.android.support:appcompat-v7:25.3.0'
28 | }
29 | }
--------------------------------------------------------------------------------
/TouchExamples/project.properties:
--------------------------------------------------------------------------------
1 | # This file is automatically generated by Android Tools.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file must be checked in Version Control Systems.
5 | #
6 | # To customize properties used by the Ant build system use,
7 | # "ant.properties", and override values to adapt the script to your
8 | # project structure.
9 |
10 | # Project target.
11 | target=android-15
12 |
--------------------------------------------------------------------------------
/TouchExamples/res/drawable-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/devunwired/custom-touch-examples/14a58187b329f72d2cd26a7224b0a440a0ec7d91/TouchExamples/res/drawable-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/TouchExamples/res/drawable-ldpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/devunwired/custom-touch-examples/14a58187b329f72d2cd26a7224b0a440a0ec7d91/TouchExamples/res/drawable-ldpi/ic_launcher.png
--------------------------------------------------------------------------------
/TouchExamples/res/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/devunwired/custom-touch-examples/14a58187b329f72d2cd26a7224b0a440a0ec7d91/TouchExamples/res/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/TouchExamples/res/layout/gesture_scroll.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
8 |
12 |
13 |
--------------------------------------------------------------------------------
/TouchExamples/res/layout/intercept_row.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/TouchExamples/res/layout/move_logger.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
15 |
24 |
--------------------------------------------------------------------------------
/TouchExamples/res/layout/touch_intercept.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/TouchExamples/res/layout/touch_listener.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
12 |
16 |
22 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/TouchExamples/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 160dp
4 | 1280dp
5 | 720dp
6 |
--------------------------------------------------------------------------------
/TouchExamples/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/TouchExamples/src/com/examples/customtouch/MainActivity.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 Wireless Designs, LLC
3 | *
4 | * See the file license.txt for copying permission.
5 | */
6 | package com.examples.customtouch;
7 |
8 | import android.content.Intent;
9 | import android.os.Bundle;
10 | import android.support.v7.app.AppCompatActivity;
11 | import android.view.View;
12 | import android.widget.AdapterView;
13 | import android.widget.ArrayAdapter;
14 | import android.widget.ListView;
15 |
16 | public class MainActivity extends AppCompatActivity implements
17 | AdapterView.OnItemClickListener {
18 |
19 | private static final String[] ITEMS = {
20 | "Move Logger Example", "Touch Listener Example",
21 | "Touch Delegate Example", "Touch Forward Example",
22 | "Pan Example", "Pan Gesture Example",
23 | "Multi-Touch Example", "Disable Touch Intercept"};
24 |
25 | @Override
26 | protected void onCreate(Bundle savedInstanceState) {
27 | super.onCreate(savedInstanceState);
28 | ListView list = new ListView(this);
29 | setContentView(list);
30 |
31 | ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, ITEMS);
32 | list.setAdapter(adapter);
33 | list.setOnItemClickListener(this);
34 | }
35 |
36 | @Override
37 | public void onItemClick(AdapterView> parent, View view, int position, long id) {
38 | switch (position) {
39 | case 0: //Move Logger View
40 | startActivity(new Intent(this, MoveLoggerActivity.class));
41 | break;
42 | case 1: //Touch Listener
43 | startActivity(new Intent(this, TouchListenerActivity.class));
44 | break;
45 | case 2: //Touch Delegate
46 | startActivity(new Intent(this, TouchDelegateActivity.class));
47 | break;
48 | case 3: //Touch Forwarding
49 | startActivity(new Intent(this, TouchForwardActivity.class));
50 | break;
51 | case 4: //2D Scrolling
52 | startActivity(new Intent(this, TwoDimensionScrollActivity.class));
53 | break;
54 | case 5: //2D GestureDetector Scrolling
55 | startActivity(new Intent(this, TwoDimensionGestureScrollActivity.class));
56 | break;
57 | case 6: //Multi-Touch Image View
58 | startActivity(new Intent(this, MultitouchActivity.class));
59 | break;
60 | case 7: //Disable Touch Intercept
61 | startActivity(new Intent(this, TouchInterceptActivity.class));
62 | default:
63 | break;
64 | }
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/TouchExamples/src/com/examples/customtouch/MoveLoggerActivity.java:
--------------------------------------------------------------------------------
1 | package com.examples.customtouch;
2 |
3 | import android.graphics.Point;
4 | import android.os.Bundle;
5 | import android.support.v7.app.AppCompatActivity;
6 | import android.util.Log;
7 | import android.view.MotionEvent;
8 | import android.view.View;
9 | import android.view.ViewConfiguration;
10 |
11 | /**
12 | * Created by Dave Smith
13 | * Double Encore, Inc.
14 | * Date: 9/24/12
15 | * MoveLoggerActivity
16 | */
17 | public class MoveLoggerActivity extends AppCompatActivity implements
18 | View.OnTouchListener {
19 |
20 | public static final String TAG = "MoveLoggerActivity";
21 |
22 | /* Slop constant for this device */
23 | private int mTouchSlop;
24 | /* Initial touch point */
25 | private Point mInitialTouch;
26 |
27 | @Override
28 | protected void onCreate(Bundle savedInstanceState) {
29 | super.onCreate(savedInstanceState);
30 | setContentView(R.layout.move_logger);
31 |
32 | findViewById(R.id.view_logall).setOnTouchListener(this);
33 | findViewById(R.id.view_logslop).setOnTouchListener(this);
34 |
35 | mTouchSlop = ViewConfiguration.get(this).getScaledTouchSlop();
36 | mInitialTouch = new Point();
37 | }
38 |
39 | @Override
40 | public boolean onTouch(View v, MotionEvent event) {
41 | if(event.getAction() == MotionEvent.ACTION_DOWN) {
42 | mInitialTouch.set((int)event.getX(), (int)event.getY());
43 | //Must declare interest to get more events
44 | return true;
45 | } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
46 | switch (v.getId()) {
47 | case R.id.view_logall:
48 | Log.i(TAG, String.format("Top Move: %.1f,%.1f", event.getX(), event.getY()));
49 | break;
50 | case R.id.view_logslop:
51 | if ( Math.abs(event.getX() - mInitialTouch.x) > mTouchSlop
52 | || Math.abs(event.getY() - mInitialTouch.y) > mTouchSlop ) {
53 | Log.i(TAG, String.format("Bottom Move: %.1f,%.1f", event.getX(), event.getY()));
54 | }
55 | break;
56 | default:
57 | break;
58 | }
59 | }
60 | //Don't interefere when not necessary
61 | return false;
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/TouchExamples/src/com/examples/customtouch/MultitouchActivity.java:
--------------------------------------------------------------------------------
1 | package com.examples.customtouch;
2 |
3 | import android.graphics.Bitmap;
4 | import android.graphics.BitmapFactory;
5 | import android.os.Bundle;
6 | import android.support.v7.app.AppCompatActivity;
7 |
8 | import com.examples.customtouch.widget.RotateZoomImageView;
9 |
10 | import java.io.IOException;
11 | import java.io.InputStream;
12 |
13 | /**
14 | * Created by Dave Smith
15 | * Double Encore, Inc.
16 | * Date: 9/24/12
17 | * MultitouchActivity
18 | */
19 | public class MultitouchActivity extends AppCompatActivity {
20 |
21 | @Override
22 | protected void onCreate(Bundle savedInstanceState) {
23 | super.onCreate(savedInstanceState);
24 |
25 | RotateZoomImageView iv = new RotateZoomImageView(this);
26 |
27 | Bitmap image;
28 | try {
29 | InputStream in = getAssets().open("android.jpg");
30 | image = BitmapFactory.decodeStream(in);
31 | in.close();
32 | } catch (IOException e) {
33 | image = null;
34 | }
35 | iv.setImageBitmap(image);
36 |
37 | setContentView(iv);
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/TouchExamples/src/com/examples/customtouch/TouchDelegateActivity.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 Wireless Designs, LLC
3 | *
4 | * See the file license.txt for copying permission.
5 | */
6 | package com.examples.customtouch;
7 |
8 | import android.os.Bundle;
9 | import android.support.v7.app.AppCompatActivity;
10 |
11 | import com.examples.customtouch.widget.TouchDelegateLayout;
12 |
13 | public class TouchDelegateActivity extends AppCompatActivity {
14 |
15 | @Override
16 | protected void onCreate(Bundle savedInstanceState) {
17 | super.onCreate(savedInstanceState);
18 | TouchDelegateLayout layout = new TouchDelegateLayout(this);
19 | setContentView(layout);
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/TouchExamples/src/com/examples/customtouch/TouchForwardActivity.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 Wireless Designs, LLC
3 | *
4 | * See the file license.txt for copying permission.
5 | */
6 | package com.examples.customtouch;
7 |
8 | import android.os.Bundle;
9 | import android.support.v7.app.AppCompatActivity;
10 | import android.view.Gravity;
11 | import android.view.ViewGroup.LayoutParams;
12 | import android.widget.Button;
13 | import android.widget.FrameLayout;
14 |
15 | import com.examples.customtouch.widget.TouchForwardLayout;
16 |
17 | public class TouchForwardActivity extends AppCompatActivity {
18 |
19 | @Override
20 | protected void onCreate(Bundle savedInstanceState) {
21 | super.onCreate(savedInstanceState);
22 |
23 | TouchForwardLayout layout = new TouchForwardLayout(this);
24 |
25 | Button button = new Button(this);
26 | button.setText("You Can't Miss Me!");
27 |
28 | FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
29 | LayoutParams.WRAP_CONTENT, Gravity.CENTER);
30 | layout.addView(button, lp);
31 | setContentView(layout);
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/TouchExamples/src/com/examples/customtouch/TouchInterceptActivity.java:
--------------------------------------------------------------------------------
1 | package com.examples.customtouch;
2 |
3 | import android.content.Context;
4 | import android.os.Bundle;
5 | import android.support.v4.view.PagerAdapter;
6 | import android.support.v4.view.ViewPager;
7 | import android.support.v7.app.AppCompatActivity;
8 | import android.view.Gravity;
9 | import android.view.LayoutInflater;
10 | import android.view.View;
11 | import android.view.ViewGroup;
12 | import android.widget.BaseAdapter;
13 | import android.widget.ListView;
14 | import android.widget.TextView;
15 |
16 | /**
17 | * Created by Dave Smith
18 | * Double Encore, Inc.
19 | * Date: 1/19/13
20 | * TouchInterceptActivity
21 | */
22 | public class TouchInterceptActivity extends AppCompatActivity implements
23 | ViewPager.OnPageChangeListener {
24 |
25 | private ViewPager mViewPager;
26 | private ListView mListView;
27 |
28 | public void onCreate(Bundle savedInstanceState) {
29 | super.onCreate(savedInstanceState);
30 | setContentView(R.layout.touch_intercept);
31 |
32 | mViewPager = new ViewPager(this);
33 | mViewPager.setLayoutParams(new ListView.LayoutParams(ListView.LayoutParams.MATCH_PARENT,
34 | getResources().getDimensionPixelSize(R.dimen.header_height)));
35 | mListView = (ListView) findViewById(R.id.list);
36 |
37 | mListView.addHeaderView(mViewPager);
38 | mListView.setAdapter(new ItemsAdapter(this));
39 |
40 | mViewPager.setOnPageChangeListener(this);
41 | mViewPager.setAdapter(new HeaderAdapter(this));
42 | }
43 |
44 | @Override
45 | public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { }
46 |
47 | @Override
48 | public void onPageSelected(int position) { }
49 |
50 | @Override
51 | public void onPageScrollStateChanged(int state) {
52 | //While the ViewPager is scrolling, disable the ScrollView touch intercept
53 | // so it cannot take over and try to vertical scroll
54 | boolean isScrolling = state != ViewPager.SCROLL_STATE_IDLE;
55 | mListView.requestDisallowInterceptTouchEvent(isScrolling);
56 | }
57 |
58 | /*
59 | * Simple ListAdapter that draws a series of row items
60 | */
61 | private static class ItemsAdapter extends BaseAdapter {
62 |
63 | private LayoutInflater mInflater;
64 |
65 | public ItemsAdapter(Context context) {
66 | mInflater = LayoutInflater.from(context);
67 | }
68 |
69 | @Override
70 | public int getCount() {
71 | return 25;
72 | }
73 |
74 | @Override
75 | public Object getItem(int position) {
76 | return null;
77 | }
78 |
79 | @Override
80 | public long getItemId(int position) {
81 | return 0;
82 | }
83 |
84 | @Override
85 | public View getView(int position, View convertView, ViewGroup parent) {
86 | if (convertView == null) {
87 | convertView = mInflater.inflate(R.layout.intercept_row, parent, false);
88 | }
89 |
90 | TextView v = (TextView) convertView.findViewById(R.id.text);
91 | v.setText(String.format("Item Row %d", position + 1));
92 |
93 | return convertView;
94 | }
95 | }
96 |
97 | /*
98 | * Simple PagerAdapter that just draws a small group of colored views
99 | */
100 | private static class HeaderAdapter extends PagerAdapter {
101 | private static final int[] COLORS = {0xFF555500, 0xFF770077, 0xFF007777, 0xFF777777};
102 |
103 | private Context mContext;
104 |
105 | public HeaderAdapter(Context context) {
106 | mContext = context;
107 | }
108 |
109 | @Override
110 | public Object instantiateItem(ViewGroup container, int position) {
111 | TextView v = new TextView(mContext);
112 | v.setBackgroundColor(COLORS[position]);
113 | v.setText(String.format("Header Card %d", position + 1));
114 | v.setGravity(Gravity.CENTER);
115 | container.addView(v);
116 |
117 | return v;
118 | }
119 |
120 | @Override
121 | public void destroyItem(ViewGroup container, int position, Object object) {
122 | container.removeView((View) object);
123 | }
124 |
125 | @Override
126 | public int getCount() {
127 | return COLORS.length;
128 | }
129 |
130 | @Override
131 | public boolean isViewFromObject(View view, Object object) {
132 | return (view == object);
133 | }
134 | }
135 | }
--------------------------------------------------------------------------------
/TouchExamples/src/com/examples/customtouch/TouchListenerActivity.java:
--------------------------------------------------------------------------------
1 | package com.examples.customtouch;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.view.MotionEvent;
6 | import android.view.View;
7 | import android.widget.CheckBox;
8 |
9 | /**
10 | * Created by Dave Smith
11 | * Double Encore, Inc.
12 | * Date: 9/25/12
13 | * TouchListenerActivity
14 | */
15 | public class TouchListenerActivity extends AppCompatActivity implements
16 | View.OnTouchListener {
17 |
18 | /* Views to display last seen touch event */
19 | CheckBox mLockBox;
20 |
21 | @Override
22 | protected void onCreate(Bundle savedInstanceState) {
23 | super.onCreate(savedInstanceState);
24 | setContentView(R.layout.touch_listener);
25 |
26 | mLockBox = (CheckBox) findViewById(R.id.checkbox_lock);
27 |
28 | findViewById(R.id.selection_first).setOnTouchListener(this);
29 | findViewById(R.id.selection_second).setOnTouchListener(this);
30 | findViewById(R.id.selection_third).setOnTouchListener(this);
31 | }
32 |
33 | @Override
34 | public boolean onTouch(View v, MotionEvent event) {
35 | /*
36 | * Consume the events here so the buttons cannot process them
37 | * if the CheckBox in the UI is checked
38 | */
39 | return mLockBox.isChecked();
40 | }
41 |
42 | private String getNameForEvent(MotionEvent event) {
43 | String action = "";
44 | switch (event.getAction()) {
45 | case MotionEvent.ACTION_DOWN:
46 | action = "ACTION_DOWN";
47 | break;
48 | case MotionEvent.ACTION_CANCEL:
49 | action = "ACTION_CANCEL";
50 | break;
51 | case MotionEvent.ACTION_MOVE:
52 | action = "ACTION_MOVE";
53 | break;
54 | case MotionEvent.ACTION_UP:
55 | action = "ACTION_UP";
56 | break;
57 | default:
58 | return null;
59 | }
60 |
61 | return String.format("%s\n%.1f, %.1f", action, event.getX(), event.getY());
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/TouchExamples/src/com/examples/customtouch/TwoDimensionGestureScrollActivity.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 Wireless Designs, LLC
3 | *
4 | * See the file license.txt for copying permission.
5 | */
6 | package com.examples.customtouch;
7 |
8 | import android.graphics.Bitmap;
9 | import android.graphics.BitmapFactory;
10 | import android.os.Bundle;
11 | import android.support.v7.app.AppCompatActivity;
12 | import android.widget.ImageView;
13 |
14 | import java.io.IOException;
15 | import java.io.InputStream;
16 |
17 | public class TwoDimensionGestureScrollActivity extends AppCompatActivity {
18 |
19 | @Override
20 | protected void onCreate(Bundle savedInstanceState) {
21 | super.onCreate(savedInstanceState);
22 | setContentView(R.layout.gesture_scroll);
23 | ImageView iv = (ImageView) findViewById(R.id.imageView);
24 |
25 | Bitmap image;
26 | try {
27 | InputStream in = getAssets().open("android.jpg");
28 | image = BitmapFactory.decodeStream(in);
29 | in.close();
30 | } catch (IOException e) {
31 | image = null;
32 | }
33 | iv.setImageBitmap(image);
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/TouchExamples/src/com/examples/customtouch/TwoDimensionScrollActivity.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 Wireless Designs, LLC
3 | *
4 | * See the file license.txt for copying permission.
5 | */
6 | package com.examples.customtouch;
7 |
8 | import android.os.Bundle;
9 | import android.support.v7.app.AppCompatActivity;
10 | import android.widget.ImageButton;
11 | import android.widget.ImageView;
12 | import android.widget.LinearLayout;
13 |
14 | import com.examples.customtouch.widget.TwoDimensionScrollView;
15 |
16 | public class TwoDimensionScrollActivity extends AppCompatActivity {
17 |
18 | @Override
19 | protected void onCreate(Bundle savedInstanceState) {
20 | super.onCreate(savedInstanceState);
21 |
22 | TwoDimensionScrollView scrollView = new TwoDimensionScrollView(this);
23 |
24 | LinearLayout layout = new LinearLayout(this);
25 | layout.setOrientation(LinearLayout.VERTICAL);
26 | for(int i=0; i < 5; i++) {
27 | ImageButton iv = new ImageButton(this);
28 | iv.setImageResource(R.drawable.ic_launcher);
29 | iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
30 | int width = getResources().getDimensionPixelSize(R.dimen.pan_content_width);
31 | int height = getResources().getDimensionPixelSize(R.dimen.pan_content_height);
32 | layout.addView(iv, new LinearLayout.LayoutParams(width, height));
33 | }
34 |
35 | scrollView.addView(layout);
36 | setContentView(scrollView);
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/TouchExamples/src/com/examples/customtouch/widget/RotateZoomImageView.java:
--------------------------------------------------------------------------------
1 | package com.examples.customtouch.widget;
2 |
3 | import android.content.Context;
4 | import android.graphics.Matrix;
5 | import android.util.AttributeSet;
6 | import android.view.MotionEvent;
7 | import android.view.ScaleGestureDetector;
8 | import android.widget.ImageView;
9 |
10 | /**
11 | * Created by Dave Smith
12 | * Double Encore, Inc.
13 | * Date: 9/24/12
14 | * RotateZoomImageView
15 | */
16 | public class RotateZoomImageView extends ImageView {
17 | private ScaleGestureDetector mScaleDetector;
18 | private Matrix mImageMatrix;
19 | /* Last Rotation Angle */
20 | private int mLastAngle = 0;
21 | /* Pivot Point for Transforms */
22 | private int mPivotX, mPivotY;
23 |
24 | public RotateZoomImageView(Context context) {
25 | super(context);
26 | init(context);
27 | }
28 |
29 | public RotateZoomImageView(Context context, AttributeSet attrs) {
30 | super(context, attrs);
31 | init(context);
32 | }
33 |
34 | public RotateZoomImageView(Context context, AttributeSet attrs, int defStyle) {
35 | super(context, attrs, defStyle);
36 | init(context);
37 | }
38 |
39 | private void init(Context context) {
40 | mScaleDetector = new ScaleGestureDetector(context, mScaleListener);
41 |
42 | setScaleType(ScaleType.MATRIX);
43 | mImageMatrix = new Matrix();
44 | }
45 |
46 | /*
47 | * Use onSizeChanged() to calculate values based on the view's size.
48 | * The view has no size during init(), so we must wait for this
49 | * callback.
50 | */
51 | @Override
52 | protected void onSizeChanged(int w, int h, int oldw, int oldh) {
53 | if (w != oldw || h != oldh) {
54 | //Shift the image to the center of the view
55 | int translateX = (w - getDrawable().getIntrinsicWidth()) / 2;
56 | int translateY = (h - getDrawable().getIntrinsicHeight()) / 2;
57 | mImageMatrix.setTranslate(translateX, translateY);
58 | setImageMatrix(mImageMatrix);
59 | //Get the center point for future scale and rotate transforms
60 | mPivotX = w / 2;
61 | mPivotY = h / 2;
62 | }
63 | }
64 |
65 | private ScaleGestureDetector.SimpleOnScaleGestureListener mScaleListener = new ScaleGestureDetector.SimpleOnScaleGestureListener() {
66 | @Override
67 | public boolean onScale(ScaleGestureDetector detector) {
68 | // ScaleGestureDetector calculates a scale factor based on whether
69 | // the fingers are moving apart or together
70 | float scaleFactor = detector.getScaleFactor();
71 | //Pass that factor to a scale for the image
72 | mImageMatrix.postScale(scaleFactor, scaleFactor, mPivotX, mPivotY);
73 | setImageMatrix(mImageMatrix);
74 |
75 | return true;
76 | }
77 | };
78 |
79 | /*
80 | * Operate on two-finger events to rotate the image.
81 | * This method calculates the change in angle between the
82 | * pointers and rotates the image accordingly. As the user
83 | * rotates their fingers, the image will follow.
84 | */
85 | private boolean doRotationEvent(MotionEvent event) {
86 | //Calculate the angle between the two fingers
87 | float deltaX = event.getX(0) - event.getX(1);
88 | float deltaY = event.getY(0) - event.getY(1);
89 | double radians = Math.atan(deltaY / deltaX);
90 | //Convert to degrees
91 | int degrees = (int)(radians * 180 / Math.PI);
92 |
93 | /*
94 | * Must use getActionMasked() for switching to pick up pointer events.
95 | * These events have the pointer index encoded in them so the return
96 | * from getAction() won't match the exact action constant.
97 | */
98 | switch (event.getActionMasked()) {
99 | case MotionEvent.ACTION_DOWN:
100 | case MotionEvent.ACTION_POINTER_DOWN:
101 | case MotionEvent.ACTION_POINTER_UP:
102 | //Mark the initial angle
103 | mLastAngle = degrees;
104 | break;
105 | case MotionEvent.ACTION_MOVE:
106 | // ATAN returns a converted value between -90deg and +90deg
107 | // which creates a point when two fingers are vertical where the
108 | // angle flips sign. We handle this case by rotating a small amount
109 | // (5 degrees) in the direction we were traveling
110 | if ((degrees - mLastAngle) > 45) {
111 | //Going CCW across the boundary
112 | mImageMatrix.postRotate(-5, mPivotX, mPivotY);
113 | } else if ((degrees - mLastAngle) < -45) {
114 | //Going CW across the boundary
115 | mImageMatrix.postRotate(5, mPivotX, mPivotY);
116 | } else {
117 | //Normal rotation, rotate the difference
118 | mImageMatrix.postRotate(degrees - mLastAngle, mPivotX, mPivotY);
119 | }
120 | //Post the rotation to the image
121 | setImageMatrix(mImageMatrix);
122 | //Save the current angle
123 | mLastAngle = degrees;
124 | break;
125 | }
126 |
127 | return true;
128 | }
129 |
130 | @Override
131 | public boolean onTouchEvent(MotionEvent event) {
132 | if (event.getAction() == MotionEvent.ACTION_DOWN) {
133 | // We don't care about this event directly, but we declare
134 | // interest so we can get later multi-touch events.
135 | return true;
136 | }
137 |
138 |
139 | switch (event.getPointerCount()) {
140 | case 3:
141 | // With three fingers down, zoom the image
142 | // using the ScaleGestureDetector
143 | return mScaleDetector.onTouchEvent(event);
144 | case 2:
145 | // With two fingers down, rotate the image
146 | // following the fingers
147 | return doRotationEvent(event);
148 | default:
149 | //Ignore this event
150 | return super.onTouchEvent(event);
151 | }
152 | }
153 | }
154 |
--------------------------------------------------------------------------------
/TouchExamples/src/com/examples/customtouch/widget/TouchDelegateLayout.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 Wireless Designs, LLC
3 | *
4 | * See the file license.txt for copying permission.
5 | */
6 | package com.examples.customtouch.widget;
7 |
8 | import android.content.Context;
9 | import android.graphics.Rect;
10 | import android.util.AttributeSet;
11 | import android.view.Gravity;
12 | import android.view.TouchDelegate;
13 | import android.widget.CheckBox;
14 | import android.widget.FrameLayout;
15 |
16 | public class TouchDelegateLayout extends FrameLayout {
17 |
18 | public TouchDelegateLayout(Context context) {
19 | super(context);
20 | init();
21 | }
22 |
23 | public TouchDelegateLayout(Context context, AttributeSet attrs) {
24 | super(context, attrs);
25 | init();
26 | }
27 |
28 | public TouchDelegateLayout(Context context, AttributeSet attrs, int defStyle) {
29 | super(context, attrs, defStyle);
30 | init();
31 | }
32 |
33 | private CheckBox mButton;
34 | private void init() {
35 | mButton = new CheckBox(getContext());
36 | mButton.setText("Click Anywhere On Screen");
37 |
38 | addView(mButton, new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER));
39 | }
40 |
41 | /*
42 | * TouchDelegate is applied to this view (parent) to delegate all touches
43 | * within the specified rectangle to the CheckBox (child). Here, the rectangle
44 | * is the entire size of this parent view.
45 | *
46 | * This must be done after the view has measured itself so we know how big to make the rect,
47 | * thus we've chosen to add the delegate in onMeasure()
48 | */
49 | @Override
50 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
51 | super.onMeasure(widthMeasureSpec, heightMeasureSpec);
52 | //Apply the whole area of this view as the delegate area
53 | Rect bounds = new Rect(0, 0, getMeasuredWidth(), getMeasuredHeight());
54 | TouchDelegate delegate = new TouchDelegate(bounds, mButton);
55 | setTouchDelegate(delegate);
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/TouchExamples/src/com/examples/customtouch/widget/TouchForwardLayout.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2012 Wireless Designs, LLC
3 | *
4 | * See the file license.txt for copying permission.
5 | */
6 | package com.examples.customtouch.widget;
7 |
8 | import android.content.Context;
9 | import android.graphics.Point;
10 | import android.util.AttributeSet;
11 | import android.view.MotionEvent;
12 | import android.view.View;
13 | import android.widget.FrameLayout;
14 |
15 | public class TouchForwardLayout extends FrameLayout {
16 |
17 | private Point mTouchOffsetPoint = new Point();
18 |
19 | public TouchForwardLayout(Context context) {
20 | super(context);
21 | }
22 |
23 | public TouchForwardLayout(Context context, AttributeSet attrs) {
24 | super(context, attrs);
25 | }
26 |
27 | public TouchForwardLayout(Context context, AttributeSet attrs, int defStyle) {
28 | super(context, attrs, defStyle);
29 | }
30 |
31 | @Override
32 | public boolean onInterceptTouchEvent(MotionEvent ev) {
33 | //Don't let any touches be passed down to the children automatically
34 | return true;
35 | }
36 |
37 | /*
38 | * In onTouchEvent(), we pass all the touches we receive directly to the
39 | * first child by calling its dispatchTouchEvent() method.
40 | *
41 | * Note that, we modify the event so the initial location of the touch will
42 | * will report as being centered in the view we are forwarding to. Each event after
43 | * this will be offset by the same amount, which creates the effect that the finger. This may have consequences depending on the reasoning for forwarding the event.
44 | * MotionEvent.setLocation() can be used to modify the x/y before forwarding.
45 | */
46 | @Override
47 | public boolean onTouchEvent(MotionEvent event) {
48 | //Forward all touch events to the first child
49 | View child = getChildAt(0);
50 | if(child == null) {
51 | return false;
52 | }
53 |
54 | if (event.getAction() == MotionEvent.ACTION_DOWN) {
55 | //Update this as the offset point
56 | mTouchOffsetPoint.x = (int)event.getX();
57 | mTouchOffsetPoint.y = (int)event.getY();
58 | }
59 |
60 | //Massage the event to be offset from the first touch
61 | event.offsetLocation(-mTouchOffsetPoint.x + child.getWidth() / 2,
62 | -mTouchOffsetPoint.y + child.getHeight() / 2);
63 |
64 | return child.dispatchTouchEvent(event);
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/TouchExamples/src/com/examples/customtouch/widget/TwoDimensionGestureScrollView.java:
--------------------------------------------------------------------------------
1 | package com.examples.customtouch.widget;
2 |
3 | import android.content.Context;
4 | import android.util.AttributeSet;
5 | import android.view.GestureDetector;
6 | import android.view.GestureDetector.SimpleOnGestureListener;
7 | import android.view.MotionEvent;
8 | import android.view.View;
9 | import android.view.ViewConfiguration;
10 | import android.widget.FrameLayout;
11 | import android.widget.OverScroller;
12 |
13 | public class TwoDimensionGestureScrollView extends FrameLayout {
14 |
15 | private GestureDetector mDetector;
16 | private OverScroller mScroller;
17 |
18 | /* Positions of the last motion event */
19 | private float mInitialX, mInitialY;
20 | /* Drag threshold */
21 | private int mTouchSlop;
22 |
23 | public TwoDimensionGestureScrollView(Context context) {
24 | super(context);
25 | init(context);
26 | }
27 |
28 | public TwoDimensionGestureScrollView(Context context, AttributeSet attrs) {
29 | super(context, attrs);
30 | init(context);
31 | }
32 |
33 | public TwoDimensionGestureScrollView(Context context, AttributeSet attrs, int defStyle) {
34 | super(context, attrs, defStyle);
35 | init(context);
36 | }
37 |
38 | private void init(Context context) {
39 | mDetector = new GestureDetector(context, mListener);
40 | mScroller = new OverScroller(context);
41 | //Get system constants for touch thresholds
42 | mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
43 | }
44 |
45 | /*
46 | * Override the measureChild... implementations to guarantee that the child view
47 | * gets measured to be as large as it wants to be. The default implementation will
48 | * force some children to be only as large as this view.
49 | */
50 | @Override
51 | protected void measureChild(View child, int parentWidthMeasureSpec, int parentHeightMeasureSpec) {
52 | int childWidthMeasureSpec;
53 | int childHeightMeasureSpec;
54 |
55 | childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
56 | childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
57 |
58 | child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
59 | }
60 |
61 | @Override
62 | protected void measureChildWithMargins(View child, int parentWidthMeasureSpec, int widthUsed,
63 | int parentHeightMeasureSpec, int heightUsed) {
64 | final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
65 |
66 | final int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(
67 | lp.leftMargin + lp.rightMargin, MeasureSpec.UNSPECIFIED);
68 | final int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
69 | lp.topMargin + lp.bottomMargin, MeasureSpec.UNSPECIFIED);
70 |
71 | child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
72 | }
73 |
74 | @Override
75 | public void computeScroll() {
76 | if (mScroller.computeScrollOffset()) {
77 | // This is called at drawing time by ViewGroup. We use
78 | // this method to keep the fling animation going through
79 | // to completion.
80 | int x = mScroller.getCurrX();
81 | int y = mScroller.getCurrY();
82 |
83 | scrollTo(x, y);
84 |
85 | // Keep on drawing until the animation has finished.
86 | postInvalidate();
87 | }
88 | }
89 |
90 | //Override scrollTo to do bounds checks on any scrolling request
91 | @Override
92 | public void scrollTo(int x, int y) {
93 | // we rely on the fact the View.scrollBy calls scrollTo.
94 | if (getChildCount() > 0) {
95 | View child = getChildAt(0);
96 | x = clamp(x, getWidth() - getPaddingRight() - getPaddingLeft(), child.getWidth());
97 | y = clamp(y, getHeight() - getPaddingBottom() - getPaddingTop(), child.getHeight());
98 | if (x != getScrollX() || y != getScrollY()) {
99 | super.scrollTo(x, y);
100 | }
101 | }
102 | }
103 |
104 | /*
105 | * Utility method to initialize the Scroller and start redrawing
106 | */
107 | public void fling(int velocityX, int velocityY) {
108 | if (getChildCount() > 0) {
109 | int height = getHeight() - getPaddingBottom() - getPaddingTop();
110 | int width = getWidth() - getPaddingLeft() - getPaddingRight();
111 | int bottom = getChildAt(0).getHeight();
112 | int right = getChildAt(0).getWidth();
113 |
114 | mScroller.fling(getScrollX(), getScrollY(), velocityX, velocityY,
115 | 0, Math.max(0, right - width),
116 | 0, Math.max(0, bottom - height));
117 |
118 | invalidate();
119 | }
120 | }
121 |
122 | /*
123 | * Utility method to assist in doing bounds checking
124 | */
125 | private int clamp(int n, int my, int child) {
126 | if (my >= child || n < 0) {
127 | /* my >= child is this case:
128 | * |--------------- me ---------------|
129 | * |------ child ------|
130 | * or
131 | * |--------------- me ---------------|
132 | * |------ child ------|
133 | * or
134 | * |--------------- me ---------------|
135 | * |------ child ------|
136 | *
137 | * n < 0 is this case:
138 | * |------ me ------|
139 | * |-------- child --------|
140 | * |-- mScrollX --|
141 | */
142 | return 0;
143 | }
144 | if ((my+n) > child) {
145 | /* this case:
146 | * |------ me ------|
147 | * |------ child ------|
148 | * |-- mScrollX --|
149 | */
150 | return child-my;
151 | }
152 | return n;
153 | }
154 |
155 | //Listener to handle all the touch events
156 | private SimpleOnGestureListener mListener = new SimpleOnGestureListener() {
157 | public boolean onDown(MotionEvent e) {
158 | //Cancel any current fling
159 | if (!mScroller.isFinished()) {
160 | mScroller.abortAnimation();
161 | }
162 | return true;
163 | }
164 |
165 | public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
166 | //Call a helper method to start the scroller animation
167 | fling((int)-velocityX/2, (int)-velocityY/2);
168 | return true;
169 | }
170 |
171 | public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
172 | //Any view can be scrolled by simply calling its scrollBy() method
173 | scrollBy((int)distanceX, (int)distanceY);
174 | return true;
175 | }
176 | };
177 |
178 | /*
179 | * Monitor touch events passed down to the children and
180 | * intercept as soon as it is determined we are dragging
181 | */
182 | @Override
183 | public boolean onInterceptTouchEvent(MotionEvent event) {
184 | switch (event.getAction()) {
185 | case MotionEvent.ACTION_DOWN:
186 | mInitialX = event.getX();
187 | mInitialY = event.getY();
188 | //Feed the down event to the detector so it has
189 | // context when/if dragging begins
190 | mDetector.onTouchEvent(event);
191 | break;
192 | case MotionEvent.ACTION_MOVE:
193 | final float x = event.getX();
194 | final float y = event.getY();
195 | final int yDiff = (int) Math.abs(y - mInitialY);
196 | final int xDiff = (int) Math.abs(x - mInitialX);
197 | //Verify that either difference is enough to be a drag
198 | if (yDiff > mTouchSlop || xDiff > mTouchSlop) {
199 | //Start capturing events
200 | return true;
201 | }
202 | break;
203 | }
204 |
205 | return super.onInterceptTouchEvent(event);
206 | }
207 |
208 | /*
209 | * Feed all touch events we receive to the detector for
210 | * processing.
211 | */
212 | @Override
213 | public boolean onTouchEvent(MotionEvent event) {
214 | return mDetector.onTouchEvent(event);
215 | }
216 | }
217 |
--------------------------------------------------------------------------------
/TouchExamples/src/com/examples/customtouch/widget/TwoDimensionScrollView.java:
--------------------------------------------------------------------------------
1 | package com.examples.customtouch.widget;
2 |
3 | import android.content.Context;
4 | import android.util.AttributeSet;
5 | import android.view.MotionEvent;
6 | import android.view.VelocityTracker;
7 | import android.view.View;
8 | import android.view.ViewConfiguration;
9 | import android.widget.FrameLayout;
10 | import android.widget.OverScroller;
11 |
12 | public class TwoDimensionScrollView extends FrameLayout {
13 |
14 | //Fling components
15 | private OverScroller mScroller;
16 | private VelocityTracker mVelocityTracker;
17 |
18 | /* Positions of the last motion event */
19 | private float mLastTouchX, mLastTouchY;
20 | /* Drag threshold */
21 | private int mTouchSlop;
22 | /* Fling Velocity */
23 | private int mMaximumVelocity, mMinimumVelocity;
24 | /* Drag Lock */
25 | private boolean mDragging = false;
26 |
27 | public TwoDimensionScrollView(Context context) {
28 | super(context);
29 | init(context);
30 | }
31 |
32 | public TwoDimensionScrollView(Context context, AttributeSet attrs) {
33 | super(context, attrs);
34 | init(context);
35 | }
36 |
37 | public TwoDimensionScrollView(Context context, AttributeSet attrs, int defStyle) {
38 | super(context, attrs, defStyle);
39 | init(context);
40 | }
41 |
42 | private void init(Context context) {
43 | mScroller = new OverScroller(context);
44 | mVelocityTracker = VelocityTracker.obtain();
45 | //Get system constants for touch thresholds
46 | mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
47 | mMaximumVelocity = ViewConfiguration.get(context).getScaledMaximumFlingVelocity();
48 | mMinimumVelocity = ViewConfiguration.get(context).getScaledMinimumFlingVelocity();
49 | }
50 |
51 | /*
52 | * Override the measureChild... implementations to guarantee that the child view
53 | * gets measured to be as large as it wants to be. The default implementation will
54 | * force some children to be only as large as this view.
55 | */
56 | @Override
57 | protected void measureChild(View child, int parentWidthMeasureSpec, int parentHeightMeasureSpec) {
58 | int childWidthMeasureSpec;
59 | int childHeightMeasureSpec;
60 |
61 | childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
62 | childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
63 |
64 | child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
65 | }
66 |
67 | @Override
68 | protected void measureChildWithMargins(View child, int parentWidthMeasureSpec, int widthUsed,
69 | int parentHeightMeasureSpec, int heightUsed) {
70 | final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
71 |
72 | final int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(
73 | lp.leftMargin + lp.rightMargin, MeasureSpec.UNSPECIFIED);
74 | final int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
75 | lp.topMargin + lp.bottomMargin, MeasureSpec.UNSPECIFIED);
76 |
77 | child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
78 | }
79 |
80 | @Override
81 | public void computeScroll() {
82 | if (mScroller.computeScrollOffset()) {
83 | // This is called at drawing time by ViewGroup. We use
84 | // this method to keep the fling animation going through
85 | // to completion.
86 | int oldX = getScrollX();
87 | int oldY = getScrollY();
88 | int x = mScroller.getCurrX();
89 | int y = mScroller.getCurrY();
90 |
91 | if (getChildCount() > 0) {
92 | View child = getChildAt(0);
93 | x = clamp(x, getWidth() - getPaddingRight() - getPaddingLeft(), child.getWidth());
94 | y = clamp(y, getHeight() - getPaddingBottom() - getPaddingTop(), child.getHeight());
95 | if (x != oldX || y != oldY) {
96 | scrollTo(x, y);
97 | }
98 | }
99 |
100 | // Keep on drawing until the animation has finished.
101 | postInvalidate();
102 | }
103 | }
104 |
105 | //Override scrollTo to do bounds checks on any scrolling request
106 | @Override
107 | public void scrollTo(int x, int y) {
108 | // we rely on the fact the View.scrollBy calls scrollTo.
109 | if (getChildCount() > 0) {
110 | View child = getChildAt(0);
111 | x = clamp(x, getWidth() - getPaddingRight() - getPaddingLeft(), child.getWidth());
112 | y = clamp(y, getHeight() - getPaddingBottom() - getPaddingTop(), child.getHeight());
113 | if (x != getScrollX() || y != getScrollY()) {
114 | super.scrollTo(x, y);
115 | }
116 | }
117 | }
118 |
119 | /*
120 | * Utility method to initialize the Scroller and start redrawing
121 | */
122 | public void fling(int velocityX, int velocityY) {
123 | if (getChildCount() > 0) {
124 | int height = getHeight() - getPaddingBottom() - getPaddingTop();
125 | int width = getWidth() - getPaddingLeft() - getPaddingRight();
126 | int bottom = getChildAt(0).getHeight();
127 | int right = getChildAt(0).getWidth();
128 |
129 | mScroller.fling(getScrollX(), getScrollY(), velocityX, velocityY,
130 | 0, Math.max(0, right - width),
131 | 0, Math.max(0, bottom - height));
132 |
133 | invalidate();
134 | }
135 | }
136 |
137 | /*
138 | * Utility method to assist in doing bounds checking
139 | */
140 | private int clamp(int n, int my, int child) {
141 | if (my >= child || n < 0) {
142 | /* my >= child is this case:
143 | * |--------------- me ---------------|
144 | * |------ child ------|
145 | * or
146 | * |--------------- me ---------------|
147 | * |------ child ------|
148 | * or
149 | * |--------------- me ---------------|
150 | * |------ child ------|
151 | *
152 | * n < 0 is this case:
153 | * |------ me ------|
154 | * |-------- child --------|
155 | * |-- mScrollX --|
156 | */
157 | return 0;
158 | }
159 | if ((my+n) > child) {
160 | /* this case:
161 | * |------ me ------|
162 | * |------ child ------|
163 | * |-- mScrollX --|
164 | */
165 | return child-my;
166 | }
167 | return n;
168 | }
169 |
170 | /*
171 | * Monitor touch events passed down to the children and
172 | * intercept as soon as it is determined we are dragging. This
173 | * allows child views to still receive touch events if they are
174 | * interactive (i.e. Buttons)
175 | */
176 | @Override
177 | public boolean onInterceptTouchEvent(MotionEvent event) {
178 | switch (event.getAction()) {
179 | case MotionEvent.ACTION_DOWN:
180 | //Stop any flinging in progress
181 | if (!mScroller.isFinished()) {
182 | mScroller.abortAnimation();
183 | }
184 | //Reset the velocity tracker
185 | mVelocityTracker.clear();
186 | mVelocityTracker.addMovement(event);
187 | //Save the initial touch point
188 | mLastTouchX = event.getX();
189 | mLastTouchY = event.getY();
190 | break;
191 | case MotionEvent.ACTION_MOVE:
192 | final float x = event.getX();
193 | final float y = event.getY();
194 | final int yDiff = (int) Math.abs(y - mLastTouchY);
195 | final int xDiff = (int) Math.abs(x - mLastTouchX);
196 | //Verify that either difference is enough to be a drag
197 | if (yDiff > mTouchSlop || xDiff > mTouchSlop) {
198 | mDragging = true;
199 | mVelocityTracker.addMovement(event);
200 | //Start capturing events ourselves
201 | return true;
202 | }
203 | break;
204 | case MotionEvent.ACTION_CANCEL:
205 | case MotionEvent.ACTION_UP:
206 | mDragging = false;
207 | mVelocityTracker.clear();
208 | break;
209 | }
210 |
211 | return super.onInterceptTouchEvent(event);
212 | }
213 |
214 | /*
215 | * Feed all touch events we receive to the detector for
216 | * processing.
217 | */
218 | @Override
219 | public boolean onTouchEvent(MotionEvent event) {
220 | mVelocityTracker.addMovement(event);
221 |
222 | switch (event.getAction()) {
223 | case MotionEvent.ACTION_DOWN:
224 | // We've already stored the initial point,
225 | // but if we got here a child view didn't capture
226 | // the event, so we need to.
227 | return true;
228 | case MotionEvent.ACTION_MOVE:
229 | final float x = event.getX();
230 | final float y = event.getY();
231 | float deltaY = mLastTouchY - y;
232 | float deltaX = mLastTouchX - x;
233 | //Check for slop on direct events
234 | if (!mDragging && (Math.abs(deltaY) > mTouchSlop || Math.abs(deltaX) > mTouchSlop) ) {
235 | mDragging = true;
236 | }
237 | if (mDragging) {
238 | //Scroll the view
239 | scrollBy((int) deltaX, (int) deltaY);
240 | //Update the last touch event
241 | mLastTouchX = x;
242 | mLastTouchY = y;
243 | }
244 | break;
245 | case MotionEvent.ACTION_CANCEL:
246 | mDragging = false;
247 | //Stop any flinging in progress
248 | if (!mScroller.isFinished()) {
249 | mScroller.abortAnimation();
250 | }
251 | break;
252 | case MotionEvent.ACTION_UP:
253 | mDragging = false;
254 | // Compute the current velocity and start a fling if it is above
255 | // the minimum threshold.
256 | mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
257 | int velocityX = (int)mVelocityTracker.getXVelocity();
258 | int velocityY = (int)mVelocityTracker.getYVelocity();
259 | if (Math.abs(velocityX) > mMinimumVelocity || Math.abs(velocityY) > mMinimumVelocity) {
260 | fling(-velocityX, -velocityY);
261 | }
262 | break;
263 | }
264 | return super.onTouchEvent(event);
265 | }
266 | }
267 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 | buildscript {
3 | repositories {
4 | jcenter()
5 | }
6 | dependencies {
7 | classpath 'com.android.tools.build:gradle:2.3.0'
8 | }
9 | }
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/devunwired/custom-touch-examples/14a58187b329f72d2cd26a7224b0a440a0ec7d91/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Sat Mar 25 12:30:33 CET 2017
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/license.txt:
--------------------------------------------------------------------------------
1 | Copyright (c) 2012 Wireless Designs, LLC
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining
4 | a copy of this software and associated documentation files (the
5 | "Software"), to deal in the Software without restriction, including
6 | without limitation the rights to use, copy, modify, merge, publish,
7 | distribute, sublicense, and/or sell copies of the Software, and to
8 | permit persons to whom the Software is furnished to do so, subject to
9 | the following conditions:
10 |
11 | The above copyright notice and this permission notice shall be
12 | included in all copies or substantial portions of the Software.
13 |
14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':TouchExamples'
2 |
--------------------------------------------------------------------------------