Long-press and Scroll Large Images Using Low Level Events
What you will learn: You will learn how to implement long-press and scroll in low level event handlers. Optionally you will learn how to pop a context menu on long-press.
Target readers - this tutorial is for you if:
- You are a game (or other performance-driven) developer looking for low level control.
- You are having trouble getting your long clicks and touch event handlers to coexist.
- You don't want to or can't use GestureDetector for your particular application.
Difficulty: 2 of 5
What it looks like:

Description: This tutorial combines image scrolling with long-press functionality. The image must fit into memory but be larger than the display surface so it is scrollable. Both scroll and long-press are implemented in a low level touch handler, allowing for greater control by the developer.
Background: (For those in a hurry who just need some working code, skip to the Full Source at the end).
For everyone else, the material here builds on my previous tutorial on scrolling large images, so you may wish to look at that first. If you are new to Android and/or event handling I recommend that you read it.
I am going to talk about what I tried first and why it didn't work. Hopefully this will help others avoid similar mistakes.
Failed attempt #1: The first thought that occurred to me was to grab a timestamp in the ACTION_DOWN event, and then get a time delta in the ACTION_UP event. If the time delta was greater than long_press_time, I'd execute my do_long_press code:
Using java Syntax Highlighting
- ACTION_DOWN:
- //...
- downTime = event.getDownTime();
- break;
- ACTION_MOVE:
- //...
- break;
- ACTION_UP:
- //...
- if (event.getEventTime – downTime) > long_press_time {
- //do_long_press stuff
- }
- break;
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
Works great. There's just one problem – the long press code doesn't get executed until the user generates an ACTION_UP, meaning he can hold his finger down until the cows come home and the long press code won't get executed. What I really wanted was a way to get the long press code to execute as soon as one second had passed... which led me to:
Failed attempt #2: Basically I just moved the code in ACTION_UP to ACTION_MOVE, figuring that ACTION_MOVE events are generated often enough that after approximately one second my long press code would execute:
Using java Syntax Highlighting
- ACTION_DOWN:
- //...
- downTime = event.getDownTime();
- break;
- ACTION_MOVE:
- //...
- if (event.getEventTime – downTime) > long_press_time {
- //do_long_press stuff
- }
- else
- //handle move normally
- break;
- ACTION_UP:
- //...
- break;
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
Result? WRONGO! It turns out that for efficiency, if your finger isn't moving no ACTION_MOVE events are generated, and once again it may be a very long time, if ever, before the long press code gets executed.
Finally. Success: I hunted down the source code to GestureDetector.java. In hindsight I probably should have done this first. In that code, I found a real gem. I will lay out the solution here, and explain it shortly.
Declare a message handler:
Using java Syntax Highlighting
- private Handler messageHandler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- switch(msg.what) {
- case LONG_PRESS:
- //handle long press
- do_long_press_stuff();
- break;
- }
- }
- };
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
In the touch event handler, process events as:
Using java Syntax Highlighting
- ACTION_DOWN:
- //...
- //send a message to the handler that will execute after long_press_delay
- //amount of time
- handler.sendMsgAfterADelay(LONG_PRESS, long_press_delay)
- break;
- ACTION_MOVE:
- //...
- //if our move tracking indicates that we are not standing still, cancel
- //the long-press event
- if (moved_some_amount)
- handler.cancelMsg(LONG_PRESS);
- break;
- ACTION_UP:
- //...
- break;
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
(The above is pseudo code; those are not real method names.)
The code above **assumes** a long press until told otherwise by the other event handlers. The benefits of this are (apart from the fact that it works):
- You get to delegate the actual trigger event to the Android OS; the Android OS is a lot more efficient at scheduling events than you will ever be at the user level.
- You only worry about the long press code setup once; you don't have to constantly check it in the ACTION_MOVE handler. The only thing you need to check is to see if you've moved, which you have to do anyway to do your other movement processing.
Again I take no credit for this; the Android Developer Team did all the work. I am just showing it off in the hopes that people can use it, and it's a shame that it's buried in the Android OS sources. If you do decide to look at GestureDetector.java, use the latest one as it's changed a bit. You can download a zip of the 2.1 sources here. This link also explains how to get Eclipse to recognize the sources so you can avoid those pesky 'source not found' pages.
If you need to learn about Android's handlers, you can do so here, but the important thing for us is that we use a handler to set up a timing task. We set up our handler, which the OS knows to associate with our program (actually the OS associates it with the thread in our program that created the handler but for this moment we can just think of it as our program). At some point in our program we send a message to our handler; in our case a message with a delay. The message gets sent immediately to the handler, but the handler does nothing with it until after the delay. The nice thing about handlers is that they work in the background; once you create one you can send it messages that you want to occur at some later point, and it handles all scheduling details for you. The basic handler we'll be using is quite similar to the above.
This handler, and the changes needed to add long-press to the touch event handler, are detailed in steps 5 and 6 of the implementation section.
Implementation:
The full source is available at the end.
The usual caveat applies: for brevity, I'm not handling activity lifecycle so you can make this run out of memory pretty quickly.
I'm using the same activity name as in the previous tutorial, since we'll be building on that material. Steps 0 and 1 are identical, and are repeated here for convenience. Regarding the code: I'll explain the long-press additions as we go; a complete explanation of the rest of the code is at the link above.
0.) In Eclipse, create a new Android Project, targeting Android 2.0 (older versions may work too, but the folders may be slightly different from those shown here). For consistency with this tutorial you may wish to name your main activity LargeImageScroller, and make your package name com.example.largeimagescroller.
1.) Obtain an image resource:
The image resource I used is 1440x1080 - tested with the Droid handset - but you can use a smaller one if you want; it just has to be larger than the display size so you can scroll it. Be careful if you go too much larger, as you may run out of memory. If you are using a different device your memory requirements may vary, and you may need to adjust the size of the image accordingly.
Add the image resource (I've named mine testlargeimg.png) to your /drawable-hdpi folder (may also be named /drawable depending on which Android version you are using).
2.) For convenience, we will run our simple application fullscreen and in landscape mode. To do this modify the project's manifest:
Edit AndroidManifest.xml and add the following to the application tag:
Using xml Syntax Highlighting
- android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
Parsed in 0.000 seconds, using GeSHi 1.0.8.4
While we are here you can also add the following if you wish to debug on an actual device (this also goes inside the application tag):
Using xml Syntax Highlighting
- android:debuggable="true"
Parsed in 0.000 seconds, using GeSHi 1.0.8.4
In the activity tag, set the screen orientation to landscape:
Using xml Syntax Highlighting
- android:screenOrientation="landscape"
Parsed in 0.000 seconds, using GeSHi 1.0.8.4
We must also add the proper permission to use the vibration service. In the manifest tag (above the application tag) add:
Using xml Syntax Highlighting
- <uses-permission android:name="android.permission.VIBRATE" />
Parsed in 0.000 seconds, using GeSHi 1.0.8.4
3.) In onCreate() add a reference to the vibrator service:
Using java Syntax Highlighting
- vibrator = ((Vibrator)getSystemService(Context.VIBRATOR_SERVICE));
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
and declare it at the top of our activity:
Using java Syntax Highlighting
- private static Vibrator vibrator = null;
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
4.) At the top of the SampleView class add:
Using java Syntax Highlighting
- // Get static method data via static access to ViewConfiguration.
- private static final long tapTime = ViewConfiguration.getTapTimeout();
- private static final long longPressTime = ViewConfiguration.getLongPressTimeout();
- private static int scaledTouchSlopSquared = 0;
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
ViewConfiguration allows us to get device-specific timeouts, slop regions, etc. There are two ways to access ViewConfiguration. The first is to access it directly (statically) as was done here to get our device-specific timeout information. A long press timeout really consists of two parts, the amount of time taken by a tap (100ms on my Droid) and the actual long press amount (500ms on my Droid).
ViewConfiguration was changed recently from static access only; the newer implementation allows access to a new .getScaledXXX() API which is much more friendly to multiple device development. This is accessed by requesting a ViewConfiguration – as we do in our SampleView constructor:
Using java Syntax Highlighting
- // Get non-static method data from ViewConfiguration.
- ViewConfiguration viewConfiguration = ViewConfiguration.get(context);
- scaledTouchSlopSquared = viewConfiguration.getScaledTouchSlop()
- * viewConfiguration.getScaledTouchSlop();
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
We need to use both static and non-static access to ViewConfiguration: the older APIs, e.g., the timeout APIs are not available via non-static access, probably for compatibility reasons. scaledTouchSlopSquared will be explained in step 6.
Slop is the threshold for which a tap is no longer considered a tap, but a move. I.e., if you move out of the slop threshold during a long press, it is no longer a long press.
5.) Add a message handler to the SampleView class:
Using java Syntax Highlighting
- private Handler messageHandler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- switch (msg.what) {
- // Schedule a long press. This will be canceled if:
- // * gesture finishes (ACTION_UP)
- // * gesture is canceled (ACTION_CANCEL)
- // * we move outside of our 'slop' range
- case MSG_LONG_PRESS:
- handleLongPress();
- break;
- default:
- throw new RuntimeException("handleMessage: unknown message " + msg);
- }
- }
- };
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
The concept of a handler was explained earlier. When a MSG_LONG_PRESS is received by the handler, it will 'hang onto' it for the duration specified in the sender's request. This type of request can be initiated by .sendEmptyMessageAtTime() or .sendEmptyMessageDelayed(), or we can have it execute immediately by issuing a .sendEmptyMessage(). Since we don't have any data to send, we use the .sendEmptyXXX() form of these APIs. There are other .sendXXX() APIs including many for when you do have data to send. (There's a completely different alternative using runnables, which are in many ways analogous to what we are doing here, using the .postXXX() APIs. More information on runnables can be found in the Android docs.)
Add our message to the top of the SampleView class:
Using java Syntax Highlighting
- private static final int MSG_LONG_PRESS = 1;
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
Add a test stub for handling our long press code: this stub simply gives us a short vibration when it's executed. This stub can go anywhere in the SampleView class:
Using java Syntax Highlighting
- void handleLongPress() {
- // Indicate that a long-press has fired.
- isLongPress = true;
- // Execute your long press code here.
- vibrator.vibrate(50);
- }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
isLongPress is used for making sure only one long-press per gesture is handled. Without isLongPress you can get long-presses 'stacking up' on each other, resulting in multiple back-to-back executions of the long press code; this is probably unintended behavior. We'll discuss isLongPress further shortly.
6.) This is the heart of our implementation: touch event handling. We'll replace the onTouchEvent() handler one action at a time.
ACTION_DOWN:
Using java Syntax Highlighting
- case MotionEvent.ACTION_DOWN:
- // Remember our initial down event location.
- startX = downX = event.getRawX();
- startY = downY = event.getRawY();
- // We will set this once a long-press actually fires.
- isLongPress = false;
- // Assume this is a long-press, it will be canceled either
- // by moving or by canceling the gesture.
- messageHandler.removeMessages(MSG_LONG_PRESS);
- messageHandler.sendEmptyMessageAtTime(MSG_LONG_PRESS,
- event.getDownTime() + tapTime + longPressTime);
- break;
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
startX and startY allow us to track how far our finger has traveled, while downX and downY will be used to determine if our finger has moved out of the slop threshold.
isLongPress indicates that we are not yet executing any long-press code.
As explained earlier we use one of the .sendEmptyXXX() APIs to send our message to our handler. We are just sending a simple message, with no data attached. For our purposes we want the handler to execute our code after a delay appropriate to a long press, so we use .sendEmptyMessageAtTime(), and we calculate our time based on the moment the event occurred (event.getDownTime()) plus the total time for a long press (which includes the time it takes to tap).
It's good practice to make sure you aren't 'doubling up' messages you've already sent to the handler, so you should almost always call .removeMessages() before (re)adding your message. (I am simplifying the 'message gets sent to the handler' process a bit. What is really going on is that the handler is adding your message(s) to an internal message queue; the OS knows how to associate the message queue, the handler, and your program. The handler just acts as a transfer station.)
ACTION_MOVE:
Using java Syntax Highlighting
- case MotionEvent.ACTION_MOVE:
- // A long-press has fired and is in progress. Don't bother with any
- // other processing. A side-effect of this is that we won't be able
- // to do any other movement until we either cancel (ACTION_CANCEL)
- // or finish (ACTION_UP) the current movement.
- if (isLongPress)
- break;
- final float x = event.getRawX();
- final float y = event.getRawY();
- scrollByX = x - startX; // calculate move increments
- scrollByY = y - startY;
- if (hasNotMoved) {
- // Have we moved out of the threshold radius of initial
- // user touch?
- final int deltaXFromDown = (int)(x - downX);
- final int deltaYFromDown = (int)(y - downY);
- int distance = (deltaXFromDown * deltaXFromDown)
- + (deltaYFromDown * deltaYFromDown);
- if (distance > scaledTouchSlopSquared) {
- // We've moved so handle scroll update and cancel
- // long-press.
- hasNotMoved = false;
- messageHandler.removeMessages(MSG_LONG_PRESS);
- startX = x; // reset previous values to latest
- startY = y;
- invalidate();
- }
- } else {
- // This is a move gesture so update our move incrementers
- // for next pass through ACTION_MOVE, and force a redraw with
- // the updated scroll values. We don't need to call
- // .removeMessages() since the only way here is via the if
- // block above which calls it for us.
- startX = x; // reset previous values to latest
- startY = y;
- invalidate();
- }
- break;
Parsed in 0.040 seconds, using GeSHi 1.0.8.4
First we check if we are already executing the code associated with a long press – we only want one of those per gesture. (You can change this behavior by judicious use of the isLongPressed Boolean, but it's more useful for real world applications to call a long press a complete gesture. One consequence of this is that a user must lift his finger in order to continue gesturing: another gesture wont be possible until an up (or cancel) event is generated).
hasNotMoved tells us whether we have moved out of our slop region. Our slop region is a circle around our touch point. If our deltas indicate that we've moved out of slop range we cancel our long press with .removeMessages(), reset startX and startY so they can be used the next time we calculate our move increments, and reset hasNotMoved to indicate that, yes, we have moved. invalidate() tells the OS to redraw our view. The view will be redrawn with our new move values.
Aside: you may be wondering about the distance check calculation and why we are using scaledTouchSlopSquared. Normally a distance (or radius in this case) is calculated using sqrt(). However, sqrt() is a very expensive operation; we don't want to do this in our event handler (eliminating floating point math from handlers, and sqrt()s in particular, is a very common practice in high performance code). Since we only care about comparing apples to apples, we can make both distances (the calculated distance, and the threshold distance we are testing against) based on the 'squared' approach used here. All we care about is that we have a round area centered at our touch point to test against.)
The explanation above is for the case where we are transitioning from a still to a moving state. If instead we enter our move handler already moving (hasNotMoved is false on entry), we simply update our move incrementers and force a redraw with the new values.
(Advanced readers: If you are looking at GestureDetector.java, you may notice at this point that there is an optimization to only update the movement incrementers if the move deltas are not near zero. The older Android implementations send continuous ACTION_MOVE updates even if no move changes are detected, so I am guessing this was an important optimization for those revs. Since the post-Cupcake revs (I think it was after Cupcake) issue ACTION_MOVE events only when there is motion, this optimization seems much less important. (If you are interested I have a test case showing how infrequently the 'near zero but not zero' cases now are, just PM me). Given that the optimization uses floating point math checks and adds additional complexity to the handler for a now negligible return I've opted to remove it completely. Again this applies only to later releases of Android – I am guessing that check was left in the new version for backward compatibility. If I've completely misrepresented this, feel free to correct me.)
ACTION_UP and ACTION_CANCEL:
Using java Syntax Highlighting
- case MotionEvent.ACTION_UP:
- isLongPress = false;
- hasNotMoved = true;
- messageHandler.removeMessages(MSG_LONG_PRESS);
- break;
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
We simply reset to our startup configuration indicating a touch gesture of any kind is not in progress, and make sure no long press messages are waiting to be processed.
The MotioneEvent.ACTION_CANCEL handler is identical.
7.) Optional - Context Menus: I showed a draft version of this tutorial to a friend and his first comment was "Cool. Can I pop a context menu with it?" So, since I know the question is going to come up:
Grab the full source listing at the end.
Change handleLongPress() to:
Using java Syntax Highlighting
- void handleLongPress() {
- isLongPress = true;
- this.performLongClick();
- }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
The magic method here is .performLongClick() and it does exactly what you think it does: among other things if you have a context menu or a long click handler registered with your view (registerForContextMenu()/onCreateContextMenu() and setOnLongClickListener()/onLongClick(), respectively) when the MSG_LONG_PRESS event fires and handleLongPress() gets called those registered methods will be called as well.
The rest is the standard context menu creation process:
In onCreate() change:
Using java Syntax Highlighting
- setContentView(new SampleView(this));
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
to:
Using java Syntax Highlighting
- SampleView sv = new SampleView(this);
- registerForContextMenu(sv);
- setContentView(sv);
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
Add an onCreateContextMenu() to your activity (below onCreate() is a good place) :
Using java Syntax Highlighting
- public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
- super.onCreateContextMenu(menu, v, menuInfo);
- menu.add(Menu.NONE, 0, Menu.NONE, "Item1");
- menu.add(Menu.NONE, 1, Menu.NONE, "Item2");
- }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
You'll need to add the appropriate imports and you can also remove any vibrator service references, imports and permissions, since vibration will now occur automatically.
8.) There are no other major changes. In particular onDraw() remains identical. To build and run, you will need to add a few variable declarations; see the full source at the end. When you run the example you should be able to smoothly scroll around your image, and long press to generate a vibration, or pop a context menu (if you've implemented step 7).
Takeaways:
- Make sure GestureDetector doesn't fit your needs before attempting to roll your own.
- Use handlers to set up event triggers; let the Android OS scheduler do the work for you.
- Understand that ACTION_MOVE events are not generated if you don't move. This is different from some older Android revisions.
- To avoid poor performance, try to keep the code that executes from within the event handlers to a minimum. (Yes, this is a repeat from last time.)
You'll need a single large image as described in step 1, recommended size is 1440x1080, though if your device is other than a Droid, your mileage may vary.
You will also need to edit your AndroidManifest.xml as described in step 2.
"/src/your_package_structure/LargeImageScroller.java"
(some code comments from the previous tutorial have been omitted for brevity, but you should still be able to use a diff utility to easily compare the two, if so desired).
Using java Syntax Highlighting
- package com.example.largeimagescroller;
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.os.Vibrator;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.graphics.Paint;
- import android.graphics.Rect;
- import android.view.Display;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.ViewConfiguration;
- import android.view.WindowManager;
- public class LargeImageScroller extends Activity {
- // Physical display width and height.
- private static int displayWidth = 0;
- private static int displayHeight = 0;
- private static Vibrator vibrator = null;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // displayWidth and displayHeight will change depending on screen
- // orientation. To get these dynamically, we should hook onSizeChanged().
- // This simple example uses only landscape mode, so it's ok to get them
- // once on startup and use those values throughout.
- Display display = ((WindowManager)
- getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
- displayWidth = display.getWidth();
- displayHeight = display.getHeight();
- vibrator = ((Vibrator) getSystemService(Context.VIBRATOR_SERVICE));
- setContentView(new SampleView(this));
- }
- private static class SampleView extends View {
- private static Bitmap bmLargeImage = null; // bitmap large enough to scroll
- private static Rect displayRect = null; // rect we display to
- private Rect scrollRect = null; // rect we scroll over our bitmap with
- private int scrollRectX = 0; // current left location of scroll rect
- private int scrollRectY = 0; // current top location of scroll rect
- private float scrollByX = 0; // x amount to scroll by
- private float scrollByY = 0; // y amount to scroll by
- private float startX = 0; // track x from one ACTION_MOVE to the next
- private float startY = 0; // track y from one ACTION_MOVE to the next
- private float downX = 0; // x cached at ACTION_DOWN
- private float downY = 0; // y cached at ACTION_DOWN
- private boolean isLongPress = false; // only want one long press per gesture
- private boolean hasNotMoved = true; // long-press determination
- // Get static method data via static access to ViewConfiguration.
- private static final long tapTime = ViewConfiguration.getTapTimeout();
- private static final long longPressTime =
- ViewConfiguration.getLongPressTimeout();
- private static int scaledTouchSlopSquared = 0;
- private static final int MSG_LONG_PRESS = 1;
- public SampleView(Context context) {
- super(context);
- displayRect = new Rect(0, 0, displayWidth, displayHeight);
- scrollRect = new Rect(0, 0, displayWidth, displayHeight);
- bmLargeImage = BitmapFactory.decodeResource(getResources(),
- R.drawable.testlargeimg);
- // Get non-static method data from ViewConfiguration.
- ViewConfiguration viewConfiguration = ViewConfiguration.get(context);
- scaledTouchSlopSquared = viewConfiguration.getScaledTouchSlop()
- * viewConfiguration.getScaledTouchSlop();
- }
- private Handler messageHandler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- switch (msg.what) {
- // Schedule a long press. This will be canceled if:
- // * gesture finishes (ACTION_UP)
- // * gesture is canceled (ACTION_CANCEL)
- // * we move outside of our 'slop' range
- case MSG_LONG_PRESS:
- handleLongPress();
- break;
- default:
- throw new RuntimeException("handleMessage: unknown message "
- + msg);
- }
- }
- };
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN:
- // Remember our initial down event location.
- startX = downX = event.getRawX();
- startY = downY = event.getRawY();
- // We will set this once a long-press actually fires.
- isLongPress = false;
- // Assume this is a long-press, it will be canceled either
- // by moving or by canceling the gesture.
- messageHandler.removeMessages(MSG_LONG_PRESS);
- messageHandler.sendEmptyMessageAtTime(MSG_LONG_PRESS,
- event.getDownTime() + tapTime + longPressTime);
- break;
- case MotionEvent.ACTION_MOVE:
- // A long-press has fired and is in progress. Don't bother with
- // any other processing. A side-effect of this is that we won't
- // be able to do any other movement until we either cancel
- // (ACTION_CANCEL) or finish (ACTION_UP) the current movement.
- if (isLongPress)
- break;
- final float x = event.getRawX();
- final float y = event.getRawY();
- scrollByX = x - startX; // calculate move increments
- scrollByY = y - startY;
- if (hasNotMoved) {
- // Have we moved out of the threshold radius of initial
- // user touch?
- final int deltaXFromDown = (int)(x - downX);
- final int deltaYFromDown = (int)(y - downY);
- int distance = (deltaXFromDown * deltaXFromDown)
- + (deltaYFromDown * deltaYFromDown);
- if (distance > scaledTouchSlopSquared) {
- // We've moved so handle scroll update and cancel
- // long-press.
- hasNotMoved = false;
- messageHandler.removeMessages(MSG_LONG_PRESS);
- startX = x; // reset previous values to latest
- startY = y;
- invalidate();
- }
- } else {
- // This is a move gesture so update our move incrementers
- // for next pass through ACTION_MOVE, and force a redraw
- // with the updated scroll values. We don't need to call
- // .removeMessages() since the only way here is via the if
- // block above which calls it for us.
- startX = x; // reset previous values to latest
- startY = y;
- invalidate();
- }
- break;
- case MotionEvent.ACTION_UP:
- isLongPress = false;
- hasNotMoved = true;
- messageHandler.removeMessages(MSG_LONG_PRESS);
- break;
- case MotionEvent.ACTION_CANCEL:
- isLongPress = false;
- hasNotMoved = true;
- messageHandler.removeMessages(MSG_LONG_PRESS);
- break;
- }
- return true; // done with this event so consume it
- }
- void handleLongPress() {
- // Indicate that a long-press has fired.
- isLongPress = true;
- // Execute your long press code here.
- vibrator.vibrate(50);
- }
- @Override
- protected void onDraw(Canvas canvas) {
- // Our move updates are calculated in ACTION_MOVE in the opposite direction
- // from how we want to move the scroll rect. Think of this as dragging to
- // the left being the same as sliding the scroll rect to the right.
- int newScrollRectX = scrollRectX - (int)scrollByX;
- int newScrollRectY = scrollRectY - (int)scrollByY;
- // Don't scroll off the left or right edges of the bitmap.
- if (newScrollRectX < 0)
- newScrollRectX = 0;
- else if (newScrollRectX > (bmLargeImage.getWidth() - displayWidth))
- newScrollRectX = (bmLargeImage.getWidth() - displayWidth);
- // Don't scroll off the top or bottom edges of the bitmap.
- if (newScrollRectY < 0)
- newScrollRectY = 0;
- else if (newScrollRectY > (bmLargeImage.getHeight() - displayHeight))
- newScrollRectY = (bmLargeImage.getHeight() - displayHeight);
- scrollRect.set(newScrollRectX, newScrollRectY, newScrollRectX
- + displayWidth, newScrollRectY + displayHeight);
- Paint paint = new Paint();
- canvas.drawBitmap(bmLargeImage, scrollRect, displayRect, paint);
- scrollRectX = newScrollRectX; // reset previous to latest
- scrollRectY = newScrollRectY;
- }
- }
- }
Parsed in 0.065 seconds, using GeSHi 1.0.8.4


