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
Related
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.
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 find that the unit of the coordinate system of Canvas is different from that of screen.
For example in my case as below:
For one particular point, its on-screen coordinate obtained from ImageView.getX() and ImageView.getY() is (336, 578).
Then by trial and error, I draw a dot on the Canvas so that this dot falls EXACTLY the same position as the ImageView. I called canvas.drawCircle(330, 440, radius, paint); to achieve this.
Here comes the question:
Why would the 2 coordinates, (336, 578) and (330, 440), different?
Is it because the screen and the canvas use different units?
Is it an issue regarding pixel, dp and all that?
You could try converting to and from window coordinates using View.getLocationInWindow this way you'll always be sure you are using the same coordinates system.
Coordinates of ImageView.getX an Y are relative to the parent. Canvas coordinates are relative to the view. This could cause the differences
Edit:
I assume in one place you get the coordinates of the view (using getX and getY). Convert these to window coordinates. Call these viewXY[].
When you need to use them, to draw in your canvas, also convert the top and left coordinates to window coordinates. We call these canvasXY[].
Now, the X position to draw at would be viewXY[0] - canvasXY[0], and the Y position would be viewXY[1] - canvasXY[1].
A view's position co-ordinates are always relative to it's parent. It's like saying, where with respect to it's parent is this view placed.
There in another thing, screen Co-ordinate. This says where with respect to the window top/left (think actual physical screen) is this object placed. This can be obtained through View.getLocationOnScreen.
If you want to compare two locations, they will only be equivalent if the parent view of both these locations is same or you're using absolute screen co-ordinates. Otherwise, you'll only get results that "seem" different.
If you want to unify the two either take them to a common parent or use absolute co-ordinates.
hi I'm working with the animations, I currently have 4 directional buttons, depending on which button I press, the image will move in that direction. My problem is so that the image goes beyond the edges of the screen, and I have no idea how to do it xD. For the animation use this ObjectAnimator objectAnimator= ObjectAnimator.ofFloat(IMG3, "translationX", Position_Start,Position_End);
Of course! All you need to do is get the size of the screen in pixels (see here). the top left corner of the screen will be (0,0) and the bottom right will be your output from one of the functions provided.
Then, all you need to do is get the position of your view (animation) using View.getLocationOnScreen() and/or getLocationInWindow() to get the top left corner and using View.getWidth() and View.getHeight() to get the size of your view.
The final step is to make sure that View.getLocationOnScreen() is never less than (0,0) and View.getLocationOnScreen() + (getWidth(),getHeight()) is always less than display.getSize(size), or whatever method you use to get screen size.
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