简体   繁体   中英

How can I make my animation goes from the bottom to top of my screen?

This is my xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <SurfaceView
        android:layout_width="match_parent"
        android:layout_height="10dp"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="0dp" />

</RelativeLayout>

I want to make this view grow with the time.

Here is the code for animation

SurfaceView surfaceViewTimer = (SurfaceView) findViewById(R.id.surface_view_timer);
Animation surfaceGrowingAnimation = new TranslateAnimation
    (0, 0, Animation.ZORDER_TOP, 300);
surfaceGrowingAnimation.setDuration(5000);

surfaceViewTimer.startAnimation(surfaceGrowingAnimation);

I want to make this animation goes from the bottom to top of the screen. Currenty it is going from the top to the bottom.

You can use a ScaleAnimation .

For instance, modify the layout so that the surface takes whole screen:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<SurfaceView
    android:id="@+id/surface"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="0dp"
    android:background="@color/red"
     />

And then scale it on Y-axis from 0 to 1:

    SurfaceView surface = (SurfaceView) findViewById(R.id.surface);

    ScaleAnimation animation = new ScaleAnimation(1.0f, 1.0f, 0.0f, 1.0f);
    animation.setDuration(5000);
    surface.startAnimation(animation);

You can do it with a TranslateAnimation like this:

    final SurfaceView surface = (SurfaceView) findViewById(R.id.surface);

    surface.post(new Runnable() {
        @Override
        public void run() {
            TranslateAnimation translation = new TranslateAnimation(0f, 0f, surface.getHeight(), 0f);
            translation.setDuration(2000);
            surface.startAnimation(translation);
        }
    });

You can do it by combining Scaling and Translate Animation.Translate Animation will help in the change in position of the view while the Scale in animation will help in the Change of Size. And in your code use Accelerate_Decelerate interpolator. The interpolator will give the required effect. You can try out with the different types of available interpolators to achieve the effect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM