Android?縮放動(dòng)畫?ScaleAnimation的使用小結(jié)
什么是ScaleAnimation
ScaleAnimation即縮放動(dòng)畫,應(yī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軸長度多少倍開始縮放,如:fromXScale= 0.5表示從自身X軸長度0.5倍開始縮放
toXScale:縮放到自身x軸長度多少倍結(jié)束,如:toXScale = 2.0表示x軸縮放到自身x軸長度2倍結(jié)束
上面兩條意思就是:該view的x軸從自身x軸長度的0.5倍開始縮放到自身x軸長度的2倍結(jié)束
fromYScale:從自身y軸長度多少倍開始縮放,如:fromYScale= 0.5表示從自身y軸長度0.5倍開始縮放
toYScale:縮放到自身y軸長度多少倍結(jié)束,如:toYScale = 2.0表示x軸縮放到自身y軸長度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)文章
Android Studio如何為Activity添加自定義注解信息
好久沒用寫文章了,今天給大家分享Android Studio如何為Activity添加自定義注解信息,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-06-06
Android開發(fā)之開發(fā)者頭條APP(三)實(shí)現(xiàn)首頁
這篇文章主要介紹了Android開發(fā)之開發(fā)者頭條APP(三)實(shí)現(xiàn)首頁的相關(guān)資料,需要的朋友可以參考下2016-04-04
Android開發(fā)筆記之:Handler Runnable與Thread的區(qū)別詳解
本篇文章是對(duì)在Android中Handler Runnable與Thread的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Android 中SP與DP的區(qū)別實(shí)例詳解
這篇文章主要介紹了Android 中SP與DP的區(qū)別實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2016-10-10
Android Studio 全屏沉浸式透明狀態(tài)欄效果的實(shí)現(xiàn)
這篇文章主要介紹了Android Studio 全屏沉浸式透明狀態(tài)欄效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
使用RecyclerView實(shí)現(xiàn)水平列表
這篇文章主要為大家詳細(xì)介紹了使用RecyclerView實(shí)現(xiàn)水平列表,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09
Android 開發(fā) 使用WebUploader解決安卓微信瀏覽器上傳圖片中遇到的bug
這篇文章主要介紹了Android 開發(fā) 使用WebUploader解決安卓微信瀏覽器上傳圖片中遇到的bug問題,本文給介紹的非常詳細(xì),需要的朋友可以參考下2016-11-11

