1. 程式人生 > >Android開發之實現蘋果瀏覽器網頁跳轉動畫

Android開發之實現蘋果瀏覽器網頁跳轉動畫

前言:上一章《Android開發之安卓屬性動畫大總結》大致介紹了安卓的屬性動畫以及使用時的注意點,今天我們就來一個小綜合案列,實現類似於蘋果瀏覽器的跳轉頁面!

----------------------分割線------------------------

來看下效果圖:


----------------------分割線------------------------

分析:1.購買詳情頁面設定成INVISIBLE。

2.首頁面同時做翻轉、透明、縮放然後往上平移動畫

3.詳情頁面開始顯示出來。

4.退出的時候,動畫反過來再做一遍。

----------------------分割線------------------------

程式碼奉上:

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

    private LinearLayout first;
    private ImageView second;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        first = (LinearLayout) findViewById(R.id.first);
        second = (ImageView) findViewById(R.id.second);
        button = (Button) findViewById(R.id.bt);
    }

    public void startFirstAnimation(View view) {
        /**
         * 1)first_View動畫:1.翻轉動畫;2.透明度動畫;3.縮放動畫
         */
        //翻轉
        ObjectAnimator firstRotationAnim = ObjectAnimator.ofFloat(first, "rotationX", 0f, 25f, 0f);
        firstRotationAnim.setDuration(400);
        //透明度
        ObjectAnimator firstAlphaAnim = ObjectAnimator.ofFloat(first, "alpha", 1f, 0.5f);
        firstAlphaAnim.setDuration(300);
        //縮放
        ObjectAnimator firstScaleXAnim = ObjectAnimator.ofFloat(first, "scaleX", 1f, 0.8f);
        firstScaleXAnim.setDuration(300);
        ObjectAnimator firstScaleYAnim = ObjectAnimator.ofFloat(first, "scaleY", 1f, 0.8f);
        firstScaleYAnim.setDuration(300);
        //平移上去
        ObjectAnimator firstTranslationYAnim = ObjectAnimator.ofFloat(first, "translationY", 0f, -0.1f * first.getHeight());
        firstTranslationYAnim.setDuration(300);
        //把隱藏的給顯示出來
        ObjectAnimator sencondTranslationYAnim = ObjectAnimator.ofFloat(second, "translationY", second.getHeight(), 0);
        sencondTranslationYAnim.setDuration(300);
        sencondTranslationYAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                second.setVisibility(View.VISIBLE);
                button.setClickable(false);
            }
        });
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(firstRotationAnim, firstAlphaAnim, firstScaleXAnim, firstScaleYAnim, firstTranslationYAnim, sencondTranslationYAnim);
        animatorSet.start();
    }

    public void startSecondAnimation(View view) {
        //翻轉
        ObjectAnimator firstRotationAnim = ObjectAnimator.ofFloat(first, "rotationX", 0f, 25f, 0f);
        firstRotationAnim.setDuration(400);
        //透明度
        ObjectAnimator firstAlphaAnim = ObjectAnimator.ofFloat(first, "alpha", 0f, 1f);
        firstAlphaAnim.setDuration(300);
        //縮放
        ObjectAnimator firstScaleXAnim = ObjectAnimator.ofFloat(first, "scaleX", 0.8f, 1f);
        firstScaleXAnim.setDuration(300);
        ObjectAnimator firstScaleYAnim = ObjectAnimator.ofFloat(first, "scaleY", 0.8f, 1);
        firstScaleYAnim.setDuration(300);
        //把first平移下去
        ObjectAnimator firstTranslationYAnim = ObjectAnimator.ofFloat(first, "translationY", -0.1f * first.getHeight(), 0f);
        firstTranslationYAnim.setDuration(300);
        //把第二個view下移
        ObjectAnimator sencondTranslationYAnim = ObjectAnimator.ofFloat(second, "translationY", 0f, second.getHeight());
        sencondTranslationYAnim.setDuration(300);
        sencondTranslationYAnim.start();
        sencondTranslationYAnim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                second.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                button.setClickable(true);
            }
        });
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(firstRotationAnim, firstAlphaAnim, firstScaleXAnim, firstScaleYAnim, sencondTranslationYAnim, firstTranslationYAnim);
        animatorSet.start();
    }
}
佈局程式碼:
<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/first"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/img01"
        android:orientation="vertical">

        <Button
            android:id="@+id/bt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#f00"
            android:onClick="startFirstAnimation"
            android:text="顯示" />
    </LinearLayout>

    <ImageView
        android:id="@+id/second"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:clickable="true"
        android:onClick="startSecondAnimation"
        android:scaleType="fitEnd"
        android:src="@drawable/img02"
        android:visibility="invisible" />
</RelativeLayout>
----------------------完------------------------