I have a 4X4 grid of textviews inside a constraintlayout. I also have an imageview that moves around the textviews upon touch input.
I override onGlobalLayout in order to record the tag id of each textview and its global location.
//inside onGlobalLayout
Rect rect = new Rect();
textviewMap.put(view.getTag().toString() , viewgetGlobalVisibleRect(rect));
So, the location of each textview is recorded in the map.
When user touches and moves the gamepiece I create a rect which computes if it intersects one of the rects inside the textviewMap!
I am able to detect the exact rect that it intersects and therefore move the gamepiece(imageview) to the centerX() and centerY() position of the rect that belongs to the textview.
Everything works wonderfully when moving horizontally. But vertically somehow the viewproperty animation does not work!
this is how I call the viewproperty animation:
if(direction.equals("horizontal")){ //works perfectly
gamepiece.animate().x(centerX()).setInterpolator(new
OverShootInterpolator).setDuration(600);
}
if(direction.equals("vertical")){ //not working good
gamepiece.animate().y(centerY()).setInterpolator(new
OverShootInterpolator).setDuration(600);
}
on the vertical movement the gamepiece jumps the textview below it and then if I try to move it up one textview it does not go. If I move two textviews vertically the gamepiece jumps only one textview NOT two.
The animations have a Animator.AnimatorListener and in the onAnimationEnd() I have tried calling:
gamepiece.clearAnimation();
animator.removeAllListeners();
Seems like this animation needs to be reset but on the vertical movement it does not work only in the horizontal movement!
see the picture I have attached!
Please help
Related
I am adding some ImageViews to a relative layout dynamically (on touch points)
Any ImageView has an OnTouchListener. In Action Move I move the ImageView(sth like Drag and Drop).
My problem is that ImageViews may overlap each other when they are moving. I want to prevent them from overlap in any way! (best is stop moving when collision happen)
Look at this image
All ImageViews are saved in a link list. I get the x & y of moving view and compare it with other views and if they had collision don't let them moving.
But I want that user can make them separate again. How can I do this work?
In brief when views collision happen I don't let them move any more but I want if the move was making them far from each other it happens.
Try this
Rect rc1 = new Rect();
imageView1.getDrawingRect(rc1);
Rect rc2 = new Rect();
imageView2.getDrawingRect(rc2);
if (Rect.intersects(rc1, rc2) {
// intersection is detected
// here is your method call
}
i have a button which rotates in the middle of the screen using an ObjectAnimator and another button (which will be a square or a circle) moving back and forth, which sometimes does overlap on the first.
I need to know when the first button is covered by the second one, however the first button does rotate, so i can't just compare the two rectangles.
Does anybody know a solution?
edit
I've been trying pturner's solution but there have been a few issues that i can't fix anyhow:
The rotating button's rectangle's values seem to be generated from the origin of the axis, rather than the origin of the button, thus the moving button will believe he's intersecting the rotating button in the top-left side of the screen.
When the rotating button rotates, the rectangles' values will eventually invert, becoming negative and thus flipping left/right and top/bottom. As a result at certain angles the moving button believes to always be overlapping, while at some other it believes to never overlap.
I really have no clue how to fix those issues, i'll just leave this question here hoping for someone to know a solution.
I think you should be able to get the rects for both views, and then transform them with the matrix, something along the lines of:
Matrix matrix1 = button1.getMatrix();
Rect box1 = new Rect();
button1.getDrawingRect(box1);
Rectf boxf1 = new RectF(box1);
matrix1.mapRect(boxf1);
Matrix matrix2 = button2.getMatrix();
Rect box2 = new Rect();
button2.getDrawingRect(box2);
Rectf boxf2 = new RectF(box2);
matrix2.mapRect(boxf2);
boolean intersect = boxf1.intersect(boxf2);
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 am making simple ball game and l have problem.I can't change position of Ball.
I tried:
SetX() and setY() but lower APIs aren't supported.
Params and margins but when I move it left or right, the whole activity content is moving with it. Up and down moving is working fine. (Activity will have 10 ImageViews)
Android animations - Problem is that I can't get coorinates (getLeft(), getTop()) during the animation.
Canvas and draw elements - I change position of image with onDraw() and invalidate() functions but when I but backgorund and all other images (as bitmaps) it is very slow.
Can you give to me any ideas or suggestions? Thanks in advance.
Try VerticalLayout and then change position with params
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