How can i integrate GestureDetector with onItemLongClick?
I have a GridView containing three images. When I touch on the first image, I want to display a Toast message. When I lift up my finger from the screen, I want to display a second Toast message.
I know that GestureDetector uses MotionEvent, but onItemLongClick does not. But in this case, I would need to keep track of the image's position ID in the grid, thus it is not possible to implement inside onTouch()?
You can track the motion of the cursor, whether its a onscreen touch or a TrackBall move, using this class and if it crosses over into the next picture you can handle that event then. Here is an example taken from the sdk examples:
#Override public boolean onTouchEvent(MotionEvent event) {
int action = event.getActionMasked();
if (action != MotionEvent.ACTION_UP && action != MotionEvent.ACTION_CANCEL) {
int N = event.getHistorySize();
int P = event.getPointerCount();
for (int i = 0; i < N; i++) {
for (int j = 0; j < P; j++) {
mCurX = event.getHistoricalX(j, i);
mCurY = event.getHistoricalY(j, i);
drawPoint(mCurX, mCurY,
event.getHistoricalPressure(j, i),
event.getHistoricalTouchMajor(j, i));
}
}
for (int j = 0; j < P; j++) {
mCurX = event.getX(j);
mCurY = event.getY(j);
drawPoint(mCurX, mCurY, event.getPressure(j), event.getTouchMajor(j));
}
}
return true;
}
You can read more and see the file in your SDK at C:\YourInstallDir\android-sdk\samples\android-10\ApiDemos\src\com\example\android\apis\graphics\TouchPaint.java or just search the whole examples files for MotionEvent there's a few more uses in them.
Related
Code from this book: Programming Android, 2nd Edition.
At first I touch the screen with two fingers - everything OK.
Then touch the screen with one finger - java.lang.IllegalArgumentException: pointerIndex out of range
public boolean onTouch(View v, MotionEvent event) {
int action = event.getActionMasked();
int idx;
int n;
switch (action) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN:
idx = MotionEventCompat.getActionIndex(event);
tracks.add(event.getPointerId(idx));
break;
case MotionEvent.ACTION_POINTER_UP:
idx = MotionEventCompat.getActionIndex(event);
tracks.remove(event.getPointerId(idx));
break;
case MotionEvent.ACTION_MOVE:
n = event.getHistorySize();
for (Integer i : tracks) {
idx = event.findPointerIndex(i.intValue());
for (int j = 0; j < n; j++) {
addDot(
dots,
event.getHistoricalX(idx, j),
event.getHistoricalY(idx, j),
event.getHistoricalPressure(idx, j),
event.getHistoricalSize(idx, j)
);
}
}
break;
default:
return false;
}
for (Integer i: tracks){
idx = event.findPointerIndex(i.intValue());
addDot(
dots,
event.getX(idx),
event.getY(idx),
event.getPressure(idx),
event.getSize(idx)
);
}
return true;
}
The method findPointerIndex() may return -1. From what I have experienced this will happen when you touch with 2 fingers (or more) and let go of the first pressed finger.
I think this is the cause of 'pointerIndex out of range' error - since -1 is not a valid index :-)
For ACTION_MOVE I would recommend using a loop with getPointerCount(), ie looping from 0 to getPointerCount() - 1.
Using this method you can get touch position with getX(finger) and getY(finger).
Where 'finger' is your loop count.
Using this method will not keep track of what ID your finger has, ie if you press with two fingers and let go the first finger (ID=0) then the second finger will continue to count as the first finger (which is now not pressed)
Hope this helps!
/Richard
Hi I'm developing a game in android, for that I have draw 8*8 rectangles using canvas.drawRect() method, now what I need is when user touches on any of the rect, its color has to change. For this I have done as follows.
public boolean onTouchEvent(final MotionEvent event) {
handleTouches(event.getX(), event.getY());
return false;
}
public void handleTouches(float x, float y) {
xLocTouch = (int) x;
yLocTouched = (int) y;
outerLoop: for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (GameView.tiles[i][j].rect.contains(xLocTouch, yLocTouched)) {
touched = true;
xTouched = i;
yTouched = j;
break outerLoop;
}
}
}
}
protected void onDraw(Canvas canvas) {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
canvas.drawRect(tiles[i][j].rect, paint);
canvas.drawRect(tiles[i][j].rect, p);
if(touched && i==xTouched && j == yTouched) {
Paint touchedColor = new Paint();
touchedColor.setColor(Color.BLUE);
canvas.drawRect(tiles[i][j].rect, touchedColor);
}
}
}
This code is works properly, but the problem is when I touch first time that rect's color changes, but for the second touch it erases old touches position. I need to keep all the touched rect as different color. Is there any way for this?
A Boolean array that tracks the touch state of each rectangle would do the trick. I can't see the external code, but this could be an additional field in your GameView class, and updated appropriately in your handleTouches method.
A perhaps less efficient, less elegant solution would be to just not set the color of the rectangle in onDraw if it's already Color.BLUE (modify your if statement appropriately). You can get the color of the touched pixel using this SE answer, but be forewarned you have to first turn the canvas into a bitmap in order to sample the color (hence the inefficiency).
I am working on a multitouch program that needs to record only the movements made by the second finger or index pointer.
Now the documentation says that we can use MotionEvent.ACTION_POINTER_INDEX_MASK and & it with action and shift by INDEX_SHIFT to get the pointer that made the action like going up or down. But this technique does not work on move.
Is there anyway that we can detect the move action made by a certain pointer alone?
Thx,
yes, you can have something like this in your View class:
#Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_MOVE:
if(event.getPointerCount()>1){
//where 1 is the index of the second finger
final int Y = event.getY(1);
final int X = event.getX(1);
}
break;
}
}
so depending on what finger you want to get the movent you can set the get to that index. Rember that values may be from 0 (the first pointer that is down) to getPointerCount()-1.
I tested this on 2.2 Gingerbread so I hope it be useful for you :)
You can get the effective pointer index by checking which pointer changed:
private final int MAX_POINTER = 5; // 5 different touch pointers supported on most devices
private float mLastTouchPositionX[];
private float mLastTouchPositionY[];
#Override
public boolean onTouchEvent(MotionEvent aEvent)
int tActionIndex = aEvent.getActionIndex();
int tPointerCount = aEvent.getPointerCount();
/*
* Check which pointer changed on move
*/
if (tMaskedAction == MotionEvent.ACTION_MOVE) {
for (int i = 0; i < tPointerCount && i < MAX_POINTER; i++) {
if (mLastTouchPositionX[i] != aEvent.getX(i) || mLastTouchPositionY[i] != aEvent.getY(i)) {
mLastTouchPositionX[i] = aEvent.getX(i);
mLastTouchPositionY[i] = aEvent.getY(i);
// Found new action index
tActionIndex = i;
break;
}
}
}
...
}
The below code is my attempt to send mMyView to the front or the back of the set of children of mPivotParent so it will be rendered on top or behind the others. Hiding the view will not suffice in my case.
mPivotParent is a FrameLayout.
Looking at mPivotParent.mChildren shows that my code below "works" in that the ordering is being set correctly. Yet it has no impact on the z order. Not only this, but the framerate gets cumulatively slower and slower the more times the repositioning code gets called. There are 4 children total and mPivotParent.mChildrenCount remains at 4 throughout as expected.
I'm targeting API Level 7.
#Override
public boolean onTouchEvent(MotionEvent event) {
Display display = getWindowManager().getDefaultDisplay();
float x = event.getRawX();
float sWidth = (int)display.getWidth();
float xLerpFromCenter = ((x / sWidth) - .5f) * 2.f; // [-1, 1]
mRotateAnimation.mfLerp = xLerpFromCenter;
if(xLerpFromCenter < -0.2f && mPivotParent.getChildAt(0) != mMyView)
{
mPivotParent.removeView(mMyView);
mPivotParent.addView(mMyView, 0);
refreshEverything();
}
else if(xLerpFromCenter > 0.2f && mPivotParent.getChildAt(0) == mMyView)
{
mPivotParent.removeView(mMyView);
mPivotParent.addView(mMyView, mPivotParent.getChildCount() - 1);
refreshEverything();
}
return super.onTouchEvent(event);
}
private void refreshEverything()
{
for(int i = 0; i < mPivotParent.getChildCount(); ++i)
{
mPivotParent.getChildAt(i).invalidate();
mPivotParent.getChildAt(i).requestLayout();
}
mPivotParent.invalidate();
mPivotParent.requestLayout();
}
Partial Solution
Here's a somewhat inefficient hack but it works for my purpose, which is to take the top item and send it to the back, keeping all other items in their same order.
private void putFrontAtBack()
{
for(int i = 0; i < mPivotParent.getChildCount() - 1; ++i)
{
mPivotParent.getChildAt(0).bringToFront();
}
refreshEverything();
}
Note: This doesn't work in the general case of arbitrary re-ordering.
Try this.
private void reorder(int[] order)
{
if(order == null || order.length != mPivotParent.getChildCount()) return;
for(int i = order.length - 1; i >= 0; i--)
{
mPivotParent.getChildAt(order[i]).bringToFront();
}
refreshEverything();
}
This code provides arbitrary reordering. The integer array "order" is used to indicate the new order of each view, where order[n]=x means the new order of childAt(x) is n.
I have displayed images from resource in my application as rows and columns randomly.
From those rows and columns i would like to swap the two images when user click on beside of images only.The following code will display the images in rows and columns as randomly.
private void rand(int imagesList[][])
{
Random generator = new Random();
int temp;
for (int i = 0; i < MAX_ROWS; i++)
for(int j = 0; j < MAX_COLS; j++)
{
int randRowPos = generator.nextInt(MAX_ROWS);
int randColPos = generator.nextInt(MAX_COLS);
temp = imagesList[i][j];
imagesList[i][j] = imagesList[randRowPos][randColPos];
imagesList[randRowPos][randColPos]= temp;
}
}
by using the above code i have displayed images as rows and columns.
Here how can i swap the two beside images from rows and columns?
please any body help me.....
I don't have privilege to add comment, so I am posting this as answer.
What do you mean by beside images ?
Is it when user will click on one image , it should get swapped with the image next to it ?
Can you also share the code where you have binned these images to view or any adapterview ?
EDIT :
I too had similar situation at the times when absolute layouts were alive.
What I had done is as follows:
Class:
public class PlayScreen extends Activity implements OnTouchListener
private Panel mainPanel; // Panel for out display
boolean firstClick = false;
OnCreate :
main = new Panel(this);
// Display the panel (calls the ondraw and updates the display)
setContentView(main,new ViewGroup.LayoutParams(screenwidth,screenheight));
// Listen for touchevents on the panel
main.setOnTouchListener(this);
Panel :
class Panel extends View
{
/*
* Init a Panel to draw on and a paint to paint with
*/
Paint mBitmapPaint;
public Panel(Context context)
{
super(context);
mBitmapPaint = new Paint();
}
#Override
protected void onDraw(Canvas canvas)
{
drawImages(canvas);
}
}
drawImages :
private void drawImages(Canvas canvas)
{
for(int i = 0; i<MAX_ROWS; i++){
for(int j=0; j<MAX_COLS; j++)
{
int xpos = j*bmp.getWidth()+j*2;
int ypos = i*bmp.getHeight()+i*2;
bmp = BitmapFactory.decodeResource(mContext.getResources(), items[i][j],opts);
canvas.drawBitmap(bmp,xpos,ypos,mBitmapPaint);
clickzonex.add(xpos);
clickzoney.add(ypos);
clickzonei.add(i);
clickZonej.add(j);
}
}
}
OnTouch:
onTouch(View v, MotionEvent event) :
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
// imply logic
x = (int) event.getX();
y = (int) event.getY();
for(int i = 0; i < clickzonex.size();i++)
{
if((x>clickzonex[i]) && (x<(clickzonex[i]+ bmp.getwidth())) && (y>(clickzoney[i])) && (y<(clickzoney[i]+bmp.getHeight())))
{
// we have a click in a zone so we get the card-number in that zone
if(firstClick == false)
{
itemAti=clickzonei[i];
itemAtj = clickzonej[i];
firstclick = false;
}
else
{
FirstItemToSwap = items[clickzonei[i]][clickzonej[i]];
SecondItemToSwap = items[itemAti][itemAtj];
// Now do the swaping using any algo you like.
main.postInvalidate();
firstclick = true;
}
break;
}
}
return true;
}
else
{
return false;
}
I have just tried to show you the logic using my own example and mixing it with your code. The main point is that in ondraw method just call drawcanvas and on touch just swap the items[][] and call postinvalidate method of Panel class.
I had to do something like this once. I just swapped the image references in the array and did a redraw(invalidate()) on the whole thing.
void swap(int x1, int y1, int x2, int y2) {
// swap items[x1][y1] and items[x2][y2]
........
invalidate();
}
Not quite sure what you are actually asking here, so please try to clarify the question.
One approach could be to use a ViewAnimator as a parent for each of the drawable ImageViews.
<ViewAnimator
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/slideshow_animator"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
Then once you capture the event that should trigger the swap, you can use the ViewAnimator to swap the View (in your case ImageView) it uses. You can even easily add an animation effect
http://developer.android.com/reference/android/widget/ViewAnimator.html