android 實(shí)現(xiàn)側(cè)邊彈窗特效代碼
大家好哇,又是我,夢(mèng)辛工作室的靈,今天來(lái)給大家講解下如何實(shí)現(xiàn) 安卓的側(cè)邊彈窗,
先大概講下基本原理吧,其實(shí)很簡(jiǎn)單,就是一個(gè)進(jìn)出動(dòng)效,用 位移 加 透明度 效果比較好,
比如你的側(cè)邊彈窗是在左邊,那就是從左往右位置 100%(代表動(dòng)效目標(biāo)的寬或高)
不過(guò)需要注意:
初始位置一定要先最后應(yīng)該顯示的位置,不要將該View使用Margin或其他位移至其他位置,不然動(dòng)效結(jié)束后,點(diǎn)擊視圖沒(méi)有響應(yīng),因?yàn)榇藭r(shí)View還在初始位置,所以你點(diǎn)擊View僅動(dòng)畫(huà)修改過(guò)后的位置是無(wú)效的,除非你使用的是屬性動(dòng)畫(huà)
下面來(lái)看下我的布局,簡(jiǎn)單寫(xiě)了一個(gè):

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:id="@+id/rel_dialog_back"
android:background="#B3000000"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- 商品信息彈窗 -->
<LinearLayout
android:layout_alignParentRight="true"
android:id="@+id/lin_dialog_content"
android:layout_width="400dp"
android:layout_height="match_parent"
android:padding="10dp"
android:background="#FFFFFF"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="我是彈窗"
android:textColor="@color/colorAccent"
android:gravity="center"
android:textSize="80sp"
android:layout_gravity="center"/>
</LinearLayout>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
然后就是res/anim下寫(xiě)動(dòng)畫(huà)文件:
dialog_in.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:interpolator="@android:anim/decelerate_interpolator">
<!--透明度標(biāo)簽:表示透明0到不透明1之間的變換-->
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0" >
</alpha>
<!-- 100% 代表向右 視圖寬度, 0%代表視圖初始位置 -->
<translate
android:fromXDelta="100%"
android:toXDelta="0%">
</translate>
</set>
dialog_out.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:interpolator="@android:anim/decelerate_interpolator">
<!--透明度標(biāo)簽:表示透明0到不透明1之間的變換-->
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0" >
</alpha>
<translate
android:fromXDelta="0%"
android:toXDelta="100%">
</translate>
</set>
最后是代碼去觸發(fā)動(dòng)畫(huà):
final Animation anim = AnimationUtils.loadAnimation(this, R.anim.dialog_in);
anim.setDuration(300);
anim.setFillAfter(true);
view.startAnimation(anim );
anim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
//一定要記得,動(dòng)畫(huà)結(jié)束后清除動(dòng)畫(huà),然后及時(shí)View 處于 View.GONE狀態(tài)時(shí)也會(huì)觸發(fā)點(diǎn)擊兇過(guò)
view.clearAnimation();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
其實(shí)還可以進(jìn)階一下,監(jiān)聽(tīng)界面左邊部分的手勢(shì),當(dāng)按下點(diǎn)與抬起點(diǎn)之間的橫向距離達(dá)到一定值時(shí)啟動(dòng),入場(chǎng)動(dòng)畫(huà)或者出場(chǎng)動(dòng)畫(huà),就可以達(dá)到通過(guò)手勢(shì)觸發(fā)或關(guān)閉側(cè)邊彈窗效果了,總體還是很簡(jiǎn)單的,大家可以試試
以上就是android 實(shí)現(xiàn)側(cè)邊彈窗特效代碼的詳細(xì)內(nèi)容,更多關(guān)于android側(cè)邊彈窗的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android編程連接MongoDB及增刪改查等基本操作示例
這篇文章主要介紹了Android編程連接MongoDB及增刪改查等基本操作,簡(jiǎn)單介紹了MongoDB功能、概念、使用方法及Android操作MongoDB數(shù)據(jù)庫(kù)的基本技巧,需要的朋友可以參考下2017-07-07
Android開(kāi)發(fā)軟鍵盤(pán)遮擋登陸按鈕的完美解決方案
在應(yīng)用登陸頁(yè)面我們需要填寫(xiě)用戶(hù)名和密碼。當(dāng)填寫(xiě)這些信息的時(shí)候,軟鍵盤(pán)會(huì)遮擋登陸按鈕,這使得用戶(hù)體驗(yàn)較差。今天小編給大家分享本教程給大家介紹解決android軟鍵盤(pán)遮擋登陸按鈕的方法,感興趣的朋友一起學(xué)習(xí)吧2016-10-10
深入學(xué)習(xí)Kotlin?枚舉的簡(jiǎn)潔又高效進(jìn)階用法
這篇文章主要為大家介紹了深入學(xué)習(xí)Kotlin?枚舉簡(jiǎn)潔又高效的進(jìn)階用法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
Android仿高德首頁(yè)三段式滑動(dòng)效果的示例代碼
很多app都會(huì)使用三段式滑動(dòng),比如說(shuō)高德的首頁(yè)和某寶等物流信息都是使用的三段式滑動(dòng)方式。本文將介紹如何實(shí)現(xiàn)這一效果,感興趣的可以學(xué)習(xí)一下2022-01-01
Android App中自定義View視圖的實(shí)例教程
這篇文章主要介紹了Android App中自定義View視圖的實(shí)例教程,詳細(xì)講解了如何在創(chuàng)建View中定義各種鎖需要的樣式屬性,需要的朋友可以參考下2016-04-04
Android實(shí)現(xiàn)ViewPage輪播圖效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)ViewPage輪播圖效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
android小動(dòng)畫(huà):不斷擴(kuò)散的圓點(diǎn)
這篇文章介紹了如何實(shí)現(xiàn)android小動(dòng)畫(huà):不斷擴(kuò)散的圓點(diǎn),文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,下面的實(shí)例代碼,大家可以看看2021-11-11
解決Android Studio 出現(xiàn)“Cannot resolve symbo
今天在調(diào)試的時(shí)候,Android Studio報(bào)了一個(gè)莫名其妙的錯(cuò)誤Cannot resolve symbol'R'讓人不知所措,因?yàn)檫@東西根本不歸我管啊,怎么會(huì)出現(xiàn) Cannot resolve symbol 這種錯(cuò)誤呢?下面給大家分享Android Studio 出現(xiàn)“Cannot resolve symbol”解決方案,需要的朋友可以參考下2023-03-03

