Ball game - changing ball position - android

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

Related

Android - Canvas not drawing after Translation?

I am working on a custom view where i draw lines horizontally one after another left to right. When the collective width of all the lines combined crosses the width of the view , i translate the view. After translating the view , the view moves leftward , but canvas.drawLine stops drawing lines as soon as i translate the view. Any solution to this problem?
for(someCondition){
canvas?.drawLine(startX,startY,stopX,stopY,linePaint)
if(startX > (width)){
log("Invisible , setting translation to ${-(startX - width)}")
translationX = -(startX - width)
}
log("width is $width and startX is $startX")
}
as soon as i translate , the canvas stops drawing but translate keeps happening.
I solved the problem without having to translate anything.
Everytime the collective width of the lines crossed the width of the View , i increased the starting index of the array so that i drew only n elements, where n is the max number of lines that can fit inside the views width. This solution also helped avoid drawing unnecessary lines everytime a new element was added to the array.

How to apply animation effect to a View without using time interpolation

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

Android - How to check the display edges

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.

How to set OnClickListener to rotated ImageView?

I need to set an OnClick listener on a rotated rectangle ImageView. I've got problem when setting itafter rotation (angle 45). When i rotated my ImageView, region of the OnClickListener answers in the same place as before rotate :(
Any ideas?
Tween Animations doesn't really "move" the View, it just alters where it is drawn, i.e. you're going to have to add an AnimationListener and resize (in your case, changing the margins should work)/move the object after the animation has completed for the "physical" location to update.
It's a lot better in Honeycomb / Ice cream sandwich with property animations.

Android - Artifacts using Animation

I am having a problem with visual artifacts on the screen when applying the 3D transformation found here. I have changed this so it rotates around the x axis instead of the y. When i do a full 180 rotation (top going away from you at first) im getting single pixel line artifacts at the bottom area (bottom 10-20%) of every other view that this is applied to. I am using a selector as the background of a LinearLayout and then applying this Animation to it. Can anyone think of a quick solution to this issue?
Thanks for any help!
Turns out you just have to invalidate the parent view on each animation step. If you have a custom Animation object you can just do this inside Animation.applyTransformation(...)
I had a similiar problem with a 2D animation where a View is moved off screen (outside parent view). My solution was quite simple. In my custom View I simply invalidate the parent view to have it redrawn at every frame.
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
((View) this.getParent()).invalidate();
canvas.drawBitmap(icon, bm_x, bm_y, mPaint);
}

Categories

Resources