溫馨提示×

Android怎么實現視圖從屏幕底部滑入的動畫效果

小億
197
2024-04-23 16:34:48
欄目: 編程語言

要實現視圖從屏幕底部滑入的動畫效果,可以使用Android中的屬性動畫或者布局動畫。

  1. 使用屬性動畫: 可以通過設置視圖的translationY屬性來實現從底部滑入的效果。首先需要在res目錄下的anim文件夾中創建一個xml文件,比如命名為slide_in_bottom.xml,內容如下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromYDelta="100%p"
        android:toYDelta="0%p"
        android:duration="500"/>
</set>

然后在代碼中加載這個動畫文件,并給目標視圖設置動畫:

Animation animation = AnimationUtils.loadAnimation(context, R.anim.slide_in_bottom);
view.startAnimation(animation);
  1. 使用布局動畫: 可以在布局文件中設置視圖的入場動畫,比如通過設置android:layoutAnimation屬性來實現從底部滑入的效果。在res目錄下的anim文件夾中創建一個xml文件,比如命名為slide_in_bottom_anim.xml,內容如下:
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:delay="0.5"
    android:animation="@anim/slide_in_bottom"/>

然后在布局文件中給目標視圖設置android:layoutAnimation屬性:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layoutAnimation="@anim/slide_in_bottom_anim">
    <!-- 子視圖 -->
</LinearLayout>

通過以上兩種方法,就可以實現視圖從屏幕底部滑入的動畫效果。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女