自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Material Design中全新的動(dòng)畫

移動(dòng)開發(fā) Android
Material Design中的動(dòng)畫將為用戶提供操作反饋并在用戶與您的應(yīng)用進(jìn)行互動(dòng)時(shí)提供視覺連續(xù)性。 Material Design將為按鈕與操作行為轉(zhuǎn)換提供一些默認(rèn)動(dòng)畫,而 Android 5.0(API Level 21)及更高版本可讓您定制這些動(dòng)畫,同時(shí)也可創(chuàng)建新動(dòng)畫。

Material Design中的動(dòng)畫將為用戶提供操作反饋并在用戶與您的應(yīng)用進(jìn)行互動(dòng)時(shí)提供視覺連續(xù)性。 Material Design將為按鈕與操作行為轉(zhuǎn)換提供一些默認(rèn)動(dòng)畫,而 Android 5.0(API Level 21)及更高版本可讓您定制這些動(dòng)畫,同時(shí)也可創(chuàng)建新動(dòng)畫:

一、觸摸反饋動(dòng)畫

效果圖: 

 

 

 

Material Design的觸摸反饋可在用戶與 UI 元素互動(dòng)時(shí),在接觸點(diǎn)上提供即時(shí)視覺確認(rèn)。 適用于按鈕的默認(rèn)觸摸動(dòng)畫使用全新 RippleDrawable類別,以波紋效果實(shí)現(xiàn)不同狀態(tài)間的轉(zhuǎn)換。

在大多數(shù)情況下,應(yīng)以下列方式指定視圖背景,在您的視圖 XML 中應(yīng)用此功能:

  • android:attr/selectableItemBackground 指定有界的波紋。
  • android:attr/selectableItemBackgroundBorderless 指定越過視圖邊界的波紋。 它將由一個(gè)非空背景的視圖的最近父項(xiàng)所繪制和設(shè)定邊界。

任何view處于可點(diǎn)擊狀態(tài),都可以使用RippleDrawable來達(dá)到水波紋特效,而且必須處于可點(diǎn)擊狀態(tài),才會(huì)出現(xiàn)波紋動(dòng)畫效果。

在代碼中可以這樣設(shè)置: 

 

 

 

注意:selectableItemBackgroundBorderless是 API Level 21 中推出的新屬性。

此外,您可利用 ripple元素將 RippleDrawable定義為一個(gè) XML 資源。

您可以為 RippleDrawable對(duì)象指定一種顏色。如果要改變默認(rèn)觸摸反饋顏色,請(qǐng)使用主題的 android:colorControlHighlight屬性。

如果要了解更多信息,請(qǐng)參閱 RippleDrawable類別的 API 參考文檔。

我們來看看系統(tǒng)自帶的觸摸反饋動(dòng)畫是怎么實(shí)現(xiàn)的,為什么只需要在view的background或者foreground屬性設(shè)置成?android:attr/selectableItemBackground或者?android:attr/selectableItemBackgroundBorderless就可以實(shí)現(xiàn)波紋動(dòng)畫的效果?這兩個(gè)屬性點(diǎn)進(jìn)去,可以看到在路徑sdk/platforms/android-xx/data/res/values/attrs.xml文件中有定義這么兩個(gè)屬性:

  1. <!-- Background drawable for bordered standalone items that need focus/pressed states. --> 
  2.  
  3. <attr name="selectableItemBackground" format="reference" /> 
  4.  
  5. <!-- Background drawable for borderless standalone items that need focus/pressed states. --> 
  6.  
  7. <attr name="selectableItemBackgroundBorderless" format="reference" />  

我們想到,這兩個(gè)屬性既然是整個(gè)app中有效的,那可能會(huì)是在Theme中的屬性吧,那就去AndroidManifest文件中跟這個(gè)Theme一步步看下去,***在Base.V21.Theme.AppCompat.Light這個(gè)style中看到確實(shí)是有這兩個(gè)item屬性:

  1. <item name="selectableItemBackground">?android:attr/selectableItemBackground</item> 
  2.  
  3. <item name="selectableItemBackgroundBorderless">?android:attr/selectableItemBackgroundBorderless</item>  

但是這里還是調(diào)用的系統(tǒng)的定義的屬性,繼續(xù)往下追,在android:Theme.Material和android:Theme.Material.Light中,可以看到:

  1. <item name="selectableItemBackground">@drawable/item_background_material</item> 
  2.  
  3. <item name="selectableItemBackgroundBorderless">@drawable/item_background_borderless_material</item>  

然后sdk路徑下platforms\\android-xx\\data\\res\\drawable可以找到這些資源文件如下圖: 

 

 

 

item_background_material的內(nèi)容是:

  1. <ripple xmlns:android="http://schemas.android.com/apk/res/android" 
  2.  
  3.     android:color="?attr/colorControlHighlight"
  4.  
  5.     <item android:id="@id/mask"
  6.  
  7.         <color android:color="@color/white" /> 
  8.  
  9.     </item> 
  10.  
  11. </ripple>  

item_background_borderless_material的內(nèi)容是:

  1. <ripple xmlns:android="http://schemas.android.com/apk/res/android" 
  2.  
  3.     android:color="?attr/colorControlHighlight" />  

系統(tǒng)的做法是用ripple元素將 RippleDrawable定義為一個(gè) XML 資源,而通過看View的源碼中在構(gòu)造方法中是這樣獲取background屬性的:

  1. public View(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
  2.  
  3.         this(context); 
  4.  
  5.  
  6.         final TypedArray a = context.obtainStyledAttributes( 
  7.  
  8.                 attrs, com.android.internal.R.styleable.View, defStyleAttr, defStyleRes); 
  9.  
  10.  
  11.         if (mDebugViewAttributes) { 
  12.  
  13.             saveAttributeData(attrs, a); 
  14.  
  15.         } 
  16.  
  17.  
  18.         Drawable background = null
  19.  
  20.  
  21.         switch (attr) { 
  22.  
  23.             case com.android.internal.R.styleable.View_background: 
  24.  
  25.                 background = a.getDrawable(attr); 
  26.  
  27.                 break; 
  28.  
  29.         . 
  30.  
  31.         . 
  32.  
  33.         . 
  34.  
  35.  

也就是說,這個(gè)background實(shí)際上就是RippleDrawable類。那我們就來看看這個(gè)RippleDrawable內(nèi)部到底是怎么做的吧。

首先官方文檔對(duì)RippleDrawable解釋是

Drawable that shows a ripple effect in response to state changes. The anchoring position of the ripple for a given state may be specified by calling setHotspot(float, float)with the corresponding state attribute identifier.

通過顯示出波紋效果來響應(yīng)狀態(tài)的改變,對(duì)于給定狀態(tài)的波紋的錨定位置可以通過調(diào)用具有對(duì)應(yīng)的狀態(tài)屬性標(biāo)識(shí)符的setHotspot(float,float)來指定。

RippleDrawable繼承自LayerDrawable,而LayerDrawable是繼承Drawable,RippleDrawable又是為了響應(yīng)View的statechange,那就看看Drawable類中對(duì)點(diǎn)擊時(shí)的狀態(tài)處理吧。

  1. public boolean setState(@NonNull final int[] stateSet) { 
  2.  
  3.     if (!Arrays.equals(mStateSet, stateSet)) { 
  4.  
  5.         mStateSet = stateSet; 
  6.  
  7.         return onStateChange(stateSet); 
  8.  
  9.     } 
  10.  
  11.     return false
  12.  
  13.  

給Drawable設(shè)置狀態(tài)屬性時(shí),會(huì)把狀態(tài)的數(shù)組傳給onStateChange方法,在RippleDrawable中重寫了onStateChange。 

 

 

 

看到setRippleActive和setBackgroundActive這兩個(gè)方法應(yīng)該可以猜到是什么意思了,接著看。

  1. private void setRippleActive(boolean active) { 
  2.  
  3.     if (mRippleActive != active) { 
  4.  
  5.         mRippleActive = active; 
  6.  
  7.         if (active) { 
  8.  
  9.             tryRippleEnter(); 
  10.  
  11.         } else { 
  12.  
  13.             tryRippleExit(); 
  14.  
  15.         } 
  16.  
  17.     } 
  18.  
  19.  

如果Drawable是enable=true且pressd=true時(shí),會(huì)調(diào)用tryRippleEnter方法 

 

 

 

看到這里,我們可以知道要開始做波紋動(dòng)畫的效果了。mRipple 是RippleForeground類的實(shí)例,然而我沒有在RippleForeground類中找到setup和enter方法,但是RippleForeground繼承自RippleComponent類,于是,我在這個(gè)類中發(fā)現(xiàn)了這兩個(gè)方法。

  1. public final void setup(float maxRadius, int densityDpi) { 
  2.  
  3.     if (maxRadius >= 0) { 
  4.  
  5.         mHasMaxRadius = true
  6.  
  7.         mTargetRadius = maxRadius; 
  8.  
  9.     } else { 
  10.  
  11.         mTargetRadius = getTargetRadius(mBounds); 
  12.  
  13.     } 
  14.  
  15.  
  16.     mDensityScale = densityDpi * DisplayMetrics.DENSITY_DEFAULT_SCALE; 
  17.  
  18.  
  19.     onTargetRadiusChanged(mTargetRadius); 
  20.  
  21.  

setup是初始化一系列參數(shù),enter創(chuàng)建一個(gè)動(dòng)畫并開始動(dòng)畫。 

 

 

 

從上面創(chuàng)建動(dòng)畫的代碼可以看到,實(shí)際上是一個(gè)組合的屬性動(dòng)畫,然后自定義了三個(gè)屬性波紋半徑TWEEN_RADIUS、波紋中心點(diǎn)TWEEN_ORIGIN和波紋的不透明度OPACITY。通過這三個(gè)屬性的過渡變化得到一個(gè)復(fù)合的動(dòng)畫。以上就是前景波紋動(dòng)畫效果的實(shí)現(xiàn)過程。

  1. private void setBackgroundActive(boolean active, boolean focused) { 
  2.  
  3.     if (mBackgroundActive != active) { 
  4.  
  5.         mBackgroundActive = active; 
  6.  
  7.         if (active) { 
  8.  
  9.             tryBackgroundEnter(focused); 
  10.  
  11.         } else { 
  12.  
  13.             tryBackgroundExit(); 
  14.  
  15.         } 
  16.  
  17.     } 
  18.  
  19.  

mBackground是RippleBackground類的實(shí)例,與RippleForeground不同的是,背景動(dòng)畫只是改變了不透明度。

  1. @Override 
  2.  
  3. protected Animator createSoftwareEnter(boolean fast) { 
  4.  
  5.     // Linear enter based on current opacity. 
  6.  
  7.     final int maxDuration = fast ? OPACITY_ENTER_DURATION_FAST : OPACITY_ENTER_DURATION; 
  8.  
  9.     final int duration = (int) ((1 - mOpacity) * maxDuration); 
  10.  
  11.  
  12.     final ObjectAnimator opacity = ObjectAnimator.ofFloat(this, OPACITY, 1); 
  13.  
  14.     opacity.setAutoCancel(true); 
  15.  
  16.     opacity.setDuration(duration); 
  17.  
  18.     opacity.setInterpolator(LINEAR_INTERPOLATOR); 
  19.  
  20.  
  21.     return opacity; 
  22.  
  23.  

以上分析的都是手指觸摸view時(shí)產(chǎn)生的enter波紋動(dòng)畫,當(dāng)手指抬起時(shí)state也會(huì)改變,會(huì)產(chǎn)生一個(gè)exit動(dòng)畫,這里就不詳細(xì)分析了。

二、使用揭露效果

效果圖: 

 

 

 

當(dāng)需要顯示或隱藏一組UI元素時(shí),揭露動(dòng)畫可為用戶提供視覺連續(xù)性。

  1. /* @param view The View will be clipped to the animating circle.要隱藏或顯示的view 
  2.  
  3.  * @param centerX The x coordinate of the center of the animating circle, relative to <code>view</code>.動(dòng)畫開始的中心點(diǎn)X 
  4.  
  5.  * @param centerY The y coordinate of the center of the animating circle, relative to <code>view</code>.動(dòng)畫開始的中心點(diǎn)Y 
  6.  
  7.  * @param startRadius The starting radius of the animating circle.動(dòng)畫開始半徑 
  8.  
  9.  * @param endRadius The ending radius of the animating circle.動(dòng)畫結(jié)束半徑 
  10.  
  11.  */ 
  12.  
  13. public static Animator createCircularReveal(View view
  14.  
  15.         int centerX,  int centerY, float startRadius, float endRadius) { 
  16.  
  17.     return new RevealAnimator(view, centerX, centerY, startRadius, endRadius); 
  18.  
  19.  

RevealAnimator和之前的動(dòng)畫使用沒什么區(qū)別,同樣可以設(shè)置監(jiān)聽器和加速器來實(shí)現(xiàn)各種各樣的特效,該動(dòng)畫主要用在隱藏或者顯示一個(gè)view,改變view的大小等過渡效果。

顯示view:

  1. final TextView tv9 = (TextView) findViewById(R.id.tv9); 
  2.  
  3.  
  4. findViewById(R.id.content_main).setOnClickListener(new View.OnClickListener() { 
  5.  
  6.     @Override public void onClick(View v) { 
  7.  
  8.         // get the center for the clipping circle 
  9.  
  10.         int cx = (tv9.getRight() - tv9.getLeft()) / 2; 
  11.  
  12.         int cy = (tv9.getBottom() - tv9.getTop()) / 2; 
  13.  
  14.  
  15.         // get the final radius for the clipping circle 
  16.  
  17.         int finalRadius = Math.max(tv9.getWidth(), tv9.getHeight()); 
  18.  
  19.  
  20.         // create the animator for this view (the start radius is zero) 
  21.  
  22.         final Animator anim = ViewAnimationUtils.createCircularReveal(tv9, cx, cy, 0, finalRadius); 
  23.  
  24.  
  25.         tv9.setVisibility(View.VISIBLE); 
  26.  
  27.  
  28.         anim.start(); 
  29.  
  30.     } 
  31.  
  32. });  

隱藏view:

  1. final TextView tv9 = (TextView) findViewById(R.id.tv9); 
  2.  
  3.  
  4. tv9.setOnClickListener(new View.OnClickListener() { 
  5.  
  6.     @Override public void onClick(View v) { 
  7.  
  8.         // get the center for the clipping circle 
  9.  
  10.         int cx = (tv9.getRight() - tv9.getLeft()) / 2; 
  11.  
  12.         int cy = (tv9.getBottom() - tv9.getTop()) / 2; 
  13.  
  14.  
  15.         // get the final radius for the clipping circle 
  16.  
  17.         int initRadius = Math.max(tv9.getWidth(), tv9.getHeight()); 
  18.  
  19.  
  20.         // create the animator for this view (the start radius is zero) 
  21.  
  22.         final Animator anim = ViewAnimationUtils.createCircularReveal(tv9, cx, cy, initRadius, 0); 
  23.  
  24.  
  25.         anim.addListener(new AnimatorListenerAdapter() { 
  26.  
  27.             @Override public void onAnimationEnd(Animator animation) { 
  28.  
  29.                 super.onAnimationEnd(animation); 
  30.  
  31.                 // make the view visible and start the animation 
  32.  
  33.                 tv9.setVisibility(View.INVISIBLE); 
  34.  
  35.             } 
  36.  
  37.         }); 
  38.  
  39.         anim.start(); 
  40.  
  41.     } 
  42.  
  43. });  

沿著中心縮?。?/p>

  1. Animator animator = ViewAnimationUtils.createCircularReveal(viewview.getWidth() / 2, view.getHeight() / 2, view.getWidth(), 0); 
  2.  
  3. animator.setInterpolator(new LinearInterpolator()); 
  4.  
  5. animator.setDuration(1000); 
  6.  
  7. animator.start();  

從左上角擴(kuò)展:

  1. Animator animator = ViewAnimationUtils.createCircularReveal(view,0,0,0,(float) Math.hypot(view.getWidth(), view.getHeight())); 
  2.  
  3. animator.setDuration(1000); 
  4.  
  5. animator.start();  

三、使用轉(zhuǎn)場(chǎng)動(dòng)畫

效果圖以共享元素的轉(zhuǎn)場(chǎng)動(dòng)畫為例: 

 

 

 

MaterialDesign應(yīng)用中的操作行為轉(zhuǎn)換透過通用元素之間的移動(dòng)和轉(zhuǎn)換提供不同狀態(tài)之間的視覺連接。可為進(jìn)入、退出轉(zhuǎn)換以及操作行為之間的共享元素轉(zhuǎn)換指定定制動(dòng)畫。在5.0之前,我們可以在startActivity之后調(diào)用overridePendingTransition來指定Activity的轉(zhuǎn)場(chǎng)動(dòng)畫。

  • 進(jìn)入轉(zhuǎn)換將決定操作行為中視圖如何進(jìn)入場(chǎng)景。例如,在分解進(jìn)入轉(zhuǎn)換中,視圖將從屏幕外進(jìn)入場(chǎng)景并飛往屏幕中心。
  • 退出轉(zhuǎn)換將決定操作行為中應(yīng)用行為的顯示視圖如何退出場(chǎng)景。例如,在分解退出轉(zhuǎn)換中,視圖將從屏幕中心退出場(chǎng)景。
  • 共享元素轉(zhuǎn)換將決定兩個(gè)操作行為轉(zhuǎn)換之間共享的視圖如何在這些操作行為中轉(zhuǎn)換。 例如,如果兩個(gè)操作行為擁有相同的圖像,但其位置與大小不同,changeImageTransform共享元素轉(zhuǎn)換將在這些操作行為之間平滑地轉(zhuǎn)換與縮放圖像。

Android 5.0(API Level 21)支持這些進(jìn)入與退出轉(zhuǎn)換:(普通過渡動(dòng)畫)

  • 分解 - 從場(chǎng)景中心移入或移出視圖。
  • 滑動(dòng) - 從場(chǎng)景邊緣移入或移出視圖。
  • 淡入淡出 - 通過調(diào)整透明度在場(chǎng)景中增添或移除視圖。

也支持這些共享元素轉(zhuǎn)換:(共享元素的過渡動(dòng)畫)

  • changeBounds - 為目標(biāo)視圖的大小添加動(dòng)畫。
  • changeClipBounds - 為目標(biāo)視圖的裁剪大小添加動(dòng)畫。
  • changeTransform - 為目標(biāo)視圖的縮放、旋轉(zhuǎn)和位移添加動(dòng)畫。
  • changeImageTransform - 為目標(biāo)圖片的縮放、旋轉(zhuǎn)和位移添加動(dòng)畫。

指定轉(zhuǎn)場(chǎng)動(dòng)畫

要想使用新的轉(zhuǎn)場(chǎng)動(dòng)畫,可以繼承Material Design主題后在style風(fēng)格中指定: 

 

 

 

其中,change_image_transform定義如下:

  1. <!-- res/transition/change_image_transform.xml --> 
  2.  
  3. <!-- (see also Shared Transitions below) --> 
  4.  
  5. <transitionSet xmlns:android="http://schemas.android.com/apk/res/android"
  6.  
  7.   <changeImageTransform/> 
  8.  
  9. </transitionSet>  

如果要帶代碼中開啟窗口內(nèi)容轉(zhuǎn)換,需要調(diào)用Window.requestFeature()方法。 

 

 

 

普通轉(zhuǎn)場(chǎng)動(dòng)畫:

所有繼承自visibility類都可以作為進(jìn)入、退出的過度動(dòng)畫。如果我們想自定義進(jìn)入和退出時(shí)的動(dòng)畫效果,只需要繼承Visibility,重載onAppear和onDisappear方法來定義進(jìn)入喝退出的動(dòng)畫。系統(tǒng)提供了三種默認(rèn)方式:

  • explode 從屏幕中心移入或移出視圖
  • slide 從屏幕邊緣移入或移出視圖
  • fade 改變視圖的透明度

想在xml中指定自定義的進(jìn)入、退出的過度動(dòng)畫需要先對(duì)動(dòng)畫進(jìn)行定義:

  1. <transition class="my.app.transition.CustomTransition"/> 

注意:其中CustomTransition是我們自定義的動(dòng)畫,它必須繼承自Visibility。

想以普通轉(zhuǎn)場(chǎng)動(dòng)畫的方式啟動(dòng)一個(gè)Activity,必須在startActivity函數(shù)中傳遞一個(gè)ActivityOptions的Bundle對(duì)象:

  1. ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(activity);  
  2.  
  3. startActivity(intent, options.toBundle());  

如果想讓返回也具備轉(zhuǎn)場(chǎng)效果,那么在返回的Activity中不要再調(diào)用finish函數(shù),而是應(yīng)該使finishAfterTransition來結(jié)束一個(gè)Activity,該函數(shù)會(huì)等待動(dòng)畫執(zhí)行完畢才結(jié)束該Activity。

共享轉(zhuǎn)場(chǎng)動(dòng)畫:

如果要在兩個(gè)具有共享元素的Activity之間使用轉(zhuǎn)場(chǎng)動(dòng)畫,那么:

1、在題中啟用窗口內(nèi)容轉(zhuǎn)換。android:windowContentTransitions

2、在Theme中指定一個(gè)共享元素轉(zhuǎn)換。

3、將transitions定義為xml資源。

4、利用 android:transitionName屬性對(duì)兩個(gè)布局中的共享元素指定一個(gè)通用名稱。

5、使用 ActivityOptions.makeSceneTransitionAnimation()方法。

  1. // get the element that receives the click event 
  2.  
  3. final View imgContainerView = findViewById(R.id.img_container); 
  4.  
  5.  
  6. // get the common element for the transition in this activity 
  7.  
  8. final View androidRobotView = findViewById(R.id.image_small); 
  9.  
  10.  
  11. // define a click listener 
  12.  
  13. imgContainerView.setOnClickListener(new View.OnClickListener() { 
  14.  
  15.     @Override 
  16.  
  17.     public void onClick(View view) { 
  18.  
  19.         Intent intent = new Intent(this, Activity2.class); 
  20.  
  21.         // create the transition animation - the images in the layouts 
  22.  
  23.         // of both activities are defined with android:transitionName="robot" 
  24.  
  25.         ActivityOptions options = ActivityOptions 
  26.  
  27.             .makeSceneTransitionAnimation(this, androidRobotView, "robot"); 
  28.  
  29.         // start the new activity 
  30.  
  31.         startActivity(intent, options.toBundle()); 
  32.  
  33.     } 
  34.  
  35. });  

如果要在代碼中生成共享view,那么需要調(diào)用View.setTransitionName()方法對(duì)兩個(gè)布局中的共享元素指定一個(gè)通用名稱。

如果有多個(gè)共享元素,則可以通過Pair進(jìn)行包裝處理:

  1. ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(activity, 
  2.  
  3.       Pair.create(view1, "name1"),//這里view1、view2如果是TextView或者ImageView等,需要轉(zhuǎn)成View類型才可以 
  4.  
  5.       Pair.create(view2, "name2"));       
  6.  
  7. startActivity(intent,.toBundle());  

返回時(shí)如果需要具備轉(zhuǎn)場(chǎng)動(dòng)畫,那么也需要用finish函數(shù)替代finishAfterTransition來結(jié)束一個(gè)Activity。

使用曲線運(yùn)動(dòng)

因?yàn)榍€運(yùn)動(dòng)和屬性動(dòng)畫以及貝塞爾曲線這些東西混雜在一起,所以準(zhǔn)備把這節(jié)拿出來單獨(dú)寫。這里就不多說了。

視圖狀態(tài)改變

Android 5.0在原有的圖片選擇器和顏色選擇器上進(jìn)行了增強(qiáng),不僅是控件能根據(jù)不同的狀態(tài)顯示不同的背景圖片,還能在兩種狀態(tài)切換時(shí)指定一個(gè)動(dòng)畫,來增加過渡效果,吸引用戶眼球,以突出重點(diǎn)內(nèi)容。

StateListAnimator類和圖片選擇器,顏色選擇器類似,可以根據(jù)view的狀態(tài)改變呈現(xiàn)不同的動(dòng)畫效果,通過xml我們可以構(gòu)建對(duì)應(yīng)不同狀態(tài)的動(dòng)畫合集,其使用方式也非常簡(jiǎn)單,在對(duì)應(yīng)的狀態(tài)指定一個(gè)屬性動(dòng)畫即可:

  1. <selector xmlns:android="http://schemas.android.com/apk/res/android"
  2.  
  3.     <item android:state_pressed="true"
  4.  
  5.         <set
  6.  
  7.             <objectAnimator android:propertyName="translationZ" 
  8.  
  9.                             android:duration="200" 
  10.  
  11.                             android:valueTo="20dp" 
  12.  
  13.                             android:valueType="floatType"/> 
  14.  
  15.         </set
  16.  
  17.     </item> 
  18.  
  19.     <item android:state_enabled="true" android:state_pressed="false"
  20.  
  21.         <set
  22.  
  23.             <objectAnimator android:propertyName="translationZ" 
  24.  
  25.                             android:duration="200" 
  26.  
  27.                             android:valueTo="0" 
  28.  
  29.                             android:valueType="floatType"/> 
  30.  
  31.         </set
  32.  
  33.     </item> 
  34.  
  35. </selector>  

代碼中這樣加載即可:

  1. TextView tv11 = (TextView) findViewById(R.id.tv11); 
  2.  
  3. StateListAnimator stateLAnim = AnimatorInflater.loadStateListAnimator(this,R.drawable.selector_for_button); 
  4.  
  5. tv11.setStateListAnimator(stateLAnim);  

繼承了Material主題后,按鈕默認(rèn)擁有了z屬性動(dòng)畫。如果想取消這種默認(rèn)狀態(tài),可以把狀態(tài)動(dòng)畫指定為null。

除了StateListAnimator類指定狀態(tài)切換的屬性動(dòng)畫外,還可以通過AnimatedStateListDrawable來指定狀態(tài)切換的幀動(dòng)畫:

  1. <animated-selector xmlns:android="http://schemas.android.com/apk/res/android"
  2.  
  3.     <item android:id="@+id/pressed" android:drawable="@drawable/btn_check_15" android:state_pressed="true"/> 
  4.  
  5.     <item android:id="@+id/normal"  android:drawable="@drawable/btn_check_0"/> 
  6.  
  7.     <transition android:fromId="@+id/normal" android:toId="@+id/pressed"
  8.  
  9.         <animation-list> 
  10.  
  11.             <item android:duration="20" android:drawable="@drawable/btn_check_0"/> 
  12.  
  13.             <item android:duration="20" android:drawable="@drawable/btn_check_1"/> 
  14.  
  15.             <item android:duration="20" android:drawable="@drawable/btn_check_2"/> 
  16.  
  17.         </animation-list> 
  18.  
  19.     </transition> 
  20.  
  21. </animated-selector>  

幀動(dòng)畫的資源文件直接在xml中作為view的background即可。

四、矢量圖動(dòng)畫

效果圖: 

 

 

 

先在drawable中定義一張矢量圖:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2.  
  3. <vector xmlns:android="http://schemas.android.com/apk/res/android" 
  4.  
  5.     android:height="200dp" 
  6.  
  7.     android:width="200dp" 
  8.  
  9.     android:viewportHeight="400" 
  10.  
  11.     android:viewportWidth="400"
  12.  
  13.        
  14.  
  15.     <group 
  16.  
  17.         android:name="rotationGroup" 
  18.  
  19.         android:pivotX="0" 
  20.  
  21.         android:pivotY="0"
  22.  
  23.             
  24.  
  25.         <path 
  26.  
  27.             android:name="star" 
  28.  
  29.             android:pathData="M 100,100 h 200 l -200 150 100 -250 100 250 z" 
  30.  
  31.             android:strokeColor="@color/colorPrimary" 
  32.  
  33.             android:strokeLineCap="round" 
  34.  
  35.             android:strokeWidth="10"/> 
  36.  
  37.            
  38.  
  39.     </group
  40.  
  41. </vector>  

然后在anim中定義動(dòng)畫:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2.  
  3. <set xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.     <objectAnimator 
  6.  
  7.         android:propertyName="trimPathStart" 
  8.  
  9.         android:valueFrom="0" 
  10.  
  11.         android:valueTo="1" 
  12.  
  13.         android:valueType="floatType" 
  14.  
  15.         android:duration="2000" 
  16.  
  17.         android:repeatMode="reverse" 
  18.  
  19.         android:repeatCount="-1" 
  20.  
  21.         android:interpolator="@android:interpolator/accelerate_decelerate"/> 
  22.  
  23. </set 

***在drawable中定義一個(gè)animated-vector:將動(dòng)畫資源指定給drawable屬性值的矢量圖。

  1. <?xml version="1.0" encoding="utf-8"?><animated-vector xmlns:android="http://schemas.android.com/apk/res/android" 
  2.    android:drawable="@drawable/vector_drawable"
  3.    <target 
  4.        android:name="star" 
  5.        android:animation="@anim/animation"/></animated-vector>  

注意:這里drawable屬性值是前面我們定義的矢量圖,target中name要和矢量圖中path的name一樣,animation就是前面定義的動(dòng)畫資源文件。

在view的xml中使用以及在代碼中開始動(dòng)畫: 

  1. <ImageView 
  2.  
  3.     android:id="@+id/iv" 
  4.  
  5.     android:layout_width="wrap_content" 
  6.  
  7.     android:layout_height="wrap_content" 
  8.  
  9.     android:layout_margin="20dp" 
  10.  
  11.     app:srcCompat="@drawable/anim_vector_drawable" 
  12.  
  13.     android:layout_gravity="center"/>   
  1. ImageView iv = (ImageView) findViewById(R.id.iv); 
  2.  
  3. Drawable drawable = iv.getDrawable(); 
  4.  
  5. if (drawable instanceof Animatable) { 
  6.  
  7.     ((Animatable) drawable).start(); 
  8.  
  9.  
責(zé)任編輯:龐桂玉 來源: 安卓巴士Android開發(fā)者門戶
相關(guān)推薦

2022-01-20 20:08:38

MaterialpalettesMaterial 3

2015-08-07 10:24:17

AndroidMaterialDes

2016-08-10 13:00:07

ChromeMaterial DeMaterial

2014-08-07 14:19:46

Material DeGoogle

2014-08-21 15:29:29

Material De概述

2014-12-08 14:35:51

Material De真實(shí)動(dòng)作

2014-12-08 13:40:10

Material De色彩

2015-07-21 15:02:37

設(shè)計(jì)扁平

2014-12-08 15:03:17

Material De圖像

2014-06-26 10:21:04

Material DeSwift

2014-10-27 14:34:39

Material De轉(zhuǎn)場(chǎng)動(dòng)畫

2014-09-10 10:35:11

Material De設(shè)計(jì)原則

2014-12-08 14:15:48

Material De字體排版

2014-08-19 16:10:05

Material DeUI設(shè)計(jì)趨勢(shì)

2014-08-21 15:40:53

Material De真實(shí)動(dòng)作

2014-10-27 14:18:06

Material De交互響應(yīng)

2018-04-25 09:06:32

Chrome瀏覽器語言

2014-08-11 11:19:19

Material De

2014-07-02 10:26:52

Material DeGoogle

2014-10-21 15:26:37

Material DeAndroid應(yīng)用
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)