如何在Android中使用滑动动画显示和隐藏视图?
此示例演示了如何在Android中使用滑动动画显示和隐藏视图。
步骤 1 − 在Android Studio中创建一个新项目,转到文件 ⇒ 新建项目,并填写所有必需的详细信息以创建新项目。
步骤 2 − 将以下代码添加到res/layout/activity_main.xml。
<?xml version = "1.0" encoding = "utf-8"?> <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"> <Button android:id = "@+id/button" android:layout_centerHorizontal = "true" android:layout_marginTop = "100dp" android:layout_width = "150dp" android:text = "Click" android:layout_height = "wrap_content"/> <LinearLayout android:id = "@+id/view" android:background = "#a6e1aa" android:orientation = "vertical" android:layout_alignParentBottom = "true" android:layout_width = "match_parent" android:layout_margin = "20dp" android:layout_height = "200dp"> <EditText android:hint = "User name" android:layout_width = "match_parent" android:layout_height = "wrap_content" /> <EditText android:hint = "Password" android:layout_width = "match_parent" android:layout_height = "wrap_content" /> </LinearLayout> </RelativeLayout>
在上面的代码中,我们使用按钮来显示/隐藏带有动画的线性布局。
步骤 3 − 将以下代码添加到src/MainActivity.java
package com.example.andy.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
boolean opened;
LinearLayout view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = findViewById(R.id.view);
view.setVisibility(View.INVISIBLE);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!opened){
view.setVisibility(View.VISIBLE);
TranslateAnimation animate = new TranslateAnimation(
0,
0,
view.getHeight(),
0);
animate.setDuration(500);
animate.setFillAfter(true);
view.startAnimation(animate);
} else {
view.setVisibility(View.INVISIBLE);
TranslateAnimation animate = new TranslateAnimation(
0,
0,
0,
view.getHeight());
animate.setDuration(500);
animate.setFillAfter(true);
view.startAnimation(animate);
}
opened = !opened;
}
});
}
}在上面的代码中,我们使用平移动画显示和隐藏线性布局,如下所示 -
要显示视图,请使用以下代码 -
TranslateAnimation animate = new TranslateAnimation( 0, 0, view.getHeight(), 0); animate.setDuration(500); animate.setFillAfter(true); view.startAnimation(animate); To hide the view, use the following code - TranslateAnimation animate = new TranslateAnimation( 0, 0, 0, view.getHeight()); animate.setDuration(500); animate.setFillAfter(true); view.startAnimation(animate);
让我们尝试运行您的应用程序。我假设您已将您的实际Android移动设备连接到您的计算机。要从Android Studio运行应用程序,请打开您的一个项目活动文件,然后单击工具栏中的运行
图标。选择您的移动设备作为选项,然后检查您的移动设备,它将显示您的默认屏幕 -

当用户点击按钮时,它将显示如下屏幕,现在点击同一个按钮隐藏视图,如下所示 -

点击此处下载项目代码
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP