欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android?縮放動(dòng)畫?ScaleAnimation的使用小結(jié)

 更新時(shí)間:2024年03月25日 15:02:54   作者:星月黎明  
ScaleAnimation即縮放動(dòng)畫,應(yīng)用場(chǎng)景特別多,比如常見的隱藏菜單點(diǎn)擊顯示,這篇文章主要介紹了Android?縮放動(dòng)畫?ScaleAnimation的使用小結(jié),需要的朋友可以參考下

什么是ScaleAnimation

ScaleAnimation即縮放動(dòng)畫,應(yīng)用場(chǎng)景特別多,比如常見的隱藏菜單點(diǎn)擊顯示

下面我分兩種方式來介紹ScaleAnimation如何使用。

1. xml文件形式

文件名:anim_scale_in.xml
效果:呈現(xiàn)view放大顯示效果
源碼:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:interpolator="@android:anim/decelerate_interpolator"
        android:duration="1000"
        android:fillAfter="true"
        android:fromXScale="0.0"
        android:fromYScale="0.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.0"
        android:toYScale="1.0" />
</set>

屬性解釋:
interpolator:動(dòng)畫插入器,該功能在xml里設(shè)置貌似無效,需在代碼中加
fromXScale:從自身x軸長(zhǎng)度多少倍開始縮放,如:fromXScale= 0.5表示從自身X軸長(zhǎng)度0.5倍開始縮放
toXScale:縮放到自身x軸長(zhǎng)度多少倍結(jié)束,如:toXScale = 2.0表示x軸縮放到自身x軸長(zhǎng)度2倍結(jié)束
上面兩條意思就是:該view的x軸從自身x軸長(zhǎng)度的0.5倍開始縮放到自身x軸長(zhǎng)度的2倍結(jié)束
fromYScale:從自身y軸長(zhǎng)度多少倍開始縮放,如:fromYScale= 0.5表示從自身y軸長(zhǎng)度0.5倍開始縮放
toYScale:縮放到自身y軸長(zhǎng)度多少倍結(jié)束,如:toYScale = 2.0表示x軸縮放到自身y軸長(zhǎng)度2倍結(jié)束
pivotX:動(dòng)畫相對(duì)于控件X坐標(biāo)的開始位置
pivotY:動(dòng)畫相對(duì)于控件Y坐標(biāo)的開始位置
如:pivotX = 50%,pivotY = 50% 表示從該控件的中心開始縮放

   //表示控件左下角開始
   android:pivotX="0"
   android:pivotY="100%"
   //表示控件左上角開始
   android:pivotX="0"
   android:pivotY="0"
   //表示控件右下角開始
    android:pivotX="100%"
    android:pivotY="100%"
   //表示控件右上角開始
   android:pivotX="100%"
   android:pivotY="0"
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:interpolator="@android:anim/accelerate_interpolator"
        android:duration="1000"
        android:fillAfter="true"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="0"
        android:toYScale="0" />
</set>

OK,現(xiàn)在有了xml布局文件,我們需要用Java代碼讓他工作起來,如下;

 /**
     * 縮放變大動(dòng)畫
     *
     * @param context
     * @param view 目標(biāo)view
     */
    public static void startScaleInAnim(Context context, View view) {
        Animation animation = AnimationUtils.loadAnimation(context, R.anim.anim_scale_in);
        if (view != null)
            view.startAnimation(animation);
    }
    /**
     * 縮放縮小動(dòng)畫
     *
     * @param context
     * @param view 目標(biāo)view
     */
    public static void startScaleOutAnim(Context context, View view) {
        Animation animation = AnimationUtils.loadAnimation(context, R.anim.anim_scale_out);
        if (view != null)
            view.startAnimation(animation);
    }

我單獨(dú)封裝在一個(gè)動(dòng)畫工具類中,哪里需要就哪里調(diào)用。

下面看看代碼的執(zhí)行效果:

縮放同時(shí)還可以添加透明度變化,如下:

放大+淡入:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:interpolator="@android:anim/decelerate_interpolator"
        android:duration="1000"
        android:fillAfter="true"
        android:fillEnabled="true"
        android:fromXScale="0"
        android:fromYScale="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.0"
        android:toYScale="1.0" />
    <!--同時(shí)配置淡入功能-->
    <alpha
        android:duration="700"
        android:fillAfter="true"
        android:fromAlpha="0"
        android:toAlpha="1" />
</set>

縮小+淡出

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:interpolator="@android:anim/accelerate_interpolator"
        android:duration="1000"
        android:fillAfter="true"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="0"
        android:toYScale="0" />
    <!--同時(shí)配置淡出功能-->
    <alpha
        android:duration="700"
        android:fillAfter="true"
        android:fromAlpha="1"
        android:toAlpha="0" />
</set>

效果如下:

下拉菜單顯示與收回,效果:

顯示:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:interpolator="@android:anim/decelerate_interpolator"
        android:duration="300"
        android:fillAfter="true"
        android:fillEnabled="true"
        android:fromXScale="1.0"
        android:fromYScale="0"
        android:pivotX="100%"
        android:pivotY="0"
        android:toXScale="1.0"
        android:toYScale="1.0" />
    <!--同時(shí)配置淡入功能-->
    <alpha
        android:duration="300"
        android:fillAfter="true"
        android:fromAlpha="0"
        android:toAlpha="1" />
</set>

收起:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:interpolator="@android:anim/accelerate_interpolator"
        android:duration="300"
        android:fillAfter="true"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="100%"
        android:pivotY="0"
        android:toXScale="1.0"
        android:toYScale="0" />
    <!--同時(shí)配置淡出功能-->
    <alpha
        android:duration="300"
        android:fillAfter="true"
        android:fromAlpha="1"
        android:toAlpha="0" />
</set>

效果:

縮放下拉與收回效果:

顯示:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:interpolator="@android:anim/decelerate_interpolator"
        android:duration="200"
        android:fillAfter="true"
        android:fillEnabled="true"
        android:fromXScale="0"
        android:fromYScale="0"
        android:pivotX="100%"
        android:pivotY="0"
        android:toXScale="1.0"
        android:toYScale="1.0" />
    <!--同時(shí)配置淡入功能-->
    <alpha
        android:duration="200"
        android:fillAfter="true"
        android:fromAlpha="0"
        android:toAlpha="1" />
</set>

收起:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:interpolator="@android:anim/accelerate_interpolator"
        android:duration="200"
        android:fillAfter="true"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="100%"
        android:pivotY="0"
        android:toXScale="0"
        android:toYScale="0" />
    <!--同時(shí)配置淡出功能-->
    <alpha
        android:duration="200"
        android:fillAfter="true"
        android:fromAlpha="1"
        android:toAlpha="0" />
</set>

效果:

類似游戲按鈕的按下放大再還原效果:

  public static void animScaleIn(View view){
        //縮放動(dòng)畫
        ScaleAnimation animation = new ScaleAnimation(1,1.2f,1,1.2f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        animation.setDuration(100);
        animation.setFillAfter(true);
        animation.setRepeatMode(Animation.REVERSE);
        animation.setRepeatCount(1);
        //透明度動(dòng)畫
        AlphaAnimation animation1 = new AlphaAnimation(1,0.8f);
        animation1.setDuration(100);
        animation1.setRepeatCount(1);
        animation1.setRepeatMode(Animation.REVERSE);
        animation1.setFillAfter(true);
        //裝入AnimationSet中
        AnimationSet set = new AnimationSet(true);
        set.addAnimation(animation);
        set.addAnimation(animation1);
        if (view != null)
        view.startAnimation(set);
    }

效果如下:

備注:由于我的圖片是導(dǎo)出視頻再用PS轉(zhuǎn)換成的gif,故效率上有所損失,實(shí)際動(dòng)畫效果和速度比圖片的快。

到此這篇關(guān)于Android 縮放動(dòng)畫 ScaleAnimation的文章就介紹到這了,更多相關(guān)Android ScaleAnimation內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論