【问题标题】:How can I make my animation goes from the bottom to top of my screen?如何让我的动画从屏幕底部到顶部?
【发布时间】:2012-11-23 14:47:16
【问题描述】:

这是我的 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>

我想让这个观点随着时间的推移而增长。

这是动画的代码

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);

我想让这个动画从屏幕底部到顶部。目前它是从上到下。

【问题讨论】:

    标签: android animation imageview surfaceview


    【解决方案1】:

    您可以使用ScaleAnimation

    例如,修改布局,使表面占据整个屏幕:

    <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"
         />
    

    然后在 Y 轴上从 0 缩放到 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);
    

    【讨论】:

    • 谢谢,但在这种情况下,它仍然是从下到上。我要改变什么价值观?
    • 哦,我不明白。然后查看我的新答案
    • 谢谢。我认为现在它会起作用。但是使用 ScaleAnimation 代替 TranslateAnimation 有什么好处呢?
    【解决方案2】:

    您可以像这样使用TranslateAnimation 来做到这一点:

        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);
            }
        });
    

    【讨论】:

    • 谢谢。但是使用 TranslateAnimation 代替 ScaleAnimation 有什么好处呢?
    【解决方案3】:

    您可以通过结合缩放和平移动画来做到这一点。平移动画将有助于改变视图的位置,而动画中的缩放将有助于改变大小。并在您的代码中使用 Accelerate_Decelerate 插值器。插值器将提供所需的效果。您可以尝试使用不同类型的可用插值器来实现效果。

    【讨论】:

    • 谢谢。我会尝试使用它。在我给出反馈之后。
    • 当然。但是请务必使用插值器并试验每种类型的插值器。它将为您提供所需的解决方案。如果它得到所需的解决方案,请告诉我。
    猜你喜欢
    • 2017-12-28
    • 1970-01-01
    • 2013-10-09
    • 2016-12-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-15
    • 2012-01-21
    • 1970-01-01
    相关资源
    最近更新 更多