Giridharan
Giridharan

Reputation: 4462

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.

Upvotes: 2

Views: 4707

Answers (2)

Kishan Solanki
Kishan Solanki

Reputation: 14618

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

Upvotes: 0

DSS
DSS

Reputation: 7259

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.

Upvotes: 2

Related Questions