Put a custom pin over an image and get its coordinates - android

I have an ImageView with an image loaded,
I want that when the user clicks in a point of the image, another little image(used as pin) is overlapped in this point and the coordinates of the point are returned.
But I haven't idea of how could I do this.

To place a image at a certain coordinates you will have to draw the image on the canvas .
To get the coordinates of the touch event use the following code:
#Override
public void onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
mTouchX = event.getX();
mTouchY = event.getY();//stores touch event
} else {
mTouchX = -1;
mTouchY = -1;
}
super.onTouchEvent(event);
}
Here is the code for drawing the image on canvas Image in Canvas with touch events
hope it helps.

Try this,
It will help you to place image based on the co-ordinates. So it can be overlapped
Link 1

Related

make a transparent part of an image unclickable

I use Android studio and I have this image with a transparent background. Whenever i click on it it'll bring me to another Activity. But even when I click on the transparent part of the image it'll bring me to the other Activity.
Is it possible to make the nontransparent part clickable (or touchable) and the transparent part unclickable?
Yes this is possible but it becomes much more difficult than just adding an OnClickListener.
The trick is to use a Touch listener instead of click and on either a DOWN or UP event take the position and then either use some simple maths to work out whether it was a transparent area (if the design is a simple one) or, do some more complicated stuff to work out your pixel values at the centre.
new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
If (event.getAction() == MotionEvent.ACTION_DOWN) {
final int x = (int) event.getX();
final int y = (int) event.getY();
//now map the coords we got to the
//bitmap (because of scaling)
ImageView imageView = ((ImageView)v);
Bitmap bitmap =((BitmapDrawable)imageView.getDrawable()).getBitmap();
int pixel = bitmap.getPixel(x,y);
//now check alpha for transparency
int alpha = Color.alpha(pixel);
If (alpha != 0) {
//do whatever you would have done for your click event here
}
}
return true; //we've handled the event
}
}

Android: Draggable Toast, Or Alternative?

I'm drawing a number of bitmaps on a canvas, and using MotionEvents to let me drag them around.
When each item is pressed, I'd like to display a Toast, or, Toast-like mini information-panel that tracks the movement of the bitmap being dragged during an ACTION_MOVE. The "Toast" would appear on ACTION_DOWN and vanish on ACTION_UP.
The problem with using Toast is that I have to give it a duration, and, also, I can't change its position once it has been displayed. Unless I can kill the Toast for each ACTION_MOVE, and display a new one right away at the current coordinates? (Sorry, thinking aloud at this point, can't get to my dev machine to test...)
I don't know what other options there might be to achieve this, and I'd very much appreciate suggestions from the community.
Hope this helps, just whipped it up, might even compile!
private boolean mDragging = false;
private float mTouchX = 0, mTouchY = 0;
private Paint mTextPaint = new Paint();//need to set this up in onCreate!
public boolean onTouchEvent(MotionEvent event)
{
mTouchX = event.getX();
mTouchY = event.getY();
if(event.getAction() == ACTION_DOWN)
{
mDragging = true;
}
else if(event.getAction() == ACTION_UP)
{
mDragging = false;
}
return true;
}
protected void onDraw (Canvas canvas)
{
/* Put all your bitmap drawing here. */
/* Draw some info text on top of everything else. */
if(mDragging)
{
String text = mTouchX + ", " + mTouchY;
canvas.drawText(mTouchX, mTouchY + 50, text, mTextPaint);
}
}
A Toast isn't suitable in this case for reasons you already mentioned. It will be better to define a region on the Canvas and draw the message string there using drawText. Put this in the onDraw method and call invalidate whenever you need to update the text or the position of the message board.

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;
}

change the position of a bitmap where ever i touch on screen

I have a large image on the screen and I want to display a small image on that image where I touch the screen. but I do not know how to change the position of the image when I touch on the screen and the small image must display where ever I touch on screen.
any suggestions or hint will be appreciative.
thanks
Suppose you have your moving bitmap already in an ImageView which is part of your RelativeLayout.
Whenever the user touches the screen, you just have to change the position of the ImageView, by changing its margins.
You should try something like this:
#Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN)
{
RelativeLayout.LayoutParams params = myImageView.getLayoutParams();
params.setMargins(event.getX(),event.getY(), 0, 0);
myImageView.setLayoutParams(params);
}
}
User first the method onTouchEvent() to get the position of your touch.
Then i suppose you have your Bitmap placed in a UIElement like for example ImageView? And if you're using AbsoluteLayout you can set the ImageView object at the same coordinates you reveiced in the ontouchEvent.
#Override
public boolean onTouchEvent(MotionEvent event) {
event.getX();
event.getY();
}

android: how to add button or text box on canvas

I am making a canvas and setting its background which is an image
I am adding text on it by canvas. Drawtext method which works perfectly alright
now I want these text to be clickable but i couldn't find any method
The other method I could think of was to add text box on canvas add write on click event of these text box but could not find any example related to this can anybody suggest what to do.
Canvas is a space where you can just draw some graphics, thus the only way to do what you want is detecting when the user click the surface the canvas is drawn on (e.g. a SurfaceView), and using the coordenates you just fire an event. Of course, you need to verify whether the click was done on the specific part you want (e.g. the area where you drew a button or something).
Use the onTouchEvent method. Here is an example I used for finding out if the user's click coordinates are in a List of rectangles (aka buttons):
#Override
public boolean onTouchEvent( MotionEvent event) {
super.onTouchEvent(event);
int x = (int)event.getX();
int y = (int)event.getY();
xStored = x; yStored=y;
if (event.getAction()==MotionEvent.ACTION_UP){
}else if(event.getAction()==MotionEvent.ACTION_DOWN){
System.out.println("Touching down!");
for(Rect rect : rectangles){
if(rect.contains(x,y)){
System.out.println("Touched Rectangle, start activity."+x+","+y);
invalidate();
}else{
}
}
}else if(event.getAction()==MotionEvent.ACTION_MOVE){
}
this.postInvalidate();
return true;
}

Categories

Resources