java - Android的RelativeLayout动画问题

标签 java android android-layout android-animation android-relativelayout

我基本上试图用滑动动画将RelativeLayout从屏幕底部(用户视线之外)动画到RelativeLayout的原始位置(如xml编辑器中编码)。类似于 iOS 的上下文框,从底部滑入。

这是我的 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"
    android:background="#ffffff">

    <TextView
            android:id="@+id/welcomemsg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#000000"
            android:text="Open Panel
            android:textSize="42sp" />

      <!-- The layout that slides in from nowhere -->
      <RelativeLayout
              android:id="@+id/bottomSpinner"
              android:layout_width="fill_parent"
              android:layout_height="250dp"
              android:layout_alignParentBottom="true"
              android:background="#ffffff"
              android:visibility="visible">

      </RelativeLayout>

</RelativeLayout>

我的代码:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_view);

        //Start the animation
        SlideToAbove();

}

//bottomSpinner is my Slide in Panel
public void SlideToAbove() {
        Animation slide = null;
        slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                0.0f, Animation.RELATIVE_TO_SELF, -5.0f);

        slide.setDuration(400);
        slide.setFillAfter(true);
        slide.setFillEnabled(true);
        bottomSpinner.startAnimation(slide);

        slide.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {

                bottomSpinner.clearAnimation();

                RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                        bottomSpinner.getWidth(), bottomSpinner.getHeight());
                lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                bottomSpinner.setLayoutParams(lp);

            }
        });
    }

Good news: It works.

Bad News: Not how I want it to.

此代码使布局滑入父布局的顶部,而不是遵守布局的“alignParentBottom=true”属性。我希望它从无处滑入并停在父级底部

我应该做出哪些改变?

最佳答案

如果你想从底部滑入,你可以使用简单的 Android 内置动画,如下所示:

Animation animation = AnimationUtils.loadAnimation(this,R.anim.abc_slide_in_bottom);
yourLayout.startAnimation(animation);

关于java - Android的RelativeLayout动画问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33909114/

相关文章:

java - 在 Java 中序列化 BigInteger,用 Botan 反序列化?

android - 这段代码制造了什么样的蛇?

java - LinearLayout提供的onMeasure()参数

java - 方法 findViewById 没有按我的预期工作

android - 如何在Fragment内部调用Fragment

java - Spring Boot 3/Spring Security 6,忽略不安全端点的 "invalid"授权 header

java - 如何使用 Entity Manager 和 GenericDao Bean 和 Tomee 来持久化对象

android - GridView、GridLayout 和 Recycler View 与 GridLayout 有什么区别?

java - 泛型和创建可比较的对象 (Java)

android GridView 上单元格的颜色在滚动时发生变化