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
Related
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
is there a way to tell a translate animation to always move to a absolute position, instead to a position relative to the card. As far as I know, using android:toXDelta only moves it to a relative position. I want it to move to a absolute position (lets say width of the screen / 2 and height of the screen / 2) from every point on the screen.
My animation:
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:duration="1000"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="-20%p"
android:toYDelta="-20%p" />
Judging from official Documentation it is not possible from XML.
However, you can do this in your Java code. Example:
view.animate().translationX(0f).translationY(0f).setInterpolator(new AccelerateDecelerateInterpolator()).start();
TranslationX and TranslationY animates to an absolute position. There is also TranslationXBy an TranslationYBy that animate relatively.
I have the following animation:
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="100%" android:toYDelta="0%" android:duration="1000" />
<translate android:fromXDelta="80%" android:toXDelta="0%" android:duration="1000" />
</set>
I use this to animate a new view to is showing up. This animation run from bottom to top and from right to left, but the animation is like rectangle. And i wana make it to be like a triangle
In other words i want the two points (X and Y) that makes the animation to be linked by a straight line.
(Not enough rep to comment)
Is it important for it to be clickable as a triangle? What I mean is, couldn't you just load on the view a triangle-like Drawable image(there are many free icon packs around or you can create your own as .png)?
This way you SEE the triangle, while as an object is just a rectangle with a texture of a triangle. (As I don't think there is any implementation in AndroidSDK of a TriangularView, but I might be wrong!).
Another way is to use a shape drawable resource.
An easy example is how it was done by Sanket Kachhela in this post
https://stackoverflow.com/a/22043386/4482734
You could take it as a starting point.
Then again, much of this post was based on the idea you needed to interact with the Triangle, but I could be wrong and it has no meaning to do so.
I'm new to Android developing. I want some animations when clicking on the card and it will move around the screen and stay at its new position forever (its interactive area should change too).
At first the card is located at the center-bottom of the screen (XDelta= 50% YDelta = 70%) then it will move up straight to center of the screen (XDelta =50% and YDelta is 40%). Then it will move to its upper left (10 dp from the left of the screen and 10 dp from the top of the screen) Sorry I can't post images due to my lack of reputation.
And here's my code in res/anim/card1.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="200"
android:fromXDelta="50%"
android:fromYDelta="70%"
android:toYDelta="40%"
android:toXDelta="50%"
android:fillEnabled="true"/>
<translate
android:duration="100"
android:fillEnabled="true"
android:fromXDelta="50%"
android:fromYDelta="40%"
android:startOffset="200"
android:toXDelta="10"
android:toYDelta="10" />
</set>
And code to get it work when clicking on the card
Animation anim = AnimationUtils.loadAnimation(MainActivity.this,
R.anim.card1);
iv_card.startAnimation(anim);
... but it just don't animate the way I want. Please help me!!!!
Check Out this (http://www.tktutorials.com/2013/07/animation-in-android-using-xml-files.html) this may help your need. Alter the translate.xml file as per your need, which is located in Res->anim->translate.xml
And solution for your posting Image. My suggestion is upload your pic in googleDrive, then change the share setting for the particular pic from private to Public on web and use the url Link to your question.
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 .