偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

App開發(fā)中三種動(dòng)畫使用和屬性詳解

移動(dòng)開發(fā) Android
幀動(dòng)畫是順序播放一組預(yù)先定義好的圖片,不同于View動(dòng)畫,系統(tǒng)提供了另外一個(gè)類AnimationDrawable來使用幀動(dòng)畫.

[[437158]]

本文轉(zhuǎn)載自微信公眾號「Android開發(fā)編程」,作者Android開發(fā)編程。轉(zhuǎn)載本文請聯(lián)系A(chǔ)ndroid開發(fā)編程公眾號。

前言

這次我們介紹一下android中動(dòng)畫的分類:

View Animation(補(bǔ)間動(dòng)畫);

Drawable Animation(幀動(dòng)畫);

Property Animation(屬性動(dòng)畫);

一、幀動(dòng)畫

幀動(dòng)畫是順序播放一組預(yù)先定義好的圖片,不同于View動(dòng)畫,系統(tǒng)提供了另外一個(gè)類AnimationDrawable來使用幀動(dòng)畫;

幀動(dòng)畫顧名思義就是通過順序一幀一幀播放圖片從而產(chǎn)生動(dòng)畫效果,效果類似放電影;

該動(dòng)畫缺點(diǎn)比較明顯,就是如果圖片過大過多會(huì)導(dǎo)致OOM。幀動(dòng)畫xml文件放置在drawable目錄下而非anim文件夾下;

幀動(dòng)畫的使用

首先我們找一組幀動(dòng)畫的圖片放入drawable-xhdpi文件夾下,其次在drawable文件夾下創(chuàng)建xml文件,如下所示:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <animation-list 
  3.     xmlns:android="http://schemas.android.com/apk/res/android" 
  4.     android:oneshot="false"
  5.     <item android:drawable="@drawable/refresh1" android:duration="180"/> 
  6.     <item android:drawable="@drawable/refresh2" android:duration="180"/> 
  7.     ... 
  8.     <item android:drawable="@drawable/refresh25" android:duration="180"/> 
  9. </animation-list> 
  10. view = findViewById(R.id.test); 
  11.         view.setBackgroundResource(R.drawable.drawable_test_anim); 
  12.         view.setOnClickListener(new View.OnClickListener() { 
  13.             @Override 
  14.             public void onClick(View v) { 
  15.                 AnimationDrawable animationDrawable = (AnimationDrawable) view.getBackground(); 
  16.                 animationDrawable.start(); 
  17.             } 
  18.         }); 

屬性介紹

  • <animation-list> 必須是根節(jié)點(diǎn),包含一個(gè)或者多個(gè)<item>元素,屬性有:
  • android:oneshot true代表只執(zhí)行一次,false循環(huán)執(zhí)行;
  • <item> 類似一幀的動(dòng)畫資源;
  • <item> animation-list的子項(xiàng),包含屬性如下:
  • android:drawable 一個(gè)frame的Drawable資源;
  • android:duration 一個(gè)frame顯示多長時(shí)間;

二、補(bǔ)間動(dòng)畫

補(bǔ)間動(dòng)畫是通過對view進(jìn)行旋轉(zhuǎn)、縮放、漸變、透明度變化,而達(dá)到的一種動(dòng)畫效果;

是一種漸進(jìn)式動(dòng)畫。并且可以通過組合以上四種操作,完成復(fù)雜的自定義動(dòng)畫效果;

缺點(diǎn)就是只是改變的view的展示狀態(tài),但是不會(huì)改變view的位置;

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:duration="4000" 
  4.     android:fillAfter="true" 
  5.     android:fillBefore="true" 
  6.     android:interpolator="@[package:]anim/interpolator_resource" 
  7.     android:repeatMode="restart | reverse" 
  8.     android:repeatCount = “0” 
  9.     android:shareInterpolator="true" 
  10.     android:startOffset="float"
  11.     <alpha 
  12.         android:fromAlpha="float" 
  13.         android:toAlpha="float" /> 
  14.     <scale 
  15.         android:fromXScale="float" 
  16.         android:fromYScale="float" 
  17.         android:pivotX="float" 
  18.         android:pivotY="float" 
  19.         android:toXScale="float" 
  20.         android:toYScale="float" /> 
  21.     <rotate 
  22.         android:fromDegrees="float" 
  23.         android:pivotX="float" 
  24.         android:pivotY="float" 
  25.         android:toDegrees="float" /> 
  26.     <translate 
  27.         android:fromXDelta="float" 
  28.         android:fromYDelta="float" 
  29.         android:toXDelta="float" 
  30.         android:toYDelta="float" /> 
  31. </set

 

通用屬性說明:

  • android:duration=""動(dòng)畫時(shí)長,單位毫秒
  • android:fillAfter="true"動(dòng)畫完成后是否停留在結(jié)束位置,默認(rèn)false
  • android:fillBefore="true"動(dòng)畫完成后是否停留在起點(diǎn)位置。默認(rèn)true,優(yōu)先級低于fillAfter
  • android:interpolator為動(dòng)畫的變化速度【可以單獨(dú)設(shè)置,可以直接放在set里面】
  • android:repeatMode="restart | reverse"動(dòng)畫重復(fù)策略 restart從起點(diǎn)位置(正序)重復(fù),reverse從結(jié)束位置(倒序)重復(fù)
  • android:repeatCount = “0”重復(fù)次數(shù) “infinite”為無線重復(fù)
  • android:shareInterpolator="true"動(dòng)畫集合中的動(dòng)畫是否公用一個(gè)差值器
  • android:startOffset="float"動(dòng)畫延遲時(shí)間

透明度動(dòng)畫

  • 透明度動(dòng)畫,通過改變view的透明度展示動(dòng)畫。對應(yīng)AlphaAnimation和xml標(biāo)簽
  • android:fromAlpha="float" 起始透明度,取值范圍(-1.0~1.0)
  • android:toAlpha="float"結(jié)束時(shí)透明度,取值范圍(-1.0~1.0)

縮放動(dòng)畫

  • 縮放動(dòng)畫,通過修改view的大小展示動(dòng)畫。對應(yīng)ScaleAnimation類和xml表情
  • android:fromXScale="float"動(dòng)畫在水平方向X的起始縮放倍數(shù)
  • android:fromYScale="float"動(dòng)畫在水平方向Y的結(jié)束縮放倍數(shù)
  • android:toXScale="float"動(dòng)畫在豎直方向X的結(jié)束縮放倍數(shù)
  • android:toYScale="float"動(dòng)畫在豎直方向Y的結(jié)束縮放倍數(shù)
  • android:pivotX="float"縮放中心點(diǎn)的x坐標(biāo)
  • android:pivotY="float"縮放中心點(diǎn)的y坐標(biāo)

旋轉(zhuǎn)動(dòng)畫

  • 通過旋轉(zhuǎn)view展示動(dòng)畫。對應(yīng)RotateAnimation類和xml標(biāo)簽
  • android:fromDegrees="float"動(dòng)畫開始時(shí) 視圖的旋轉(zhuǎn)角度(正數(shù) = 順時(shí)針,負(fù)數(shù) = 逆時(shí)針)
  • android:toDegrees="float"動(dòng)畫結(jié)束時(shí) 視圖的旋轉(zhuǎn)角度(正數(shù) = 順時(shí)針,負(fù)數(shù) = 逆時(shí)針)
  • android:pivotX="float"旋轉(zhuǎn)中心點(diǎn)的x坐標(biāo) 具體如上縮放中心點(diǎn)參數(shù)解釋
  • android:pivotY="float"旋轉(zhuǎn)中心點(diǎn)的y坐標(biāo) 具體如上縮放中心點(diǎn)參數(shù)解釋

平移動(dòng)畫

  • 平移動(dòng)畫,更改view的展示位置展示動(dòng)畫。對應(yīng)TranslateAnimation類和 xml表情
  • android:fromXDelta="float"view在水平x方向的起始值
  • android:fromYDelta="float"view在水平y(tǒng)方向的起始值
  • android:toXDelta="float"view在水平x方向的結(jié)束值
  • android:toYDelta="float" view在水平y(tǒng)方向的結(jié)束值

具體動(dòng)畫的使用

應(yīng)用動(dòng)畫xml配置

  1. TextView textDemo = findViewById(R.id.text_demo); 
  2. Animation animation = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.rotate_animation_test); 
  3. textDemo.startAnimation(animation); 

使用java類配置動(dòng)畫,具體參數(shù)類同xml參數(shù),建議使用xml配置動(dòng)畫

  1. AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 2.0f); 
  2. textDemo.startAnimation(alphaAnimation); 

監(jiān)聽動(dòng)畫

  1. alphaAnimation.setAnimationListener(new Animation.AnimationListener() { 
  2.     @Override 
  3.     public void onAnimationStart(Animation animation) { 
  4.         //動(dòng)畫開始回調(diào) 
  5.     } 
  6.     @Override 
  7.     public void onAnimationEnd(Animation animation) { 
  8.         //動(dòng)畫結(jié)束回調(diào) 
  9.     } 
  10.     @Override 
  11.     public void onAnimationRepeat(Animation animation) { 
  12.         //動(dòng)畫重復(fù)時(shí)回調(diào) 
  13.     } 
  14. }); 

三、屬性動(dòng)畫

屬性動(dòng)畫本質(zhì)是通過改變對象的屬性(例如:x,y等屬性),來實(shí)現(xiàn)動(dòng)畫的,所以基本上是無所不能的,只要對象有這個(gè)屬性,就能實(shí)現(xiàn)動(dòng)畫效果;

屬性動(dòng)畫是在api 11的新特性,通過動(dòng)態(tài)的改變view的屬性從而達(dá)到動(dòng)畫效果。雖然可以使用nineoldandroid庫向下兼容,但是兼容本質(zhì)是使用補(bǔ)間動(dòng)畫完成,也就是說不會(huì)更改view的屬性,也不會(huì)更改view的位置;

屬性動(dòng)畫比較常用的類:ValueAnimator、ObjectAnimator、AnimationSet,其中ObjectAnimator是ValueAnimator的子類,而AnminationSet是動(dòng)畫集合;

1.單個(gè)動(dòng)畫

  1. ObjectAnimator animator = ObjectAnimator 
  2.         .ofInt(textDemo, "backgroundColor", 0XffFF0000, 0Xff0000FF) 
  3.         .setDuration(2000); 
  4. animator.setRepeatMode(ValueAnimator.RESTART); 
  5. animator.setRepeatCount(10); 
  6. animator.start(); 

2.動(dòng)畫集合

  1. AnimatorSet animatorSet = new AnimatorSet(); 
  2. ValueAnimator translationX = ObjectAnimator.ofFloat(textDemo, "translationX", 200f); 
  3. ValueAnimator animator = ObjectAnimator 
  4.         .ofInt(textDemo, "backgroundColor", 0XffFF0000, 0Xff0000FF); 
  5. animatorSet.playTogether(translationX,animator); 
  6. animatorSet.setDuration(1000).start(); 

動(dòng)畫配置同樣可以使用xml配置;

3.差值器和估值器

差值器(Interpolator)

根據(jù)時(shí)間流逝百分比計(jì)算當(dāng)前屬性改變百分比。同xml配置動(dòng)畫中的 android:interpolator屬性配置,常見有LinearInterpolator(線性差值器)、AccelerateDecelerateInterpolator(加速減速差值器)等。自定義需要實(shí)現(xiàn)Interpolator或者TimeInterpolator。Interpolator接口繼承TimeInterpolator;

  1. // Interpolator接口 
  2. public interface Interpolator extends TimeInterpolator{  
  3.     // 內(nèi)部只有一個(gè)方法 
  4.      float getInterpolation(float input) {   
  5.          // 參數(shù)說明 
  6.          // input值值變化范圍是0-1,且隨著動(dòng)畫進(jìn)度(0% - 100% )均勻變化 
  7.         // 即動(dòng)畫開始時(shí),input值 = 0;動(dòng)畫結(jié)束時(shí)input = 1 
  8.         // 而中間的值則是隨著動(dòng)畫的進(jìn)度(0% - 100%)在0到1之間均勻增加 
  9.       ...// 插值器的計(jì)算邏輯 
  10.       return xxx; 
  11.       // 返回的值就是用于估值器繼續(xù)計(jì)算的fraction值,下面會(huì)詳細(xì)說明 
  12.     }   
  13. // TimeInterpolator接口 
  14. // 同上 
  15. public interface TimeInterpolator {   
  16.     float getInterpolation(float input);   

估值器(TypeEvaluator)

根據(jù)當(dāng)前屬性改變百分比計(jì)算改變后的屬性值。屬性動(dòng)畫特有的屬性;自定義估值器需要實(shí)現(xiàn)TypeEvaluator接口;

  1. public interface TypeEvaluator {   
  2.     public Object evaluate(float fraction, Object startValue, Object endValue) {   
  3. // 參數(shù)說明 
  4. // fraction:插值器getInterpolation()的返回值 
  5. // startValue:動(dòng)畫的初始值 
  6. // endValue:動(dòng)畫的結(jié)束值 
  7.         ....// 估值器的計(jì)算邏輯 
  8.         return xxx; 
  9.         // 賦給動(dòng)畫屬性的具體數(shù)值 
  10.         // 使用反射機(jī)制改變屬性變化 

可以對任意屬性做屬性動(dòng)畫,屬性動(dòng)畫要求動(dòng)畫作用的對象提供該屬性的get()和set()方法。因?yàn)閷傩詣?dòng)畫本質(zhì)就是根據(jù)外界傳遞的對象屬性的初始值和終點(diǎn)值,然后根據(jù)估值器和差值器計(jì)算屬性值,不斷調(diào)用屬性的set方法,通過時(shí)間的推移所傳遞的值,越來越近終點(diǎn)值;

注意:

對象屬性必須提供對應(yīng)的set方法,而且如果沒有初始值傳入的情況下必須要設(shè)置get方法,因?yàn)?系統(tǒng)要獲取初始值,如果沒有滿足條件則程序cash;

對象屬性的set方法對屬性做出改變,需要能夠通過某種方法表示出來。帶來ui展示效果的改變。否則動(dòng)畫不會(huì)生效;

ValueAnimator

使用ValueAnimator通過監(jiān)聽動(dòng)畫過程,自己改變對象屬性完成動(dòng)畫

  1. ValueAnimator valueAnimator = ValueAnimator.ofInt(1, 100); 
  2.         valueAnimator.setDuration(2000);//動(dòng)畫持續(xù)時(shí)間 
  3.         valueAnimator.setRepeatCount(0);//重復(fù)次數(shù) 
  4.         valueAnimator.setRepeatMode(ValueAnimator.REVERSE);//重復(fù)方式, 
  5.         valueAnimator.setStartDelay(20);//開始前延遲時(shí)間 
  6.         valueAnimator.setEvaluator(new IntEvaluator());//估值器 
  7.         valueAnimator.setInterpolator(new LinearInterpolator());//差值器 
  8.         valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
  9.             @Override 
  10.             public void onAnimationUpdate(ValueAnimator animation) { 
  11.                 //獲取當(dāng)前動(dòng)畫屬性值,即1~100 
  12.                 Integer animatedValue = (Integer) animation.getAnimatedValue(); 
  13.                 //獲取動(dòng)畫的百分比 
  14.                 float animatedFraction = animation.getAnimatedFraction(); 
  15.                 ViewGroup.LayoutParams layoutParams = textView.getLayoutParams(); 
  16.                 int width = layoutParams.width; 
  17.                 int height = layoutParams.height; 
  18.                 layoutParams.height = height + 1; 
  19.                 layoutParams.width=width+1; 
  20.                 Log.e(TAG,"animatedValue: "+animatedValue+" animatedFraction : "+animatedFraction 
  21.                 +" width : "+layoutParams.width+" height : "+layoutParams.height); 
  22.                 textView.requestLayout(); 
  23.             } 
  24.         }); 
  25.         valueAnimator.start(); 
  26.     } 

四、注意事項(xiàng)

OOM注意,在幀動(dòng)畫中如果圖片過大、數(shù)量過多容易出現(xiàn);

內(nèi)存泄漏, 切記在activity銷毀時(shí),停止動(dòng)畫。否則一些無限循環(huán)動(dòng)畫將導(dǎo)致activity不能釋放而內(nèi)存泄漏。出現(xiàn)在屬性動(dòng)畫中,view動(dòng)畫不存在此類問題;

View動(dòng)畫即補(bǔ)間動(dòng)畫,是改變view的視覺位置,改變view的影像展示位置,而不改變真實(shí)位置;注意交互體驗(yàn)??赡軙?huì)出現(xiàn)view動(dòng)畫結(jié)束后,view無法隱藏的問題及setVisibility(View.GONE)失效。此時(shí)調(diào)用view.clearAnimation()清除動(dòng)畫??尚迯?fù)此類問題;

總結(jié)

公眾號里面是系統(tǒng)的總結(jié),但是要系統(tǒng)學(xué)習(xí)一類的知識(shí)點(diǎn)了,又不是很好;

 

于是就想寫個(gè)小冊-掘金小冊,這樣老鐵們就可以系統(tǒng)的學(xué)習(xí)某個(gè)知識(shí)點(diǎn)了;

 

責(zé)任編輯:武曉燕 來源: Android開發(fā)編程
相關(guān)推薦

2011-08-01 18:42:40

分區(qū)維度物化視圖

2024-07-16 14:15:09

2018-01-17 15:02:28

VMware網(wǎng)絡(luò)連接

2012-03-26 12:23:25

JavaSwing

2017-01-05 16:19:12

C++正則表達(dá)式

2024-02-26 13:47:00

C#Socket數(shù)據(jù)接收

2009-06-09 16:53:22

Java Swing處理方法比較

2009-07-01 17:22:05

連接字符串

2021-07-10 10:01:37

Python簡單函數(shù)

2021-03-17 09:59:26

Python函數(shù)調(diào)用

2024-04-11 12:57:55

Python函數(shù)

2009-12-01 09:18:22

Linux版本

2021-11-23 10:30:35

Android技術(shù)代碼

2010-04-02 13:15:01

Oracle跟蹤

2024-04-25 12:49:22

2013-06-17 17:08:47

Windows PhoWP開發(fā)共享數(shù)據(jù)方式

2022-06-20 08:50:16

TypeScript類型語法

2010-04-26 12:19:28

Oracle 數(shù)據(jù)庫

2013-12-18 15:45:33

多核

2023-10-28 16:25:17

濾波C++
點(diǎn)贊
收藏

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