Android: How do I move cursor on touch? - android

Here is my scenerio. Wherever you touch my screen, an circle is drawn, and when you lift your finger/stylus of the screen, the circle disappears. This is fine, but the problem now is if you touch, then move, the circle will not update to the new position of your finger/stylus. It will just stay at the original touch position regardless of movement until you lift your finger/stylus off the screen.
How am I able to get the circle(cursor) to redraw or move itself to the new position of your finger/stylus? Here is my code:
rLO.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
cursor.x = (int)event.getX() - (cursor.radius / 2);
cursor.y = (int)event.getY() - (cursor.radius / 2);
cursor.onDraw(cursor.e);
rLO.addView(cursor);
break;
case MotionEvent.ACTION_UP:
rLO.removeView(cursor);
break;
case MotionEvent.ACTION_MOVE:
}// end switch
return true;
}
});
Thanks in advance!

Related

how to use multi touch in Motion event?

This is what I want to achieve:
In the below image there is a white dot on the screen and i want to move the white dot to the RIGHT when right part of the screen is touched and to the LEFT when left part of the screen is touched.
This is my code:
protected void onDraw(Canvas canvas)
{
final Bitmap mball = ball;
if (pressed){
if (touch_x>canvas.getWidth()/2)
left += 2;
else left -= 2;
}
canvas.drawBitmap(mball, left, canvas.getHeight()*0.7f, null);
invalidate();
}
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
final int action = event.getAction();
int pointerIndex;
switch (action ){
case MotionEvent.ACTION_DOWN:
touch_x = event.getX();
pressed = true;
break;
case MotionEvent.ACTION_POINTER_DOWN:
touch_x = event.getX(event.getActionIndex());
pressed = true;
break;
case MotionEvent.ACTION_UP:
mActivePointerId = -1;
pressed = false;
break;
case MotionEvent.ACTION_CANCEL: {
mActivePointerId = -1;
break;
}
}
return pressed;
}
It is not working the way I want it to work. When the user touches the right part the white ball should move towards right and if LEFT part is touched without lifting off the RIGHT finger the ball should move towards left and vice-versa.
What is happening is if RIGHT part of screen is touched ball keeps moving towards right and when left part is touched without removing the right finger the ball continues to move towards right.
I have also tried using pointer Index for primary and non-primary pointers but still no luck.
Any help would be appreciated.

How to draw a 5*5 array and move images on it

I have a project which i'm required to develop Android game that display a 5*5 table and and image for each player which each one of them can move the image in place inside the 5*5 table.
ex:
Note : I need to know the exact coordinates (or anything relative) so i can save the position in array and move the image to that new place (eg : re-draw it on the new position).
Any ideas ?
Thanks in advance.
Just use a RelativeLayout with 5x5 ImageViews (set to not visible).
And on the images that the user can move to the place, use Drag and Drop.
In onDrag you set the visibility of the image to Gone.
For all ImageView (5x5 Table), you set the onDragListener.
After that, in the overwritten method OnDrop, you can receive the view that is dropped and can determine which drawable to show.
edit:
Oh well, in this case I would use GridView as said in the comments. And make usage of drag and drop. You don't need to attach a DragListener to every image then. You can simply let the GridView listen to the drop events and determine by the x and y where to drop.
Little example (just as a hint)
gameStoneView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
DragShadowBuilder shadowBuilder = new DragShadowBuilder(gameStoneView);
gameStoneView.startDrag(null, shadowBuilder, gameStoneView, 0);
if (dragListener != null) {
dragListener.onDragStart();
}
break;
default:
LOG.v("Motion event reorderIcon: DEFAULT - not action down");
}
return true;
}
});
gridView.setOnDragListener(new OnDragListener() {
#Override
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_LOCATION:
currentY = event.getY();
currentX = event.getX();
break;
case DragEvent.ACTION_DROP:
final Point position = getPositionOfItem(currentX, currentY);
dropItemAt(position);
break;
case DragEvent.ACTION_DRAG_EXITED:
//BORDER DRAGEVENT: ACTION_DRAG_EXITED
viewDraggedOutSide = true;
break;
case DragEvent.ACTION_DRAG_ENDED:
//BORDER DRAGEVENT: ACTION_DRAG_ENDED
if (viewDraggedOutSideList) {
reinsertDraggedItem();
update();
}
viewDraggedOutSideList = false;
return true;
default:
return true;
}
}
});

How to change the minimum swipe distance in Android Gallery?

I have a class that extends Gallery called MyGallery and I have a set of bitmaps to show in a MyGallery widget.
In order to swipe only one item per gesture, I had to override the onFling() so it won't call the super.onFling(). The problem is that in order to change item, I have to swipe more than 50% of the screen size and it's bothering me. I did a search but I didn't find any ways to change this minimum limit. Is there any way to do this?
You can use the MotionEvent object to handle the minimum and maximum Swipe distances.
Calculate the swipe distance using the MotionEvent object i.e.., event.
#Override
public boolean onTouchEvent(MotionEvent event)
{
// TODO Auto-generated method stub
float rawX = 0.0f, dist;
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
rawX = event.getX();
break;
case MotionEvent.ACTION_UP:
dist = event.getX() - rawX;
break;
default:
break;
}
return super.onTouchEvent(event);
}

Allowing rectangle to be clickable - android

I wrote a View with canvas, which contains many rectangles. I want these rectangles to be used as a button which will open a new activity. How can I do it?
You need to be careful with Suri Sahani example, onTouchEvent is called on any action qualified as a touch event, meaning press, release, movement gesture, etc(Android Event Listener Documentation). To use the onTouchEvent properly you need to check the MotionEvent type.
List<Rect> retangles;//Assume these have been drawn in your draw method.
#Override
public boolean onTouchEvent(MotionEvent event) {
int touchX = event.getX();
int touchY = event.getY();
switch(event){
case MotionEvent.ACTION_DOWN:
System.out.println("Touching down!");
for(Rect rect : rectangles){
if(rect.contains(touchX,touchY)){
System.out.println("Touched Rectangle, start activity.");
Intent i = new Intent(<your activity info>);
startActivity(i);
}
}
break;
case MotionEvent.ACTION_UP:
System.out.println("Touching up!");
break;
case MotionEvent.ACTION_MOVE:
System.out.println("Sliding your finger around on the screen.");
break;
}
return true;
}
In your onTouchEvent() just capture the x and y values and you can use the contains(int x, int y) method in the Rect class. If contains(x, y) returns true, then the touch was inside the rectangle and then just create the intent and start the new activity.

Android touchevent--- need help/suggestion according to scenario

Now I have an ImageView and its a circle which is at the slightly below than center position (But this should not matter).
I have written code onTouch of ImageView for ACTION_DOWN,ACTION_UP ,now consider as user have put finger on circle and move and move.... I want to active some code when user move finger and exceed the region of CIRCLE image(As soon as user exceed the region the code should be ececuted onlly once)
Here is my code
ImageView view1 = (ImageView) findViewById(R.id.fccircledetectionarea);
view1.setOnTouchListener(new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
ImageView imageView=(ImageView) findViewById(R.id.fccircledetectionarea);
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
final float x=imageView.getTop();
Toast.makeText(PlayScreen.this, "Top Position:"+x, Toast.LENGTH_SHORT).show();
break;
case MotionEvent.ACTION_UP:
Toast.makeText(PlayScreen.this, "Over", Toast.LENGTH_SHORT).show();
break;
case MotionEvent.ACTION_MOVE:
Toast.makeText(PlayScreen.this, "Over", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
});
I cannot archive my goal through ACTION_MOVE: as it start to work if user move finger within the circle
And my second query is that How to set alpha of this imageview I have used
view1.setAlpha(0);
But is not working and I have also made this imageview invisible but than my onTouch code is not working
Within your onTouch method record a boolean to check if the user is touching within the bounds of the circle.
boolean inCircle = java.lang.Math.sqrt(x*x + y*y) <= circleRadius
Where x is taken from event.getX() - circleCenterX and y is taken from event.getY() - circleCenterY (touch position's offset from center of the circle)

Categories

Resources