Draggable circle google map android studio - android

So I am currently working on an android studio project which suppose to create a geofence each time the user presses a long click on the screen the circle appears there I also would like to allow the user drag the circle on the screen and update it's current placement each time. I added a view to the top of the map layout how can I implement such thing? Should I convert the latlng of the circle's center into floats and calculates it when the user presses down and if so I will be glad if someone could show me how to convert latlang into float and vice versa in case my idea of implementation is not accurate.
This is the cuurent state of my code as far as the onTouchListener:
my_view.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
float xDown,yDown;
switch(event.getActionMasked()){
case MotionEvent.ACTION_DOWN:
xDown=event.getX();
yDown=event.getY();
break;
case MotionEvent.ACTION_MOVE:
float moveX,moveY;
moveX=event.getX();
moveY=event.getY();
// float distanceX=moveX-xDown;
//float distanceY=moveY-yDown;
break;
}
return false;
}
});

Related

Android visual effect on touch on a activity

I'm working on an Android app and I want to add some eyecandy to the UI. I have an activity (let's call it MainActivity) which has some text fields, buttons and a gallery on it.
What I would like to achieve is: When the user touches somewhere on this activity, there should be some visual effect at the point where he touched (e.g. something like sparkles etc.).
So the essential parts of my question are:
a) How can I determine where the user touched
b) how can I draw my effects on to the screen (as an 'overlay' to the activity).
Thanks in advance for every helpful answer.
(I've checked out this answer but it doesn't seem to be applicable to my situation. )
First, you have to add onTouchListener to your root Layout, then you can get coordinates, where the user has touched the screen.
example :
float x,y;
rl = (RelativeLayout) findViewById(R.id.root_layout);
rl.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
float x = event.getX();
float y = event.getY();
return super.onTouchEvent();
}
});
You should override the onTouchEvent method of your activity. You can then obtain the coordinates of the touch event:
#Override
public boolean onTouchEvent(MotionEvent event) {
float touchX = event.getX();
float touchY = event.getY();
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
// touch down
break;
case MotionEvent.ACTION_MOVE:
// movement
break;
case MotionEvent.ACTION_UP:
// touch up
break;
default:
// default
}
return super.onTouchEvent(event);
}
To answer further on your question, to have a overlay, look at the APIDemos -> Graphics -> OpenGL ES -> Translucent GLSurfaceView. This will help you to create overlay on your activity. There may be some other example in API demos which will get you some help. Android's API Demos are good set of examples to address some known issues.
If you are working on ICS then I recommend you to look at the Developer Options in Settings application. If you are not working with ICS, I believe you can look at the APIDEMOS for touchevents. They draw lines on touches. The code in that, can get you started.

Dragging an image to a specific location at the screen and alert if its the right location -Android-

I'm trying to develop an application where i can drag an image to a specific location in the screen and alert if its the right location , now the dragging part is done i just need to verify the location and show the alert when the image is in that specific location
Get the Rect coord of the specific location and check if the dragged item coordinates clash with the location Rect coords. You can use Rect.contains() api for checking. If it returns true, you can show the alert.
if (locationRect.contains(drag.left, drag.top, drag.right, drag.bottom)) {
// Show Alert dialog
}
I think you have onTouch method in your code..
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
.....
break;
case MotionEvent.ACTION_MOVE:
int x_cord = (int)event.getRawX();
int y_cord = (int)event.getRawY();
//if you want the alert when the image enters a square of (10,10) (25,25),(10,25) and (25,10).. then
if(x_cord>=10 && x_cord<=25)
{
if(y_cord>10 && y_cord<25){<-- these cordinates work if the image you are moving is a square of side 15 .. so you can change accordingly..
//alert here
}
}
......
}

Setting onDragListener for my Map?

so I am using osmdroid map and am trying to set a listener when a user drags the map around like this.
mapView.setOnDragListener(new View.OnDragListener() {
public boolean onDrag(View v, DragEvent event) {
Log.i(PREFS_NAME, "X:" + String.valueOf(event.getX()));
Log.i(PREFS_NAME, "Y:" + String.valueOf(event.getY()));
return false;
}
});
Now, everything works fine, but when I put this code in program exits with an error.
Is this the right thing to use anyway as the reason for this is that I want to get notified whenever a user moves around the map. I want to be able to check where user moves the map, hence the getX and getY I am watching. The reason for this is that I can stop the map movement if it goes out of bounds I set.
Is this the right way to do this?
MapViews don't respond to touch events like onClick() and onLongPress(). What I would do is before onCreate() add this:
#Override
public boolean dispatchTouchEvent(MotionEvent ev) {
float x = ev.getX();
float y = ev.getY();
return super.dispatchTouchEvent(ev);
}
x and y will now contain the screen coordinates that the user pressed. Hope this helps!

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)

rotate image depending on user touch on screen

i am developing game.i am displaying gun object center bottom of the screen.when user tap on screen i need to rotate gun that direction.i done rotating image.but when user tap on screen i need to rotate image that direction.can you please help me
Thanks in advance
Aswan
is your image a bitmap? could you convert it to one? Would an onClick listener not work and when the user clicks do something like.. http://www.anddev.org/resize_and_rotate_image_-_example-t621.html
Seeing some code as to what you've attempting would be nice also. The following will help you with checking if the bitmap has been clicked in case you're stuck on that also http://developer.android.com/reference/android/view/View.html .
I've never attemped this myself but I think that would work. Try redrawing the bitmap when you have resized it.
Just use onTouchListener for your View and use this code for that
#Override
public boolean onTouch(View v, MotionEvent event) {
float currentX = event.getX();
float currentY = event.getY();
Log.i(TAG, "action type is"+event.getAction());
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE: {
Log.i(TAG, "Entering in onTouch");
double rotationAngleRadians = Math.atan2(currentX - dialer.centerX, dialer.centerY - currentY);
dialer.rotationAngle = (int) Math.toDegrees(rotationAngleRadians);
Log.i(TAG, "rotaion angle"+dialer.rotationAngle);
dialer.invalidate();
return true;
}
}
return true;
}

Categories

Resources