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

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

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

How can one drag an ImageView in Android?

I've spent some time researching this and still haven't found a great answer.
What I would like is to drag an ImageView around the screen. Not a drag and drop, but just an actual drag.
This tutorial has been helpful:
http://code.tutsplus.com/tutorials/android-gesture--mobile-2239
It works perfect, but it's designed to drag a bitmap, not an ImageView. I've tried replacing the Bitmap with an ImageView, but I'm not sure how the Canvas and ImageView interact in the onDraw method. I've tried imageView.draw(canvas); and that doesn't seem to work - the ImageView doesn't show up at all.
Bitmaps are not idea, because you can't add borders to them like you can with an ImageView.
I'd also like to be able to fling the ImageView off the screen.
Any suggestions?
Thanks in advance.
You can create RelativeLayout that match all your screen. In the layout create ImageView.
Set OnTouchListener listener to your relative layout to handle your finger coordinate.
After that you can write something like this :
public boolean onTouch(View v, MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_MOVE)
{
move(event);
}
}
public void move(MotionEvent event)
{
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) mImageView.getLayoutParams();
layoutParams.topMargin = event.getY();
layoutParams.leftMargin = event.getX();
mImageView.setLayoutParams(layoutParams);
}
Like using method move(MotionEvent event) you can drag your imageView where you want.
I hope this help you!

Touch events and gestures in surface view

Can anybody point me to some classes or suggest anything for the following case?
I have SurfaceView which has a background image, on top of which I wish to paint other bitmaps. I would like to support following operations:
on single tap new bitmap is added on top of background,
on double tap bitmap at given position is removed,
on tap&move (like drag&drop) bitmap is moving,
on press and move surface view is scrolling,
on pinch out/pinch in surface view is scaling accordingly.
Is this doable only with GestureRezognizer? If not, how to handle all those cases?
To handle touch input, override onTouchEvent in your class that extends SurfaceView to handle a MotionEvent. Here is a sample code that gets the screen position when a user first touches the screen.
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
touchX = event.getX();
touchY = event.getY();
}
return true;
}
More information about the MotionEvent object can be found on the Android Developers website.

Put a custom pin over an image and get its coordinates

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

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