1. 程式人生 > >Android動畫之View間漸變

Android動畫之View間漸變

前言

漸變動畫(也叫消失)通常指漸漸的淡出某個 UI 元件,同時同步地淡入另一個。在你 App 想切換內容或 view 的情況下,這種動畫很有用。漸變簡短不易察覺,它也能提供從一個介面到下一個之間流暢的轉換。但當你不使用它們時,轉換經常會感到生硬而倉促。
效果如下圖所示:
這裡寫圖片描述

實現步驟

1.建立view

建立兩個你想相互漸變的 view。下面的例子建立了一個進度提示圈和可滑動文字 view。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width
="match_parent" android:layout_height="match_parent">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView style="?android:textAppearanceMedium"
android:lineSpacingMultiplier="1.2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/lorem_ipsum" android:padding="16dp" />
</ScrollView> <ProgressBar android:id="@+id/loading_spinner"
style="?android:progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" />
</FrameLayout>

2.設定動畫

為設定動畫,你需要:

  • 1.為你想漸變的 view 建立成員變數。在之後動畫應用途中修改 View 的時候你會需要這些引用的。
  • 2.對於被淡入的 view,設定它的 visibility 為 GONE。這樣防止 view 再佔據佈局的空間,而且也能在佈局計算中將其忽略,加速處理過程。
  • 3.將 config_shortAnimTime 系統屬性暫存到一個成員變數裡。這個屬性為動畫定義了一個標準的“短”持續時間。對於細微或者快速發生的動畫,這是個很理想的持續時段。config_longAnimTime 和 config_mediumAnimTime 也行,如果你想用的話。

下面是個使用之前程式碼佈局作為內容 view 的 activity 例子。

public class CrossfadeActivity extends Activity {

    private View mContentView;
    private View mLoadingView;
    private int mShortAnimationDuration;

    ...

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

        mContentView = findViewById(R.id.content);
        mLoadingView = findViewById(R.id.loading_spinner);

        // Initially hide the content view.
        mContentView.setVisibility(View.GONE);

        // Retrieve and cache the system's default "short" animation time.
        mShortAnimationDuration = getResources().getInteger(
                android.R.integer.config_shortAnimTime);
    }

3.漸變View

既然正確地設定了那些 view,做下面這些事情來漸變他們吧:

  • 1.對於正在淡入的 view,設定它的 alpha 值為 0 並且設定 visibility 為 VISIBLE(記住他起初被設定成了 GONE)。這樣就讓 view 可見了但是它是透明的。
  • 2.同樣對於淡入的 view,把 alpha 值從 0 動態改變到 1。同時,對於淡出的 view,把 alpha 值從 1 動態變到 0。
  • 3.使用 Animator.AnimatorListener 中的 onAnimationEnd(),設定淡出 view 的 visibility 為 GONE。即使 alpha 值為 0,也要把 view 的 visibility 設定成 GONE 來防止 view 佔據佈局空間,還能把它從佈局計算中忽略,加速處理過程。

下面方法展示如何做這些:

private View mContentView;
private View mLoadingView;
private int mShortAnimationDuration;

...

private void crossfade() {

    // Set the content view to 0% opacity but visible, so that it is visible
    // (but fully transparent) during the animation.
    mContentView.setAlpha(0f);
    mContentView.setVisibility(View.VISIBLE);

    // Animate the content view to 100% opacity, and clear any animation
    // listener set on the view.
    mContentView.animate()
            .alpha(1f)
            .setDuration(mShortAnimationDuration)
            .setListener(null);

    // Animate the loading view to 0% opacity. After the animation ends,
    // set its visibility to GONE as an optimization step (it won't
    // participate in layout passes, etc.)
    mLoadingView.animate()
            .alpha(0f)
            .setDuration(mShortAnimationDuration)
            .setListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    mLoadingView.setVisibility(View.GONE);
                }
            });
}

相關推薦

Android動畫View漸變

前言 漸變動畫(也叫消失)通常指漸漸的淡出某個 UI 元件,同時同步地淡入另一個。在你 App 想切換內容或 view 的情況下,這種動畫很有用。漸變簡短不易察覺,它也能提供從一個介面到下一個之間流暢的轉換。但當你不使用它們時,轉換經常會感到生硬而倉促。 效

Android 動畫動畫(View Animation)

Android 中補間動畫包括下面四種: 1.透明度動畫 (AlphaAnimation) 2.縮放動畫 (ScaleAnimation) 3.平移動畫 (TranslateAnimation) 4.旋轉動畫 (RotateAnimation) 補間動畫是專門針對V

Android開發:View漸變動畫(例項)

漸變動畫(也叫消失)通常指漸漸的淡出某個UI元件,同時同步地淡入另一個。當App想切換內容或View的情況下,這種動畫很有用。漸變簡短不易察覺,同時又提供從一個介面到下一個之間流暢的轉換。如果在需要轉換的時候沒有使用任何動畫效果,這會使得轉換看上去感到生硬而倉促。在這裡我

Android動畫動畫及其應用場景

前言目錄目錄1. 作用物件檢視控制元件(View)如Android的TextView、Button等等不可作用於View元件的屬性,如:顏色、背景、長度等等2. 原理通過確定開始的檢視樣式 & 結束的檢視樣式、中間動畫變化過程由系統補全來確定一個動畫結束的檢視樣式:平移、縮放、旋轉 & 透明度

android動畫AlphaAnimation(漸變動畫,最簡單的動畫

AlphaAnimation漸變動畫只需要設定從一可見程度,到另一可見程度即可。 示例程式碼: package com.hongchou.www.myalphaanimation; import android.app.Activity; import

Android 動畫ScaleAnimation

android中提供了4中動畫: AlphaAnimation 透明度動畫效果 ScaleAnimation 縮放動畫效果 TranslateAnimation 位移動畫效果 RotateAnimation 旋轉動畫效果 本節講解ScaleAnimation

Android動畫Interpolator插入器

SDK對Interpolator的描述是:An interpolator defines the rate of change of an animation. This allows the basic animation effects (alpha, scale, translate, rot

Android 動畫逐幀動畫小述

一、概述 本節主要介紹下逐幀動畫的基本使用,下面我們就從XML方式建立動畫和程式碼方式建立動畫來分別說下吧 二、逐幀動畫介紹 在開始介紹逐幀動畫之前,先來看一下在本例中我們要實現的逐幀動畫的效果 逐幀動畫,我的理解就是將一幀幀的靜態圖片進行有序的展示,利用人眼的視覺

Android動畫屬性動畫基礎用法

在上一篇Android動畫基礎之補間動畫與逐幀動畫 ,我們複習了Android的基礎動畫Tweened Animation、Frame Animation,同時指出他們的缺陷,改變是View的顯示效果而不是View本身的屬性,還有我們不多不少會看多人家的APP有一些很酷炫的動畫效果,但是

Android動畫Interpolator和AnimationSet(三)

AnimationSet可以加入Animation,加入之後設定AnimationSet對加入的所有Animation都有效。 AnimationSet anim=new AnimationSet(t

Android 動畫屬性動畫

import android.animation.ValueAnimator; import android.animation.ValueAnimator.AnimatorUpdateListener; import android.app.Activity;

Android 動畫ScaleAnimation應用詳解

android中提供了4中動畫: AlphaAnimation 透明度動畫效果 ScaleAnimation 縮放動畫效果 TranslateAnimation 位移動畫效果 RotateAnimation 旋轉動畫效果 本節講解ScaleAnimation 動

Android繪圖RadialGradient 放射漸變(11)

1 RadialGradient 簡介 LinearGradient 和 SweepGradient,這次講解RadialGradient; RadialGradient被稱為放射漸變,就是從中心向外圓形漸變。 兩個建構函式,第一個建構函式可以實現兩種顏色的漸變,第二個建構函式可以實

Android繪圖LinearGradient線性漸變(9)

1 linearGradient簡介 linearGradient線性漸變,會用到Paint的setShader,Shader 被稱為著色器,在opengl中這個概念經常被用到,android中的shader主要用來給影象著色,Shader在繪製過程中會返回橫向重要的顏色組,Pain

Android動畫——圓形進度條加波浪線

效果圖 圓形進度條 public class RecordView extends View { //View預設最小寬度 private static final int DEFAULT_MIN_WIDTH = 500; p

Android探究View的繪制流程

off ice 父類 ati cti break gravity android face Android中Activity是作為應用程序的載體存在,代表著一個完整的用戶界面,提供了一個窗口來繪制各種視圖,當Activity啟動時,我們會通過setContentView方法

Android探究View的繪製流程

Android中Activity是作為應用程式的載體存在,代表著一個完整的使用者介面,提供了一個視窗來繪製各種檢視,當Activity啟動時,我們會通過setContentView方法來設定一個內容檢視,這個內容檢視就是使用者看到的介面。 PhoneWindow是Android系統中最基本的視窗系統,每個Ac

詳解Android動畫Tween Animation

轉載地址:http://blog.csdn.net/liuhe688/article/details/6660823 前面講了動畫中的Frame動畫,今天就來詳細講解一下Tween動畫的使用。 同樣,在開始例項演示之前,先引用官方文件中的一段話:

android 動畫view頂部退出、進入(系列動畫

<translate xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator"

Android GUIView繪製流程

private void performLayout(WindowManager.LayoutParams lp, int desiredWindowWidth, int desiredWindowHeight) { mLayoutRequested =