Creating Apps With Material Design —— Defining Custom Animations

來源:互聯網
上載者:User

標籤:

轉載請註明 http://blog.csdn.net/eclipsexys 翻譯自Developer Android,時間倉促,有翻譯問題請留言指出。謝謝


定義動畫

在材料設計動畫讓使用者與您的應用程式進行互動時,為他們的行為提供反饋。並提供可視化的連續性。

該材料的主題提供了一些預設的動畫button和活動過渡,而Android5.0(API等級21)以上,您能夠自己定義這些動畫和建立新的: 


    觸摸反饋 
    通告顯示 
    活動轉變 
    曲線運動 
    檢視狀態更改 

自己定義觸摸反饋 
觸摸反饋在材料設計提供了一種瞬時視覺確認在接觸點上,當使用者與使用者介面元素進行互動。預設的觸摸反饋的動畫button,使用新的RippleDrawable類來實現不同狀態之間的轉換與產生連鎖反應動畫。

 

在大多數情況下,你應該通過指定視圖背景。在視圖中的XML應用此功能:


    ?android:attr/selectableItemBackground for a bounded ripple    ?

android:attr/selectableItemBackgroundBorderless for a ripple that extends beyond the view


或者,你能夠定義一個RippleDrawable為使用波紋元素的XML資源。 

您能夠指定一種顏色RippleDrawable對象。要更改預設的觸摸反饋的顏色,使用的主題的android:colorControlHighlight屬性。


使用Reveal Effect

Reveal動畫為使用者提供視覺的連續性,當您顯示或隱藏一組UI元素。該ViewAnimationUtils.createCircularReveal()方法。您能夠設定動畫clipping circle來顯示或隱藏視圖。

 

要使用此效果顯示先前不可見的view:

// previously invisible viewView myView = findViewById(R.id.my_view);// get the center for the clipping circleint cx = (myView.getLeft() + myView.getRight()) / 2;int cy = (myView.getTop() + myView.getBottom()) / 2;// get the final radius for the clipping circleint finalRadius = myView.getWidth();// create and start the animator for this view// (the start radius is zero)Animator anim =    ViewAnimationUtils.createCircularReveal(myView, cx, cy, 0, finalRadius);anim.start();

要使用此效果隱藏先前看到的view:

// previously visible viewfinal View myView = findViewById(R.id.my_view);// get the center for the clipping circleint cx = (myView.getLeft() + myView.getRight()) / 2;int cy = (myView.getTop() + myView.getBottom()) / 2;// get the initial radius for the clipping circleint initialRadius = myView.getWidth();// create the animation (the final radius is zero)Animator anim =    ViewAnimationUtils.createCircularReveal(myView, cx, cy, initialRadius, 0);// make the view invisible when the animation is doneanim.addListener(new AnimatorListenerAdapter() {    @Override    public void onAnimationEnd(Animator animation) {        super.onAnimationEnd(animation);        myView.setVisibility(View.INVISIBLE);    }});// start the animationanim.start();


自己定義Activity transitions

在材料設計應用程式的Activity通過運動來進行不同狀態之間的轉換。

您能夠指定自己定義動畫的進入和退出的過渡和Activity之間共用內容的轉換。 

Android5.0(API層級21)支援這些進入和退出的轉換: 

    爆炸 - 從現場的中心移動的view。 
    投影片 - 行動裝置檢視或從情境的邊緣。 
    褪色 - 通過改變其透明度加入或刪除情境視圖。 

transition擴充了能見度類的不論什麼變化都支援作為進入或退出轉型。欲瞭解很多其它資訊,請參閱該轉換類的API參考。 

Android5.0(API層級21)也支援這些共同的元素轉換: 

    changeBounds - 動畫處理目標View布局界限。

 
    changeClipBounds - 動畫處理目標View地區剪輯。

 
    changeTransform - 動畫處理目標View縮放和旋轉。 
    changeImageTransform - 動畫處理改變靶心圖表像的大小和比例。 

當您在應用程啟用Activity轉變時,預設以交叉漸層過渡的進入和退出Activity的啟動。



指定自己定義的轉換 
首先。當你定義一個風格,繼承了材料的主題屬性使表單內容轉換 Android:windowContentTransitions。您也能夠指定進入。退出,並指定您定義的樣式共用元素的轉換:

<style name="BaseAppTheme" parent="android:Theme.Material">  <!-- enable window content transitions -->  <item name="android:windowContentTransitions">true</item>  <!-- specify enter and exit transitions -->  <item name="android:windowEnterTransition">@transition/explode</item>  <item name="android:windowExitTransition">@transition/explode</item>  <!-- specify shared element transitions -->  <item name="android:windowSharedElementEnterTransition">    @transition/change_image_transform</item>  <item name="android:windowSharedElementExitTransition">    @transition/change_image_transform</item></style>

在這個範例中。change_image_transform過渡的定義例如以下:

<!-- res/transition/change_image_transform.xml --><!-- (see also Shared Transitions below) --><transitionSet xmlns:android="http://schemas.android.com/apk/res/android">  <changeImageTransform/></transitionSet>

該changeImageTransform元素相應於ChangeImageTransform類。欲瞭解很多其它資訊,請參閱轉換的API參考。 

為確保表單移動動畫實現,須要調用Window.requestFeature()方法:

// inside your activity (if you did not enable transitions in your theme)getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);// set an exit transitiongetWindow().setExitTransition(new Explode());

要指定你的代碼轉換,調用這些方法與Transition對象: 

    Window.setEnterTransition() 
    Window.setExitTransition() 
    Window.setSharedElementEnterTransition() 
    Window.setSharedElementExitTransition() 

該setExitTransition()和setSharedElementExitTransition()方法定義為調用活動的退出過渡。

該setEnterTransition()和setSharedElementEnterTransition()方法定義了稱為活動的輸入過渡。

 

為了得到一個過渡的完整效果。您必須啟用這兩個主叫和被叫的活動表單中的內容轉換。否則。調用活動將啟動退出過渡,但隨後你會看到一個表單的過渡(如規模或褪色)。 

開始儘快的進入過渡,使用Window.setAllowEnterTransitionOverlap()方法被調用的Activity。這讓你有很多其它戲劇性的進入過渡動畫。 


使用轉換開始活動 
假設啟用轉換並設定為Activity的退出過渡,當您啟動還有一個Activity時。例如以下的轉變被啟用:

startActivity(intent,              ActivityOptions.makeSceneTransitionAnimation(this).toBundle());

假設您設定了進入轉型的第二個活動,過渡也是該活動開始時啟用。

要禁用轉換,當你開始還有一項活動,提供了一個null的選項。

 

實現一個共同的元素之間的螢幕過渡動畫
    讓你的主題表單的內容轉換。 
    指定你的風格的共用元素的過渡。 
    定義轉換為XML資源。 
    指定一個共同的名字在兩個布局與Android的共用元素:transitionName屬性。

 
    使用ActivityOptions.makeSceneTransitionAnimation()方法。

// get the element that receives the click eventfinal View imgContainerView = findViewById(R.id.img_container);// get the common element for the transition in this activityfinal View androidRobotView = findViewById(R.id.image_small);// define a click listenerimgContainerView.setOnClickListener(new View.OnClickListener() {    @Override    public void onClick(View view) {        Intent intent = new Intent(this, Activity2.class);        // create the transition animation - the images in the layouts        // of both activities are defined with android:transitionName="robot"        ActivityOptions options = ActivityOptions            .makeSceneTransitionAnimation(this, androidRobotView, "robot");        // start the new activity        startActivity(intent, options.toBundle());    }});

對於您的代碼產生共用動態視圖。使用View.setTransitionName()方法在兩個Activity中指定一個共同的元素名稱。 

為了扭轉情境過渡動畫,當你完畢了第二個活動,叫Activity.finishAfterTransition()方法。而不是Activity.finish()。 


啟動有多個共用的元素的Activity 
為了使兩項活動有多個共用的元素,定義了兩種布局方式與Android的共用元素之間的情境過渡動畫:transitionName屬性(或使用View.setTransitionName()在這兩個活動的方法),並建立一個ActivityOptions對象例如以下:

ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(this,        Pair.create(view1, "agreedName1"),        Pair.create(view2, "agreedName2"));

使用Curves Motion

在材料設計的動畫依賴於曲線插補時間和空間上的運動模式。

採用Android5.0(API等級21)以上,則能夠定義自己定義定時曲線和曲線運動模式的動畫。 

該PathInterpolator類是基於貝茲路徑或路徑對象上的新插值。該插補指定一個1x1正方形的運動曲線。用(0,0)錨點和(1,1)和控制點使用建構函式的參數指定。

您也能夠定義一個路徑插補為XML資源:

<pathInterpolator xmlns:android="http://schemas.android.com/apk/res/android"    android:controlX1="0.4"    android:controlY1="0"    android:controlX2="1"    android:controlY2="1"/>

該系統提供的XML資源中的材料設計規範的三個基本曲線: 

    @interpolator/fast_out_linear_in.xml    @interpolator/fast_out_slow_in.xml    @interpolator/linear_out_slow_in.xml


你能夠通過一個PathInterpolator對象的Animator.setInterpolator()方法。

 

該ObjectAnimator類有新的建構函式。使您能夠同一時候使用兩種或兩種以上的屬性,在一次路徑動畫座標。比如,以下的動畫師使用Path對象進行動畫視圖的x和y屬性:

ObjectAnimator mAnimator;mAnimator = ObjectAnimator.ofFloat(view, View.X, View.Y, path);...mAnimator.start();

視圖動畫狀態改變

該StateListAnimator類能夠定義動畫執行時的檢視狀態發生改變。

以下的示範範例示範怎樣為XML資源定義一個StateListAnimator:

<!-- animate the translationZ property of a view when pressed --><selector xmlns:android="http://schemas.android.com/apk/res/android">  <item android:state_pressed="true">    <set>      <objectAnimator android:propertyName="translationZ"        android:duration="@android:integer/config_shortAnimTime"        android:valueTo="2dp"        android:valueType="floatType"/>        <!-- you could have other objectAnimator elements             here for "x" and "y", or other properties -->    </set>  </item>  <item android:state_enabled="true"    android:state_pressed="false"    android:state_focused="true">    <set>      <objectAnimator android:propertyName="translationZ"        android:duration="100"        android:valueTo="0"        android:valueType="floatType"/>    </set>  </item></selector>

要高度自己定義的檢視狀態動畫視圖,定義使用XML資源檔裡選擇元素在這個範例中的動畫,並將其分配給您的視圖與Android:stateListAnimator屬性。在代碼分配一個狀態表動畫到一個視圖中,使用AnimationInflater.loadStateListAnimator()方法,以及動畫分配給你的View與View.setStateListAnimator()方法。 

當你的主題擴充了材料的主題,button都為Z動畫預設。為了避免你的button此問題,設定了android:stateListAnimator屬性來@null。

 

該AnimatedStateListDrawable類用於建立,顯示相關的檢視狀態更改的動畫可繪。

預設情況下,一些安卓5.0系統組件的使用這些動畫。以下的示範範例示範怎樣為XML資源定義一個AnimatedStateListDrawable:

<!-- res/drawable/myanimstatedrawable.xml --><animated-selector    xmlns:android="http://schemas.android.com/apk/res/android">    <!-- provide a different drawable for each state-->    <item android:id="@+id/pressed" android:drawable="@drawable/drawableP"        android:state_pressed="true"/>    <item android:id="@+id/focused" android:drawable="@drawable/drawableF"        android:state_focused="true"/>    <item android:id="@id/default"        android:drawable="@drawable/drawableD"/>    <!-- specify a transition -->    <transition android:fromId="@+id/default" android:toId="@+id/pressed">        <animation-list>            <item android:duration="15" android:drawable="@drawable/dt1"/>            <item android:duration="15" android:drawable="@drawable/dt2"/>            ...        </animation-list>    </transition>    ...</animated-selector>


Animate Vector Drawables

向量可繪製具有可擴充性又不失清晰。該AnimatedVectorDrawable類,您能夠設定動畫的向量繪製的屬性。

 

你通常在三個XML檔案裡定義動畫向量可繪製對象: 

    A vector drawable with the <vector> element in res/drawable/    An animated vector drawable with the <animated-vector> element in res/drawable/    One or more object animators with the <objectAnimator> element in res/anim/


向量動畫可繪製對象能夠動畫的< group>的屬性和< path>元素。 <group>元素定義了一組路徑或小組,並在< path>元素定義要繪製的路徑。

 

當你定義你想要的動畫向量繪製,使用android:name屬性為唯一的名稱分配給組和路徑。這樣你就能夠把它們從你的動畫定義。比如:

<!-- res/drawable/vectordrawable.xml --><vector xmlns:android="http://schemas.android.com/apk/res/android"    android:height="64dp"    android:width="64dp"    android:viewportHeight="600"    android:viewportWidth="600">    <group        android:name="rotationGroup"        android:pivotX="300.0"        android:pivotY="300.0"        android:rotation="45.0" >        <path            android:name="v"            android:fillColor="#000000"            android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" />    </group></vector>

動畫繪製向量的定義是指通過他們的名字向量繪製的組和路徑:

<!-- res/drawable/animvectordrawable.xml --><animated-vector xmlns:android="http://schemas.android.com/apk/res/android"  android:drawable="@drawable/vectordrawable" >    <target        android:name="rotationGroup"        android:animation="@anim/rotation" />    <target        android:name="v"        android:animation="@anim/path_morph" /></animated-vector>

動畫的定義代表ObjectAnimator或AnimatorSet對象。在這個範例中,第一動畫旋轉目標組360度:

<!-- res/anim/rotation.xml --><objectAnimator    android:duration="6000"    android:propertyName="rotation"    android:valueFrom="0"    android:valueTo="360" />

在本實施例中的第二動畫搖身一變載體可展開的路徑從一個形狀到還有一種。

兩個路徑必須是變形相容:它們必須具有同樣數目的命令和每一個命令的參數的數量同樣。


<!-- res/anim/path_morph.xml --><set xmlns:android="http://schemas.android.com/apk/res/android">    <objectAnimator        android:duration="3000"        android:propertyName="pathData"        android:valueFrom="M300,70 l 0,-70 70,70 0,0   -70,70z"        android:valueTo="M300,70 l 0,-70 70,0  0,140 -70,0 z"        android:valueType="pathType" /></set>

ps:path畫畫,編寫更複雜的路徑,需要使用SVG編輯

Creating Apps With Material Design —— Defining Custom Animations

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.