Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีแสดง/ซ่อน View ด้วยแอนิเมชั่น Slide Up/Down ใน Android


ตัวอย่าง: การแสดง/ซ่อน View พร้อมแอนิเมชั่น Slide Up/Down

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

เปิด Android Studio ไปที่ File → New Project แล้วกรอกรายละเอียดที่จำเป็น

ขั้นตอนที่ 2: จัดการ Layout (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://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="คลิก"
        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="ชื่อผู้ใช้"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <EditText
            android:hint="รหัสผ่าน"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
</RelativeLayout>

ในโค้ดข้างต้น มี Button ควบคุมการแสดง/ซ่อน LinearLayout (ที่มี EditText 2 อัน) ด้วยแอนิเมชั่น

ขั้นตอนที่ 3: เขียนโค้ด Java (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 (Slide Up)
                    view.setVisibility(View.VISIBLE);
                    TranslateAnimation animate = new TranslateAnimation(
                        0, 0,
                        view.getHeight(), 0);
                    animate.setDuration(500);
                    animate.setFillAfter(true);
                    view.startAnimation(animate);
                } else {
                    // ซ่อน View (Slide Down)
                    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 สำหรับ Slide Up/Down

แสดง View (Slide Up):

TranslateAnimation animate = new TranslateAnimation(
    0, 0,
    view.getHeight(), 0);
animate.setDuration(500);
animate.setFillAfter(true);
view.startAnimation(animate);

ซ่อน View (Slide Down):

TranslateAnimation animate = new TranslateAnimation(
    0, 0,
    0, view.getHeight());
animate.setDuration(500);
animate.setFillAfter(true);
view.startAnimation(animate);

พารามิเตอร์ของ TranslateAnimation: (fromX, toX, fromY, toY) โดย Y ลบ = ขึ้น, Y บวก = ลง

การทดสอบแอปพลิเคชัน

เชื่อมต่ออุปกรณ์ Android จริง ใน Android Studio คลิกไอคอน Run เลือกอุปกรณ์ แล้วตรวจสอบหน้าจอ

วิธีแสดง/ซ่อน View ด้วยแอนิเมชั่น Slide Up/Down ใน Android

กดปุ่มครั้งแรก: View เลื่อนขึ้นมาแสดง (Slide Up)

วิธีแสดง/ซ่อน View ด้วยแอนิเมชั่น Slide Up/Down ใน Android

กดปุ่มอีกครั้ง: View เลื่อนลงซ่อน (Slide Down)