成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

移動開發指南:Android Transition框架介紹

譯文
移動開發 Android
Android Transition框架允許我們對應用程序用戶界面當中的各類外觀變化加以配置。大家可以在應用程序屏幕內實現動畫式過渡、將每個階段定義為一種場景并控制應用程序如何從一種顯示場景過渡到另一種。

【51CTO譯文】Android Transition框架允許我們對應用程序用戶界面當中的各類外觀變化加以配置。大家可以在應用程序屏幕內實現動畫式過渡、將每個階段定義為一種場景并控制應用程序如何從一種顯示場景過渡到另一種。

在今天的文章中,我們將構建一款簡單的應用程序、并為其制作一套動畫過渡效果。為了完成這項任務,大家需要涉及的內容包括在XML當中準備布局與可繪制文件、而后利用Java配置并應用這套過渡機制。我們將定義兩種場景,其中同樣的一組視圖項目將以不同方式排列在設備屏幕之上。在大家使用Transition框架時,Android將自動完成兩種場景轉換過程中的動畫過渡效果。

1. 創建應用程序

***步

作為教程的***步,我們首先在自己選定的IDE中創建一款新的應用程序。大家至少需要使用SDK 19才能讓這些Transition類順利起效,因此如果打算讓其支持其它早期版本、我們還需要執行其它一些額外步驟。

首先為應用程序指定一個主Activity與布局文件,并為該布局選擇start_layout.xml作為名稱。我們隨后還會添加其它布局文件,并利用Transition機制在不同顯示布局之間進行轉換。下面幾幅截圖顯示了這一過程在Android Studio中的具體實現流程。

第二步

下面我們在Transition中準備一些可繪制圖形以資利用。我們將準備四個圓形圖案,每一個都采用不同的漸變顏色進行填充。在這款示例應用程序的可繪制資源目錄內,首先創建一個名為shape1.xml的新文件。通過以下代碼添加圖形:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:dither="true"  
  4.     android:shape="oval" >  
  5.    
  6.     <gradient  
  7.         android:endColor="#66ff0000"  
  8.         android:gradientRadius="150"  
  9.         android:startColor="#ffffcc00"  
  10.         android:type="radial"  
  11.         android:useLevel="false" />  
  12.    
  13.     <size  
  14.         android:height="100dp"  
  15.         android:width="100dp" />  
  16.    
  17. </shape>  

以上代碼構建出的是一個由漸變色填充而成的圓形圖案。而四個圖形在大小與樣式方面完全相同,僅僅在色彩上有所區別。當然,大家可能需要為不同像素密度的設備分別準備多種不同版本的圖形。利用以下代碼創建shape2.xml:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:dither="true" 
  4.     android:shape="oval" > 
  5.   
  6.     <gradient 
  7.         android:endColor="#66ffcc00" 
  8.         android:gradientRadius="150" 
  9.         android:startColor="#ff00ff00" 
  10.         android:type="radial" 
  11.         android:useLevel="false" /> 
  12.   
  13.     <size 
  14.         android:height="100dp" 
  15.         android:width="100dp" /> 
  16.   
  17. </shape> 

現在添加shape3.xml:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:dither="true" 
  4.     android:shape="oval" > 
  5.   
  6.     <gradient 
  7.         android:endColor="#6600ff00" 
  8.         android:gradientRadius="150" 
  9.         android:startColor="#ff0000ff" 
  10.         android:type="radial" 
  11.         android:useLevel="false" /> 
  12.   
  13.     <size 
  14.         android:height="100dp" 
  15.         android:width="100dp" /> 
  16.   
  17. </shape> 

***添加shape4.xml:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:dither="true" 
  4.     android:shape="oval" > 
  5.   
  6.     <gradient 
  7.         android:endColor="#660000ff" 
  8.         android:gradientRadius="150" 
  9.         android:startColor="#ffff0000" 
  10.         android:type="radial" 
  11.         android:useLevel="false" /> 
  12.   
  13.     <size 
  14.         android:height="100dp" 
  15.         android:width="100dp" /> 
  16.   
  17. </shape> 

我們將把這些圖形作為ImageButtons應用在兩種布局場景之內。

#p#

2. 創建布局場景

***步

接下來,我們要對將在幾種XML布局之間進行過渡的兩類場景進行定義。首先是處理大家在創建應用程序時就已經添加完成的主布局文件,即start_layout.xml。將其打開并切換到XML編輯標簽。利用以下代碼使用RelativeLayout:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     xmlns:tools="http://schemas.android.com/tools" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="match_parent" 
  5.     android:background="#ff000000" 
  6.     android:id="@+id/base" 
  7.     tools:context=".TransitionsActivity"> 
  8.   
  9. </RelativeLayout> 

我們已經為該布局添加了背景顏色與ID。這條ID的作用在于確保Android處理不同場景之間的過渡效果,大家還將在第二個場景中再次使用同樣的ID。當我們在兩個場景之間過渡時,Android會以動畫形式對各場景中擁有同樣ID的視圖進行轉換。如果二者不具備同樣的ID,那么Android系統會將它們視為完全不同的項目、并單純以淡入或者淡出方式處理其過渡效果。

在RelativeLayout當中,為我們之前創建好的每個圖形創建ImageButton:

  1. <ImageButton 
  2.     android:layout_width="wrap_content" 
  3.     android:layout_height="wrap_content" 
  4.     android:id="@+id/btn1" 
  5.     android:src="@drawable/shape1" 
  6.     android:background="#00000000" 
  7.     android:contentDescription="shape" 
  8.     android:layout_alignParentLeft="true" 
  9.     android:layout_alignParentTop="true" 
  10.     android:onClick="changeScene"/> 
  11.   
  12. <ImageButton 
  13.     android:layout_width="wrap_content" 
  14.     android:layout_height="wrap_content" 
  15.     android:id="@+id/btn2" 
  16.     android:src="@drawable/shape2" 
  17.     android:background="#00000000" 
  18.     android:contentDescription="shape" 
  19.     android:layout_alignParentRight="true" 
  20.     android:layout_alignParentTop="true" 
  21.     android:onClick="changeScene"/> 
  22.   
  23. <ImageButton 
  24.     android:layout_width="wrap_content" 
  25.     android:layout_height="wrap_content" 
  26.     android:id="@+id/btn3" 
  27.     android:src="@drawable/shape3" 
  28.     android:background="#00000000" 
  29.     android:contentDescription="shape" 
  30.     android:layout_alignParentLeft="true" 
  31.     android:layout_alignParentBottom="true" 
  32.     android:onClick="changeScene"/> 
  33.   
  34. <ImageButton 
  35.     android:layout_width="wrap_content" 
  36.     android:layout_height="wrap_content" 
  37.     android:id="@+id/btn4" 
  38.     android:src="@drawable/shape4" 
  39.     android:background="#00000000" 
  40.     android:contentDescription="shape" 
  41.     android:layout_alignParentRight="true" 
  42.     android:layout_alignParentBottom="true" 
  43.     android:onClick="changeScene"/> 

需要注意的是,每一個圖形按鈕都擁有自己的ID——我們創建的第二套布局當然也是如此——外加onClick屬性。我們隨后將把這一方法添加到主Activity當中并在用戶點擊任意圖形時啟動過渡流程。

現在我們將在IDE當中看到整套布局的預覽圖,不過在某些情況下大家要真正在設備或者模擬器上運行該應用才能看到其漸變以及/或者透明效果。這些圖形被排列在屏幕的四個邊角位置,具體效果如下圖所示。

第二步

我們創建的***套布局方案將顯示為過渡流程的起始狀態。現在讓我們為場景創建第二個布局文件,并將其作為過渡流程的結束狀態。在我們的應用程序布局資源目錄中添加一個新文件,將其命名為end_layout.xml。切換到文本編輯標簽并輸入以下代碼:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     xmlns:tools="http://schemas.android.com/tools" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="match_parent" 
  5.     android:background="#ff000000" 
  6.     android:id="@+id/base" 
  7.     tools:context=".TransitionsActivity"> 
  8.   
  9.     <ImageButton 
  10.         android:layout_width="wrap_content" 
  11.         android:layout_height="wrap_content" 
  12.         android:id="@+id/btn1" 
  13.         android:src="@drawable/shape1" 
  14.         android:background="#00000000" 
  15.         android:contentDescription="shape" 
  16.         android:layout_alignParentRight="true" 
  17.         android:layout_alignParentBottom="true" 
  18.         android:onClick="changeScene"/> 
  19.   
  20.     <ImageButton 
  21.         android:layout_width="wrap_content" 
  22.         android:layout_height="wrap_content" 
  23.         android:id="@+id/btn2" 
  24.         android:src="@drawable/shape2" 
  25.         android:background="#00000000" 
  26.         android:contentDescription="shape" 
  27.         android:layout_alignParentLeft="true" 
  28.         android:layout_alignParentBottom="true" 
  29.         android:onClick="changeScene"/> 
  30.   
  31.     <ImageButton 
  32.         android:layout_width="wrap_content" 
  33.         android:layout_height="wrap_content" 
  34.         android:id="@+id/btn3" 
  35.         android:src="@drawable/shape3" 
  36.         android:background="#00000000" 
  37.         android:contentDescription="shape" 
  38.         android:layout_alignParentRight="true" 
  39.         android:layout_alignParentTop="true" 
  40.         android:onClick="changeScene"/> 
  41.   
  42.     <ImageButton 
  43.         android:layout_width="wrap_content" 
  44.         android:layout_height="wrap_content" 
  45.         android:id="@+id/btn4" 
  46.         android:src="@drawable/shape4" 
  47.         android:background="#00000000" 
  48.         android:contentDescription="shape" 
  49.         android:layout_alignParentLeft="true" 
  50.         android:layout_alignParentTop="true" 
  51.         android:onClick="changeScene"/> 
  52.   
  53. </RelativeLayout> 

現在讓我們花點時間審視以上布局代碼。除了各個圖形按鈕的位置之外,它與***套布局完全相同。每個圖形都從起始位置被移動到了其對角線處。過渡流程將因此而對各圖形進行位置互換,也就是將其引導至屏幕上的對角位置。

#p#

3. 不同場景之間進行過渡

***步

現在我們已經對兩套布局進行了定義,現在要做的就是利用過渡機制完成二者之間的移動流程。打開應用程序中的主Activity類。大家將需要使用以下導入語句:

  1. import android.transition.AutoTransition;  
  2. import android.transition.Scene;  
  3. import android.transition.Transition;  
  4. import android.view.View;  
  5. import android.view.ViewGroup;  
  6. import android.view.animation.AccelerateDecelerateInterpolator;  
  7. import android.widget.RelativeLayout;  
  8. import android.transition.TransitionManager;  

在Activity類聲明中、onCreate方法之前處,我們需要添加以下實例變量以應用該過渡機制:

  1. //scenes to transition 
  2. private Scene scene1, scene2; 
  3. //transition to move between scenes 
  4. private Transition transition; 
  5. //flag to swap between scenes 
  6. private boolean start; 

第二步

現在讓我們為過渡做好準備,整個流程將在用戶點擊任意圖形時正式開始。在onCreate中,我們要在IDE已經輸入的現有代碼之后添加以下內容:

  1. //get the layout ID 
  2. RelativeLayout baseLayout = (RelativeLayout)findViewById(R.id.base); 
  3.   
  4. //first scene 
  5. ViewGroup startViews = (ViewGroup)getLayoutInflater() 
  6.     .inflate(R.layout.start_layout, baseLayout, false); 
  7.   
  8. //second scene 
  9. ViewGroup endViews = (ViewGroup)getLayoutInflater() 
  10.     .inflate(R.layout.end_layout, baseLayout, false); 

我們首先需要對基礎場景進行定義,也就是我們在兩個場景布局文件內為所包含布局設定的ID。接下來,我們還需要定義作為過渡流程起始與結束狀態的兩個場景,為其指定布局文件名稱以及所包含基礎場景。通過這種方式,Android將能夠根據我們的需要在兩個場景之間進行過渡、并將不同場景下具備相同ID的任意視圖元素作為同一對象加以處理,這樣場景切換時就能顯示出動畫式的變化效果。

接下來,我們定義作為過渡流程起始與結束狀態的兩個場景,仍然是在onCreate當中:

  1. //create the two scenes 
  2. scene1 = new Scene(baseLayout, startViews); 
  3. scene2 = new Scene(baseLayout, endViews); 

我們要將基礎布局與相關場景布局傳遞至每一個構造函數當中。現在我們已經可以在定義過渡流程時引用這些場景了。

第三步

下面我們作好執行過渡的準備,仍然是在onCreate當中:

  1. //create transition, set properties 
  2. transition = new AutoTransition(); 
  3. transition.setDuration(5000); 
  4. transition.setInterpolator(new AccelerateDecelerateInterpolator()); 
  5.   
  6. //initialize flag 
  7. start=true

Android提供了一系列過渡類型可供選擇,大家可以根據自己需要的場景變化方式采用其中的不同動畫效果。在今天的示例當中,我們選擇的是AutoTransition,因此Android會計算如何以兩種變化場景的屬性為基礎實現過渡。感興趣的朋友也可以點擊此處查看更多與Transition引用相關的選項。

我們為過渡流程設置了持續時間與內插程序。大家也可以根據需要為整套變化機制設定啟動延時。***,我們通過初始化將布爾標記設定為true。為了簡便起見,我們將采取點擊任意圖形來激活場景切換的方式,但這只是為了演示示例所具備的實際功能。

第四步

大家一定還記得,我們在創建布局XML文件時已經將onClick屬性添加到圖形按鈕當中。現在我們要將該方法添加到Activity內:

  1. public void changeScene(View v){ 
  2.   
  3.     //check flag 
  4.     if(start) { 
  5.         TransitionManager.go(scene2, transition); 
  6.         start=false
  7.     } 
  8.     else { 
  9.         TransitionManager.go(scene1, transition); 
  10.         start=true
  11.     } 

我們利用Activity實現從當前場景向另一套場景的過渡,其中布爾標記會持續追蹤我們當前正在使用的場景類型。我們還指定了此前已經創建完成的Transition對象,旨在保證切換的執行過程與預期效果相匹配。

現在大家應該已經可以運行自己的應用程序,并在點擊任意圖形時查看到整個過渡流程。每當我們執行點擊操作時,過渡流程就會將各個圖形緩慢移動到屏幕上的對角線位置,再次點擊則可使其回歸原位。

內容總結

在今天的文章中,我們事實上還只是初步了解了自己能夠利用Android Transition框架實現怎樣的設計方案與過渡效果。要在自己的應用程序中引入更多過渡機制,大家可以點擊此處查看TransitionManager類當中的其它方法,其中包括beginDelayedTransition與transitionTo。此外,大家也不妨嘗試利用TransitionSet將多種過渡機制結合在一起,例如同時使用來自不同過渡機制的漸變與移動效果。根據過渡機制復雜程度的不同,大家可能還需要用到TransitionValues類,它能夠提供與對應過渡相關的數據值引用能力。如果各位還想了解更多與場景處理相關的技術手段,也可以點擊此處查看Scene類的相關說明。

原文鏈接:An Introduction to Android Transitions

核子可樂譯

責任編輯:閆佳明 來源: 51CTO譯文
相關推薦

2014-06-12 09:35:25

設備定向API移動開發

2011-12-29 10:48:49

移動Web

2011-08-02 17:58:09

iPhone開發 事件

2011-11-29 16:38:58

Knockout

2014-05-16 11:09:38

Handlebars模板引擎

2012-05-18 10:08:56

TitaniumAndroid

2023-05-08 15:59:27

UI自動化腳本鴻蒙

2013-10-09 09:10:28

移動應用開發NativeHybrid

2011-07-25 16:21:22

Sencha touc

2017-02-22 16:51:11

移動·開發技術周刊

2022-08-02 08:01:09

開發插件Chrome前端技術

2015-03-10 10:59:18

Node.js開發指南基礎介紹

2022-12-25 10:53:47

2011-06-09 18:24:36

QT Wince

2012-03-26 09:27:40

谷歌安卓開發谷歌安卓

2023-05-15 18:44:07

前端開發

2021-03-31 09:50:25

鴻蒙HarmonyOS應用開發

2009-06-24 16:30:21

JSF組件模型

2017-02-05 09:13:58

PHP Cake框架構建

2015-11-12 16:14:52

Python開發實踐
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 欧美性久久 | 亚洲一区二区三区四区五区午夜 | 精品九九九 | 国产高清在线精品一区二区三区 | 中文在线一区 | 蜜臀av日日欢夜夜爽一区 | 爱操av| 国产高清视频 | 久久久这里只有17精品 | 欧美一区二区在线观看 | 国产精品免费一区二区三区四区 | 免费一区二区三区 | 欧美日本免费 | 综合色播 | 亚洲欧美aⅴ| 黄色大片视频 | va精品 | 精品免费国产视频 | 午夜影院视频在线观看 | 精品成人佐山爱一区二区 | 盗摄精品av一区二区三区 | 日韩色综合 | 精品在线视频播放 | 日本韩国欧美在线观看 | 毛片99 | 韩日一区二区三区 | 国产精品中文字幕在线观看 | 色在线免费 | 一区二区三区国产在线观看 | 亚洲成人精品久久久 | 欧美日韩精品中文字幕 | 天天干夜夜操 | 亚洲人成人一区二区在线观看 | 一区二区免费看 | 国产精品美女久久久久 | 亚洲风情在线观看 | 97国产超碰 | 天天操夜夜艹 | 国产高清免费 | 91中文字幕在线观看 | 亚洲精品www |