補(bǔ)間動(dòng)畫(huà)與幀動(dòng)畫(huà)簡(jiǎn)單筆記

補(bǔ)間動(dòng)畫(huà)與幀動(dòng)畫(huà):

  • 1、Animations
  • 2、Tween
    首先來(lái)看看Tween動(dòng)畫(huà)
  • 1、Alpha:漸變透明度動(dòng)畫(huà)
  • 2、Scale:漸變尺寸伸縮動(dòng)畫(huà)
  • 3、Translate:畫(huà)面轉(zhuǎn)換位置移動(dòng)動(dòng)畫(huà)
  • 4、Rotate:畫(huà)面轉(zhuǎn)移選擇動(dòng)畫(huà)
這些動(dòng)畫(huà)的執(zhí)行步奏差不多,先定義Animation動(dòng)畫(huà)對(duì)象,然后設(shè)置動(dòng)畫(huà)的一些屬性,最后通過(guò)startAnimation()方法來(lái)開(kāi)始動(dòng)畫(huà)
            setDuration(long durationMillis)   設(shè)置動(dòng)畫(huà)顯示的時(shí)間
            startAnimation(Animation animation)   animation為要播放的動(dòng)畫(huà)
            
                
  • 第一種:Alpha
    (1)通過(guò)xml來(lái)創(chuàng)建動(dòng)畫(huà)alpha_anim.xml 在res/anim目錄下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha android:fromAlpha="0.1" android:toAlpha="1.0" android:duration="5000"/>
                        </set>
            (2)直接在程序中創(chuàng)建動(dòng)畫(huà):
 Animation alpha = new AlphaAnimation(0.1f,1.0f); 
                        alpha.setDuration(5000);
                        
  • 第二種:Sacle
ScaleAnimation(float fromX, float toX, float fromY, float toY,int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
  • 功能:創(chuàng)建一個(gè)漸變尺寸伸縮動(dòng)畫(huà)
  • fromX,toX,分別是其實(shí)和結(jié)束時(shí)候x坐標(biāo)上的伸縮尺寸,fromY,toY,分別是起始和結(jié)束的時(shí)候y坐標(biāo)的伸縮尺寸
  • pivotXValue,pivotYValue 分別是伸縮動(dòng)畫(huà)相對(duì)于x,y坐標(biāo)開(kāi)始的位置,
  • pivotXType,pivotYType分別是x,y的伸縮模式
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale android:interpolator="@android:anim/accelerate_decelerate_interpolator"
             android:fromXScale="0.0" android:toXScale="1.0"
             android:fromYScale="0.0" android:toYScale="1.0"
             android:pivotX="50%" android:pivotY="50%"
             android:fillAfter="false" android:duration="5000"/>
                        </set>
  • 第三種:Translate
TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) 
 * 創(chuàng)建一個(gè)移動(dòng)畫(huà)面位置的動(dòng)畫(huà)fromXDelta ,fromYDelta 起始坐標(biāo)  toXDelta toYDelta 結(jié)束坐標(biāo)
<?xml version="1.0" encoding="utf-8"?>
            <set xmlns:android="http://schemas.android.com/apk/res/android">
                <translate
                    android:fromXDelta="10"
                    android:fromYDelta="10"
                    android:toXDelta="100"
                    android:toYDelta="100"/>
            </set>
        
  • 第四種:RotateAnimation
RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue,int pivotYType, float pivotYValue)
* 參數(shù);fromDegrees 為開(kāi)始的角度,toDegrees為結(jié)束的角度  pivotXType   pivotYType 分別為x,y的伸縮模式 pivotXValue  pivotYValue 分別為伸縮動(dòng)畫(huà)相對(duì)于x,y坐標(biāo)的開(kāi)始位置
        <?xml version="1.0" encoding="utf-8"?>
                <set xmlns:android="http://schemas.android.com/apk/res/android">
                    <rotate
                        android:duration="5000"
                        android:fromDegrees="0"
                        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
                        android:pivotX="50%"
                        android:pivotY="50%"
                        android:toDegrees="360"/>
                </set>
  • 3、Frame

             frame動(dòng)畫(huà)起始就是逐幀動(dòng)畫(huà),用法也比Tween動(dòng)畫(huà)簡(jiǎn)單,只需要?jiǎng)?chuàng)建一個(gè)AnimationDtawable對(duì)象來(lái)表示Frame動(dòng)畫(huà),然后通過(guò)addFrame方法把沒(méi)一幀要顯示的內(nèi)容加進(jìn)去就行了,最后通過(guò)start方法就可以播放這個(gè)動(dòng)畫(huà)了,通過(guò)還可以使用setOneShort()方法來(lái)設(shè)置動(dòng)畫(huà)是否重復(fù)播放,在這里,還需要設(shè)置圖片的所在位置
             首先在res/main目錄下創(chuàng)建一個(gè)xml配置文件,用于存放圖片資源的索引配置的是一個(gè)以<animation-list>根元素和<item>子元素,如下
    
    
        <?xml version="1.0" encoding="utf-8"?>
            <animation-list xmlns:android="http://schemas.android.com/apk/res/android">
                <item android:drawable="@mipmap/g1" android:duration="300"/>
                <item android:drawable="@mipmap/g2" android:duration="300"/>
                <item android:drawable="@mipmap/g3" android:duration="300"/>
                <item android:drawable="@mipmap/g4" android:duration="300"/>
                <item android:drawable="@mipmap/g5" android:duration="300"/>
                <item android:drawable="@mipmap/g6" android:duration="300"/>
                <item android:drawable="@mipmap/g7" android:duration="300"/>
                <item android:drawable="@mipmap/g8" android:duration="300"/>
                <item android:drawable="@mipmap/g9" android:duration="300"/>
                <item android:drawable="@mipmap/g10" android:duration="300"/>
            </animation-list>
            
          AnimationDrawable a = (AnimationDrawable) frameImage.getDrawable();
            a.start();
             //a.stop();
  • Android框架提供了兩種動(dòng)畫(huà)系統(tǒng):屬性動(dòng)畫(huà)(3.0中引進(jìn))以及視圖動(dòng)畫(huà),總的來(lái)說(shuō),屬性動(dòng)畫(huà)系統(tǒng)是最好的選擇,因?yàn)樗屿`活,并提供了更多的特性

  • 屬性動(dòng)畫(huà)系統(tǒng)是一個(gè)強(qiáng)大的框架,他允許你動(dòng)畫(huà)幾乎所有的東西,例如一個(gè)對(duì)象在屏幕中的位置,要?jiǎng)赢?huà)多久,和動(dòng)畫(huà)之間的距離

  • 通常我們要操作的屬性為:

  • rotationX,rotationY 旋轉(zhuǎn)

  • scaleX ,scaleY 縮放

  • translationX,translationY 平移

  • x,y 坐標(biāo)

  • alpha 透明度

ObjectAnimator:

 //圍繞x周旋轉(zhuǎn)360度
                ObjectAnimator.ofFloat(object_image,"rotationX",0.0f,360f).setDuration(500).start();
               
               //組合多個(gè)動(dòng)畫(huà)
                PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha",1f,0f,1f);
                PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleX",1f,0,1f);
                PropertyValuesHolder pvhZ = PropertyValuesHolder.ofFloat("scaleY",1f,0,1f);
                ObjectAnimator.ofPropertyValuesHolder(object_image,pvhX,pvhY,pvhZ).setDuration(500).start();
        
        
        ValueAnimator:
        
        和ObjectAnimator用法類鎖,
        自由落體實(shí)例:
          DisplayMetrics dm = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(dm);
                ValueAnimator anim = ValueAnimator.ofFloat(object_image.getY(),dm.heightPixels + object_image.getHeight()).setDuration(5000);
                anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        object_image.setTranslationY((Float) animation.getAnimatedValue());
                    }
                });
                anim.start();
                
  • 監(jiān)聽(tīng)動(dòng)畫(huà)的事件:
  • 對(duì)于動(dòng)畫(huà),一般都是一些輔助效果,比如我們要?jiǎng)h除某個(gè)元素,我們可能希望是個(gè)淡出的效果,但是最終還要?jiǎng)h掉,并不是你透明度沒(méi)有了,還站著位置
    所以我們需要知道動(dòng)畫(huà)如何結(jié)束
final View view = object_image;
             ObjectAnimator anim = ObjectAnimator.ofFloat(object_image,"alpha",1f,0f).setDuration(500);
               anim.addListener(new Animator.AnimatorListener() {
                    @Override
                 public void onAnimationStart(Animator animation) {
                    }
                    @Override
                  public void onAnimationEnd(Animator animation) {
                     ViewGroup viewGroup = (ViewGroup) view.getParent();
                       if (viewGroup != null){
                        //動(dòng)畫(huà)結(jié)束的時(shí)候刪除
                            viewGroup.removeView(object_image);
                       }
                   }
                    @Override
             public void onAnimationCancel(Animator animation) {
                    }
                    @Override
             public void onAnimationRepeat(Animator animation) {
                    }
                });
                anim.start();
                
                或者
                anim.addListener(new AnimatorListenerAdapter() {
                    @Override
               public void onAnimationEnd(Animator animation) {
                        super.onAnimationEnd(animation);
                        ViewGroup viewGropu = (ViewGroup) object_image.getParent();
                        if (viewGropu != null){
                            viewGropu.removeView(object_image);
                        }
                    }
                });
                anim.start(0;)
                
                
  • 動(dòng)畫(huà)集合:
  ObjectAnimator a1 = ObjectAnimator.ofFloat(object_image,"translationX",0f,200f);

                ObjectAnimator a2 = ObjectAnimator.ofFloat(object_image,"translationY",0f,200f);

                ObjectAnimator a3 = ObjectAnimator.ofFloat(object_image,"rotation",0.0f,360f,0f);

                AnimatorSet set = new AnimatorSet();
                set.setDuration(1000);

                //set.playTogether(a1,a2,a3);  //三個(gè)動(dòng)畫(huà)同時(shí)執(zhí)行
                //set.playSequentially(a1,a2,a3);  //按序執(zhí)行
                //set.setStartDelay(300);  //延時(shí)執(zhí)行

                //a1,a2同時(shí)執(zhí)行,之后a3
                set.play(a1).with(a2);
                set.play(a3).with(a2);
                set.start();
                
  • 注意:
animSet.play().with();也是支持鏈?zhǔn)骄幊痰?,但是?                animSet.play(anim1).widh(anim2).before(anim3).before(anim4);
                這樣是不行的,系統(tǒng)不會(huì)根據(jù)你寫(xiě)的這一長(zhǎng)串決定先后順序的
                
  • 插值器(Interpolators)
例如:set.setInterpolator(new BounceInterpolator());
        AccelerateDecelerateInterpolator  插補(bǔ),其變化率慢慢開(kāi)始和結(jié)束,但是通過(guò)中間加速
        AccelerateInterpolator  插補(bǔ)  其變化率開(kāi)始緩慢,然后加快
        AnticipateInterpolator  內(nèi)插的變化開(kāi)始落后,然后向前甩
        AnticipateOvershootInterpolator  內(nèi)插的變化,開(kāi)始落后,甩向前沖過(guò)目標(biāo)值,然后終于可以追溯到最終值
        BounceInterpolator 插補(bǔ) 其變化在最后反彈
        CycleInterpolator  內(nèi)插動(dòng)畫(huà)重復(fù)指定的周期數(shù)
        DecelerateInterpolator 插補(bǔ) 其變化的速度開(kāi)始很快,然后減速
        LinearInterpolator  插補(bǔ)  其變化率是恒定的
        OvershootInterpolator 內(nèi)插的變化甩向前和沖過(guò)的最后一個(gè)值,然后回來(lái)
        TimeInterpolator 一個(gè)接口,使您實(shí)現(xiàn)自己的插補(bǔ)
        
        
  • 使用xml文件夾創(chuàng)建屬性動(dòng)畫(huà)
首先在res下創(chuàng)建animator文件下,然后創(chuàng)建自己的文件
    <?xml version="1.0" encoding="utf-8"?>
        <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
                        android:duration="1000"
                        android:propertyName="scaleX"
                        android:valueFrom="1.0"
                        android:valueTo="2.0"
                        android:valueType="floatType"
            >
        </objectAnimator>
  • 多個(gè)動(dòng)畫(huà)組合
<?xml version="1.0" encoding="utf-8"?>
            <set xmlns:android="http://schemas.android.com/apk/res/android"
                 android:ordering="together"
                >
                <objectAnimator
                    android:duration="1000"
                    android:propertyName="scaleX"
                    android:valueFrom="1"
                    android:valueTo="0.5"/>
                <objectAnimator
                    android:duration="1000"
                    android:propertyName="scaleY"
                    android:valueFrom="1"
                    android:valueTo="0.5"/>

            </set>
            
  • 多個(gè)動(dòng)畫(huà)組合
   Animator anim = AnimatorInflater.loadAnimator(this,R.animator.anim);
            object_image.setPivotX(0);
            object_image.setPivotY(0);
            //顯示的調(diào)用invalidate
            object_image.invalidate();
            anim.setTarget(object_image);
            anim.start();
            
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容