最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android補(bǔ)間動(dòng)畫(huà)的實(shí)現(xiàn)示例

 更新時(shí)間:2023年04月25日 08:27:15   作者:向陽(yáng)逐夢(mèng)  
本文主要介紹了Android補(bǔ)間動(dòng)畫(huà)的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

幀動(dòng)畫(huà)是通過(guò)連續(xù)播放圖片來(lái)模擬動(dòng)畫(huà)效果,而補(bǔ)間動(dòng)畫(huà)開(kāi)發(fā)者只需指定動(dòng)畫(huà)開(kāi)始,以及動(dòng)畫(huà)結(jié)束"關(guān)鍵幀",而動(dòng)畫(huà)變化的"中間幀"則由系統(tǒng)計(jì)算并補(bǔ)齊!

1.補(bǔ)間動(dòng)畫(huà)的分類和Interpolator

Andoird所支持的補(bǔ)間動(dòng)畫(huà)效果有如下這五種,或者說(shuō)四種吧,第五種是前面幾種的組合而已。

  • AlphaAnimation: 透明度漸變效果,創(chuàng)建時(shí)許指定開(kāi)始以及結(jié)束透明度,還有動(dòng)畫(huà)的持續(xù)時(shí)間,透明度的變化范圍(0,1),0是完全透明,1是完全不透明;對(duì)應(yīng)<alpha/>標(biāo)簽!
  • ScaleAnimation:縮放漸變效果,創(chuàng)建時(shí)需指定開(kāi)始以及結(jié)束的縮放比,以及縮放參考點(diǎn),還有動(dòng)畫(huà)的持續(xù)時(shí)間;對(duì)應(yīng)<scale/>標(biāo)簽!
  • TranslateAnimation:位移漸變效果,創(chuàng)建時(shí)指定起始以及結(jié)束位置,并指定動(dòng)畫(huà)的持續(xù)時(shí)間即可;對(duì)應(yīng)<translate/>標(biāo)簽!
  • RotateAnimation:旋轉(zhuǎn)漸變效果,創(chuàng)建時(shí)指定動(dòng)畫(huà)起始以及結(jié)束的旋轉(zhuǎn)角度,以及動(dòng)畫(huà)持續(xù)時(shí)間和旋轉(zhuǎn)的軸心;對(duì)應(yīng)<rotate/>標(biāo)簽
  • AnimationSet:組合漸變,就是前面多種漸變的組合,對(duì)應(yīng)<set/>標(biāo)簽

在開(kāi)始講解各種動(dòng)畫(huà)的用法之前,我們先要來(lái)講解一個(gè)東西:Interpolator

用來(lái)控制動(dòng)畫(huà)的變化速度,可以理解成動(dòng)畫(huà)渲染器,當(dāng)然我們也可以自己實(shí)現(xiàn)Interpolator接口,自行來(lái)控制動(dòng)畫(huà)的變化速度,而Android中已經(jīng)為我們提供了五個(gè)可供選擇的實(shí)現(xiàn)類:

  • LinearInterpolator:動(dòng)畫(huà)以均勻的速度改變
  • AccelerateInterpolator:在動(dòng)畫(huà)開(kāi)始的地方改變速度較慢,然后開(kāi)始加速
  • AccelerateDecelerateInterpolator:在動(dòng)畫(huà)開(kāi)始、結(jié)束的地方改變速度較慢,中間時(shí)加速
  • CycleInterpolator:動(dòng)畫(huà)循環(huán)播放特定次數(shù),變化速度按正弦曲線改變:Math.sin(2 * mCycles * Math.PI * input)
  • DecelerateInterpolator:在動(dòng)畫(huà)開(kāi)始的地方改變速度較快,然后開(kāi)始減速
  • AnticipateInterpolator:反向,先向相反方向改變一段再加速播放
  • AnticipateOvershootInterpolator:開(kāi)始的時(shí)候向后然后向前甩一定值后返回最后的值
  • BounceInterpolator: 跳躍,快到目的值時(shí)值會(huì)跳躍,如目的值100,后面的值可能依次為85,77,70,80,90,100
  • OvershottInterpolator:回彈,最后超出目的值然后緩慢改變到目的值

2.各種動(dòng)畫(huà)的詳細(xì)講解

這里的android:duration都是動(dòng)畫(huà)的持續(xù)時(shí)間,單位是毫秒

1)AlphaAnimation(透明度漸變)

anim_alpha.xml

<alpha xmlns:android="http://schemas.android.com/apk/res/android"  
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"  
    android:fromAlpha="1.0"  
    android:toAlpha="0.1"  
    android:duration="2000"/>

屬性解釋:

fromAlpha :起始透明度toAlpha:結(jié)束透明度透明度的范圍為:0-1,完全透明-完全不透明

2)ScaleAnimation(縮放漸變)

anim_scale.xml

<scale xmlns:android="http://schemas.android.com/apk/res/android"  
    android:interpolator="@android:anim/accelerate_interpolator"  
    android:fromXScale="0.2"  
    android:toXScale="1.5"  
    android:fromYScale="0.2"  
    android:toYScale="1.5"  
    android:pivotX="50%"  
    android:pivotY="50%"  
    android:duration="2000"/>

屬性解釋:

  • fromXScale/fromYScale:沿著X軸/Y軸縮放的起始比例
  • toXScale/toYScale:沿著X軸/Y軸縮放的結(jié)束比例
  • pivotX/pivotY:縮放的中軸點(diǎn)X/Y坐標(biāo),即距離自身左邊緣的位置,比如50%就是以圖像的中心為中軸點(diǎn)

3)TranslateAnimation(位移漸變)

anim_translate.xml

<translate xmlns:android="http://schemas.android.com/apk/res/android"  
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"  
    android:fromXDelta="0"  
    android:toXDelta="320"  
    android:fromYDelta="0"  
    android:toYDelta="0"  
    android:duration="2000"/>

屬性解釋:

  • fromXDelta/fromYDelta:動(dòng)畫(huà)起始位置的X/Y坐標(biāo)
  • toXDelta/toYDelta:動(dòng)畫(huà)結(jié)束位置的X/Y坐標(biāo)

4)RotateAnimation(旋轉(zhuǎn)漸變)

anim_rotate.xml

<rotate xmlns:android="http://schemas.android.com/apk/res/android"  
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"  
    android:fromDegrees="0"  
    android:toDegrees="360"  
    android:duration="1000"  
    android:repeatCount="1"  
    android:repeatMode="reverse"/> 

屬性解釋:

  • fromDegrees/toDegrees:旋轉(zhuǎn)的起始/結(jié)束角度
  • repeatCount:旋轉(zhuǎn)的次數(shù),默認(rèn)值為0,代表一次,假如是其他值,比如3,則旋轉(zhuǎn)4次另外,值為-1或者infinite時(shí),表示動(dòng)畫(huà)永不停止
  • repeatMode:設(shè)置重復(fù)模式,默認(rèn)restart,但只有當(dāng)repeatCount大于0或者infinite或-1時(shí)才有效。還可以設(shè)置成reverse,表示偶數(shù)次顯示動(dòng)畫(huà)時(shí)會(huì)做方向相反的運(yùn)動(dòng)!

5)AnimationSet(組合漸變)

非常簡(jiǎn)單,就是前面幾個(gè)動(dòng)畫(huà)組合到一起而已

anim_set.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"  
    android:interpolator="@android:anim/decelerate_interpolator"  
    android:shareInterpolator="true" >  
  
    <scale  
        android:duration="2000"  
        android:fromXScale="0.2"  
        android:fromYScale="0.2"  
        android:pivotX="50%"  
        android:pivotY="50%"  
        android:toXScale="1.5"  
        android:toYScale="1.5" />  
  
    <rotate  
        android:duration="1000"  
        android:fromDegrees="0"  
        android:repeatCount="1"  
        android:repeatMode="reverse"  
        android:toDegrees="360" />  
  
    <translate  
        android:duration="2000"  
        android:fromXDelta="0"  
        android:fromYDelta="0"  
        android:toXDelta="320"  
        android:toYDelta="0" />  
  
    <alpha  
        android:duration="2000"  
        android:fromAlpha="1.0"  
        android:toAlpha="0.1" />  

</set>  

3.寫(xiě)個(gè)例子來(lái)體驗(yàn)下

好的,下面我們就用上面寫(xiě)的動(dòng)畫(huà)來(lái)寫(xiě)一個(gè)例子,讓我們體會(huì)體會(huì)何為補(bǔ)間動(dòng)畫(huà):首先來(lái)個(gè)簡(jiǎn)單的布局:

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn_alpha"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="透明度漸變" />

    <Button
        android:id="@+id/btn_scale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="縮放漸變" />

    <Button
        android:id="@+id/btn_tran"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="位移漸變" />

    <Button
        android:id="@+id/btn_rotate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="旋轉(zhuǎn)漸變" />

    <Button
        android:id="@+id/btn_set"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="組合漸變" />

    <ImageView
        android:id="@+id/img_show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="48dp"
        android:src="@mipmap/img_face" />
    
</LinearLayout>

好噠,接著到我們的MainActivity.java,同樣非常簡(jiǎn)單,只需調(diào)用AnimationUtils.loadAnimation()加載動(dòng)畫(huà),然后我們的View控件調(diào)用startAnimation開(kāi)啟動(dòng)畫(huà)即可。

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button btn_alpha;
    private Button btn_scale;
    private Button btn_tran;
    private Button btn_rotate;
    private Button btn_set;
    private ImageView img_show;
    private Animation animation = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bindViews();
    }

    private void bindViews() {
        btn_alpha = (Button) findViewById(R.id.btn_alpha);
        btn_scale = (Button) findViewById(R.id.btn_scale);
        btn_tran = (Button) findViewById(R.id.btn_tran);
        btn_rotate = (Button) findViewById(R.id.btn_rotate);
        btn_set = (Button) findViewById(R.id.btn_set);
        img_show = (ImageView) findViewById(R.id.img_show);

        btn_alpha.setOnClickListener(this);
        btn_scale.setOnClickListener(this);
        btn_tran.setOnClickListener(this);
        btn_rotate.setOnClickListener(this);
        btn_set.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_alpha:
                animation = AnimationUtils.loadAnimation(this,
                        R.anim.anim_alpha);
                img_show.startAnimation(animation);
                break;
            case R.id.btn_scale:
                animation = AnimationUtils.loadAnimation(this,
                        R.anim.anim_scale);
                img_show.startAnimation(animation);
                break;
            case R.id.btn_tran:
                animation = AnimationUtils.loadAnimation(this,
                        R.anim.anim_translate);
                img_show.startAnimation(animation);
                break;
            case R.id.btn_rotate:
                animation = AnimationUtils.loadAnimation(this,
                        R.anim.anim_rotate);
                img_show.startAnimation(animation);
                break;
            case R.id.btn_set:
                animation = AnimationUtils.loadAnimation(this,
                        R.anim.anim_set);
                img_show.startAnimation(animation);
                break;
        }
    }
}

運(yùn)行效果圖

到此這篇關(guān)于Android補(bǔ)間動(dòng)畫(huà)的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Android補(bǔ)間動(dòng)畫(huà)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

禄丰县| 连城县| 凤庆县| 黄大仙区| 南岸区| 昔阳县| 延津县| 江城| 玛纳斯县| 泰顺县| 武陟县| 盐城市| 桐庐县| 密山市| 黎城县| 黄陵县| 额敏县| 祁阳县| 靖远县| 搜索| 龙里县| 辛集市| 凌云县| 醴陵市| 万年县| 峨边| 庆安县| 玛沁县| 基隆市| 厦门市| 南华县| 锡林郭勒盟| 垦利县| 墨竹工卡县| 锡林浩特市| 大埔区| 无棣县| 诸暨市| 株洲市| 勐海县| 车险|