I need the user to drag an arrow between two views, but all info I can find online is to make a dragshadow.
here's an exemple of the result i need.
https://imgur.com/pfQxtSK
thanks in advance.
What you want requires you to observe touch events inside of your parent ViewGroup, or custom view (whatever is drawing the light-blue grid from the image), using onTouchEvent(MotionEvent) callback. Next, you wanna determine the direction of the drag using the MotionEvent's x and y coordinates and some basic trigonometry. This will allow you to determine the angle of the drag and therefore dynamically the adjust the angle (rotation) of your arrow so that it accurately tracks the finger. Next use the same x and y coordinates you got from the MotionEvent to determine where on the canvas to draw the arrow. If the arrow is another view added as a child, you can set that view's x and y coordinates or translate the view using methods provided by the View class (you may need to play with offset's so the view doesn't get drawn off-screen after several drags/touches). If the arrow is a bitmap our drawable, you will need to adjust the draw coordinates in your view's onDraw(Canvas) method.
Related
I have an image view and a photoview in android studio. The photoview is set to an image bitmap. I want to initially place a view on a specific x, y coordinate on top of the photoview but I want to place the view depending on the matrix of the image bitmap, not the photoview. Then I want to make the image view translate from 1 x, y coordinate to another coordinate. what is the best way to do this?
Your question is a little vague but looking at the title of the question, to move a view to the coordinates of another view, you could set the x and y to the values of the view you want to move to. Let's say you want to move imagview1 to the point where imageview2 is:
imageview1.animate().x(imageview2.getX()).y(imageview2.getY());
Or if you don't wanna animate it just remove animate():
imageview1.setX(imageview2.getX());```
Could you elaborate more or in this case, clearly
In the android canvas, I have 5 shapes. In my case, I need to select one of them and move to new position. The same time need to find which one select by touch.
If You have drawn shapes by x, y coordinate and by width height then implement OnTouchListener interface and take touch of whole screen. Take touch coordinate x and y and match on onTouch implement method in which shape coordinate are touched.
I have a 600x1000 pixel screen and my player starts near the bottom left. His x and y coordinates are (50, 900). I'm drawing this player to a canvas and using
canvas.translate(-player.getX()+GAMEWIDTH/2, 0)
to make sure the player stays in the middle of the screen with respect to the x-axis. This results in my player being rendered at (300, 900), however the player's x and y coordinates remain at (50, 900). This presents an issue because I need the player's coordinates to be the same as his rendered location because I use touch events on the player's collision rectangles to move him around. Is there any way to make my screen's coordinates be relative to my canvas coordinates so that my player's coordinates correspond to where they actually get rendered? Or is there maybe another way to do it?
The touchEvents are always based on the the x & y relative to the canvas element itself. Since the view is centering the character by translating the view canvas.translate(-player.getX() + GAMEWIDTH/2 , 0); you need to also apply that translation to the touchEvent. You can do it in the touch handler itself, or you could store both a relative and absolute position for the items in your game world. This will become more important if items become nested in one another. This is typically done by storing the parent element of a sprite/object.
//example of how I usually handle object hierarchy
this._x = this.x + this.parent._x;
this._y = this.y + this.parent._y;
the canvas/stage would also store it's center as a ._x and ._y which would be the parent of the the objects added, this way when you generate the global position to do your touchEvents against instead of passing in the .x or .y you would pass in the ._x and ._y which are already translated by the GAMEWIDTH/2.
I am trying to change View's position, scale and alpha with touch events (at the time). When user touches a view and makes some movement the view should be effected. It is like applying combined animation via touch distance instead of time.
I can also calculate interpolated value for distance. For example if I move my finger half of the target distance I have the interpolation value 0,5.
Actually I could achieve this by changing the margins around the view but it only works with ImageView. Otherwise the position of the elements change during applying the effects. (Like TextViews in RelativeLayout)
So I thought that I can create a combined animation and use my own interpolated value instead of the time interpolation. I hope it is possible.
I would do it following way:
Create custom FrameLayout and call setWillNotDraw(false);
Load the image as Bitmap to be drawn
Catch touch event's inside the FrameLayout
Implement proper drawing and transformation(position, alpha, scale) in onDraw method for the Bitmap
I have a activity where I have two imageViews. In onTouch I recognize which imageView was clicked. Now I have a question. How I can draw new very small image in place where I clicked. I know that onTouch i have getX() and getY() and I save this position when I touch the screen, but I don't know how I can draw image in place where I clicked. My image is in drawable resources. I need to use canvas or is better way. I need draw this picture and after that I need run animation this image.
edit1: How can I set positionX and positionY my image. I have position where I click but I don't know how I can set image in this coordinates.
you should have a image view with visible attribute of FALSE and after touch (or any event you want) set three attribute : visible = true; x = getX() and y = getY();
is it enough or explain more?
I see the following approach.
Replace the ImageView by a custom view derived from ImageView.
Override onDraw.
call super.onDraw to draw the image
paint your image using onDraw's canvas at the last touch position.
each time you get a touch, you need to invalidate the custom view