Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

第三方登入區塊的上拉展開、下拉隱藏:屬性動畫實作

編輯:關於 Android 程式設計

UI 與交互直接決定了使用者對一款應用的第一印象,而動畫在其中扮演的角色越來越吃重。本文把專案登入頁中的一個小效果抽離出來:第三方登入區可上拉展開、下拉隱藏,讓畫面在安靜與熱鬧之間自由切換。

(1)activity_main.xml

結構上不難:外層 RelativeLayout 貼底,內部再分「箭頭與提示文字」和「真正要展開的按鈕區」兩塊。

<?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"
    tools:context="com.example.propertyanimation.MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="vertical">

        <RelativeLayout
            android:id="@+id/re_ControlshowhideView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:orientation="vertical">

            <RelativeLayout
                android:id="@+id/re_showarrowhead"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <View
                    android:layout_width="match_parent"
                    android:layout_height="0.1dp"
                    android:layout_marginLeft="12dp"
                    android:layout_marginRight="12dp"
                    android:layout_marginTop="17dip"
                    android:background="#dadada" />
                <ImageView
                    android:id="@+id/arrowhead"
                    android:layout_width="30dip"
                    android:layout_height="30dip"
                    android:layout_centerInParent="true"
                    android:src="@drawable/jiantoubelow" />
            </RelativeLayout>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/re_showarrowhead"
                android:layout_marginTop="10dp"
                android:gravity="center"
                android:text="-其他登錄方式-" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/showhideView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/re_ControlshowhideView"
            android:gravity="center"
            android:visibility="gone"
            android:orientation="vertical">

            <Button
                android:id="@+id/btn_qq"
                android:layout_width="40dp"
                android:layout_height="57.5dp"
                android:layout_gravity="center_vertical"
                android:layout_marginLeft="10dp"
                android:background="@drawable/qqlogin"
                android:clickable="true"
                android:gravity="center"
                android:paddingLeft="10dp"
                android:textSize="16sp" />

            <Button
                android:id="@+id/btn_weixin"
                android:layout_width="40dp"
                android:layout_height="57.5dp"
                android:layout_gravity="center_vertical"
                android:layout_marginLeft="10dp"
                android:layout_toRightOf="@+id/btn_qq"
                android:background="@drawable/weixinlogin"
                android:clickable="true"
                android:gravity="center"
                android:paddingLeft="10dp"
                android:textSize="16sp" />
        </RelativeLayout>
    </RelativeLayout>
</RelativeLayout>

(2)PropertyAnimation.java:把動畫封裝成模組

核心手法是用 ValueAnimator 驅動「高度」這個屬性,並在每次更新時寫回 LayoutParams。箭頭的旋轉則交給 RotateAnimation。封裝成獨立類之後,其他頁面要複用就非常直接。

package com.example.propertyanimation;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;

public class PropertyAnimation {

    private float mDensity;
    private int mHiddenViewMeasuredHeight;

    public PropertyAnimation(Context context) {
        // 展開後要抵達的目標高度,這裡直接用 dp 轉 px 取得
        mDensity = context.getResources().getDisplayMetrics().density;
        mHiddenViewMeasuredHeight = (int) (mDensity * 57.5 + 0.5);
    }

    public void animateOpen(View v) {
        v.setVisibility(View.VISIBLE);
        ValueAnimator animator = createDropAnimator(v, 0, mHiddenViewMeasuredHeight);
        animator.start();
    }

    /**
     * 為箭頭設定向上(展開)的旋轉動畫
     */
    public void animationIvOpen(View view) {
        // RotateAnimation(起始角度, 結束角度, X 軸伸縮模式, X 伸縮值, Y 軸伸縮模式, Y 伸縮值)
        // 伸縮模式可取 ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT
        RotateAnimation animation = new RotateAnimation(0, 180,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setFillAfter(true);   // 動畫結束後停留
        animation.setDuration(100);
        view.startAnimation(animation);
    }

    /**
     * 為箭頭設定向下(收起)的旋轉動畫
     */
    public void animationIvClose(View view) {
        RotateAnimation animation = new RotateAnimation(180, 0,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setFillAfter(true);
        animation.setDuration(100);
        view.startAnimation(animation);
    }

    /**
     * 設定隱藏動畫
     *
     * @param view 動畫作用的控件
     */
    public void animateClose(final View view) {
        int origHeight = view.getHeight();
        ValueAnimator animator = createDropAnimator(view, origHeight, 0);
        // 繼承 AnimatorListenerAdapter 即可只實作需要的回呼
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                view.setVisibility(View.GONE);
            }
        });
        animator.start();
    }

    /**
     * 自定義動畫:讓高度從 start 過渡到 end
     */
    private ValueAnimator createDropAnimator(final View v, int start, int end) {
        ValueAnimator animator = ValueAnimator.ofInt(start, end);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator arg0) {
                int value = (int) arg0.getAnimatedValue();
                ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
                layoutParams.height = value;
                v.setLayoutParams(layoutParams);
            }
        });
        return animator;
    }
}

(3)MainActivity.java:接上線

package com.example.propertyanimation;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends Activity implements View.OnClickListener {

    private ImageView mIv_arrowhead;
    private RelativeLayout mHiddenLayout;
    private PropertyAnimation propertyAnimation;
    private Button btn_qq;      // QQ 登錄
    private Button btn_weixin;  // 微信登錄

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

        propertyAnimation = new PropertyAnimation(this);

        mIv_arrowhead = (ImageView) this.findViewById(R.id.arrowhead);
        mIv_arrowhead.setOnClickListener(this);

        mHiddenLayout = (RelativeLayout) this.findViewById(R.id.showhideView);

        btn_qq = (Button) this.findViewById(R.id.btn_qq);
        btn_qq.setOnClickListener(this);

        btn_weixin = (Button) this.findViewById(R.id.btn_weixin);
        btn_weixin.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.arrowhead:
                if (mHiddenLayout.getVisibility() == View.GONE) {
                    propertyAnimation.animateOpen(mHiddenLayout);
                    propertyAnimation.animationIvOpen(mIv_arrowhead);
                } else {
                    propertyAnimation.animateClose(mHiddenLayout);
                    propertyAnimation.animationIvClose(mIv_arrowhead);
                }
                break;
            case R.id.btn_qq:
                Toast.makeText(this, "QQ登錄", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_weixin:
                Toast.makeText(this, "微信登錄", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}

這個模式可以推廣到任何「次要功能預設收起」的場景。若嫌每次改高度寫 LayoutParams 太囉嗦,Android 4.4 以上也可以改用 ViewAnimationUtils.createCircularReveal() 或 TransitionManager,程式碼會更短。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved