Rotate an object in a circular way using buttons in android studio - android

I want to make a game which requires several objects to rotate around the center of the screen when you press a button ( go left or right ), however they need to orbit the center periodically as you press the button, not move when you press it once. I can make them move in that way but they do a 360 spin or go back to their original position. Can someone give me an example of how to do this? Its the same functionality as in the circle pong app.
Thank you!

There are multiple ways to do this. If you want to do it using your own Custom View. In computer graphics coordinate system (0,0) start from left top corner. For that first translate your view to center.
canvas.translate(getWidth()/2,getHeight()/2);
then in order to move an object you have to use a mathematical formula.
x = R * cos#
y = R * sin# where range of cos and sin is between 1 and -1.
R is radius . you can simply draw your view to x,y location . So it will display as it is moving.
and there are other simpler ways. See here
And also see a Custom knob I created at Here

In your anim folder create an xml
<?xml version="1.0" encoding="UTF-8"?>
<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:duration="1200" />
in your code
view.startAnimation(
AnimationUtils.loadAnimation(activity, R.anim.rotate_indefinitely) );

Maybe this post will help you Move ImageView elliptically around a center in a RelativeView
Here instead of 200,400 put your own diameter based on which you want to rotate the objects

Related

Smooth slider with two (or more) images

How I can reach such behavior with a smooth slider?
I want that first ImageView to increase his width and cover over second ImageView.
I tried three different approaches to this purpose.
First I drew bitmap (two bitmaps combined inside one canvas) inside the canvas of one ImageView. New position generated with ValueAnimation but it gives values with not equals intervals. During rendering the transition do jumps noticeably enough. On a good phone especially.
Second I did my own method for generating sequence numbers without intervals and but occurs another problem: phone cant render new bitmaps a few hundred times per second. If to do intervals jumps occur again.
Then I tried another way: to create two ImageView and first must smooth roll out over the second. But I can't reach the proper effect than the first picture must not stretch (image inside ImageView increased width together) but need a roll-out like on GIF.
Code of my third the attempt:
In fragment
val animation: Animation = AnimationUtils.loadAnimation(ctx, R.anim.scale_right)
imageView.startAnimation(animation)
imageView.visibility = View.VISIBLE
scale_right.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="6000"
android:fromXScale="0"
android:fromYScale="1.0"
android:pivotX="0%"
android:pivotY="0%"
android:toXScale="1.0"
android:toYScale="1.0" />
</set>
What do I need to do?

How to use scale animation to hide image in Android

Is it possible to hide image using scale animation in xml. What I mean is that I have an image on the screen and I want to hide it using scale animation in the direction from 0.0 -> 1.0. So the image is on the screen and I want to start scaling it from left to right until it disappear. Is it possible?
This is not what I want. You see my app is suporting devices below 3.0. And I don't want to move an image. I want to scale it, but in opposite direction. The scale animation works with the coords of the screen, so the image will be shown if you start from 0.0 to 1.0. But I want to hide it from 0.0 to 1.0. Is it possible?
First scaling means changing the size of the image which is not what you want! you want to change the position of an image. With property animation (supported in android 3.0+) you can animate any property of an object (even non visual properties).
use this code to animate any property
ValueAnimator move = ValueAnimator.ofFloat(0,100); //start and ending value
scaleUp.setDuration(300);
scaleUp.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
Float newValue = (Float) valueAnimator.getAnimatedValue();
myObject.setX(newValue); //in pixcels
}
});
move.start();
This code changes the value of "x" property from 0 to 100 over the period of 300ms. x and y are position of your view in pixels. of course you need to first calculate start and ending point in pixels.
findout more about "x" property here http://developer.android.com/reference/android/view/View.html#setX(float)
you can also animate setTranslationX istead http://developer.android.com/reference/android/view/View.html#setTranslationX(float)
As i said earlier with property animation you can animate any property you want and it's really easy to work with, to learn more about it read this http://developer.android.com/guide/topics/graphics/prop-animation.html
I managed to do what I wanted. I used scale animation with the tag pivotX. With pivotX or pivotY you can set from what point of the image to start the animation and in what direction. You have to play a little bit with it until you recive your desired animation. Here is my code:
<scale
android:duration="4000"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="450"
android:interpolator="#android:anim/linear_interpolator"
android:startOffset="300"
android:toXScale="0.0"
android:toYScale="1.0" />

Translation of image from center to top in android

Hi I have stuck with the problem of creating a splash screen where the image is placed in center_vertical|center horizontal.how to translater over to center_horizontal|top in android.can any one please guide me how to do it.
Use animations to do this, for more information about animating views, you could refer to this link and also this
What you are actually looking for in translating a view from the center to the top of the screen, you could use translation animation using the xml.
1.Create a folder anim in the res folder
2.Add a resource file that describes your translation effect like :
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="50%p" android:fromYDelta="50%p"
android:toXDelta="50%p" android:toYDelta="0%p"
android:duration="1000"
android:fillAfter="true" />
You could experiment with the % values.
3.implement this in code like :
translateAnim= AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.translate_anim);
imageView.startAnimation(translateAnim);
Hope this much of clue suffices!
P.S: You could experiment with fillAfter true or false, so that you could understand their effects better.
This is I think best and easiest approach to achieve animation from center to top:
ivSplashCenter.animate()
.translationY(-((rlContainer.height - (ivSplashCenter.height * 2)) / 2).toFloat())
.setInterpolator(AccelerateInterpolator()).duration = 1500
Here, ivSplashCenter is imageview and rlContainer is my root view of XML
NOTE:
One thing you need to understand here is that I am keeping a space from the top edge of the height of the image by ivSplashCenter.height * 2,
If you don't want any padding/space at the top then you can just use ivSplashCenter.height

How to rotate a shape around a central axis

I'm surprised to find so few examples when it comes to Android animation. I have found the animation demos but they provide only a small set of what you might want to achieve.
For my particular case I'm just trying to animate a shape around a central point. I can see from the demo that you can use the following to pass in values:
ObjectAnimator.ofObject(
ballHolder
, "name"
, new XYEvaluator()
, position1, position2, position3, position4, positionEtc);
But how can I pass in all the values for a circle? I'm sure you wouldn't achieve it this way (as in, the example above).
Maybe I should be doing it a different way? Any help would be much appreciated.
try this code and adjust it as you need :
To implement the rotation animation, you can define that animation in XML:
create an animation xml file under /res/anim folder called :rotate_around_center_point.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<rotate android:toDegrees="360"
android:duration="700"
android:pivotX="205"
android:pivotY="180"
android:interpolator="#android:anim/linear_interpolator"/>
</set>
apply the animation to View : lets say it for rotate image :
ImageView animationTarget = (ImageView) this.findViewById(R.id.testImage);
Animation animation = AnimationUtils.loadAnimation(this,
R.anim.rotate_around_center_point); animationTarget.startAnimation(animation);
OR
create the animation dynamically in Java code:
Animation newAnimation = new RotateAnimation(0, 360, 205, 180);
newAnimation.setDuration(700);
animationTarget.startAnimation(newAnimation);
HOPE THIS HELP .

Changing X and Y position of button/ changing margin of button dynamically

I would like to change margin of my buttons in d applcn...
suppose I have 5 buttons like this in a linear layout, one below d other...
When I focus on a button, its width should increase (which I know how to handle) and at the same time, width should increase linearly towards both left and right.
i.e. If the current width of the button is 250 with x co-ordinate as 50 (that means button's left is 50 and button's right is 300), when I focus on the button I should get x co-ordinate as 75 (left = 25 and right = 325). How to change the margin/x co-ordinate of the button?
Check out the Animation and ScaleAnimation classes at http://developer.android.com/reference/android/view/animation/Animation.html and http://developer.android.com/reference/android/view/animation/ScaleAnimation.html.
One way to do it is to define two XML files in your res/anim directory, one to make the button larger, and the other to make it smaller. So for makeLarger.xml
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:fromXScale="1"
android:toXScale="1.1"
android:pivotX="50%"
android:pivotY="50%"
android:duration="200"
android:fillAfter="true" />
Here, you're increasing the width by 10%, from 1.0 to 1.1. For the makeSmaller.xml file, swap the values of android:fromXScale and android:toXScale, or just set them as you like. To apply the animation to your button,
Button myButton = (Button)findViewById(R.id.myButton);
myButton.setAnimation(AnimationUtils.loadAnimation(this, R.anim.makeLarger));
Of course, the makeLarger and makeSmaller animations should be set when you touch the button.
I hope this helps!

Categories

Resources