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

<?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下寫動畫文件:
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ā)動畫:
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) {
//一定要記得,動畫結(jié)束后清除動畫,然后及時View 處于 View.GONE狀態(tài)時也會觸發(fā)點(diǎn)擊兇過
view.clearAnimation();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
其實(shí)還可以進(jìn)階一下,監(jiān)聽界面左邊部分的手勢,當(dāng)按下點(diǎn)與抬起點(diǎn)之間的橫向距離達(dá)到一定值時啟動,入場動畫或者出場動畫,就可以達(dá)到通過手勢觸發(fā)或關(guān)閉側(cè)邊彈窗效果了,總體還是很簡單的,大家可以試試
以上就是android 實(shí)現(xiàn)側(cè)邊彈窗特效代碼的詳細(xì)內(nèi)容,更多關(guān)于android側(cè)邊彈窗的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android編程連接MongoDB及增刪改查等基本操作示例
這篇文章主要介紹了Android編程連接MongoDB及增刪改查等基本操作,簡單介紹了MongoDB功能、概念、使用方法及Android操作MongoDB數(shù)據(jù)庫的基本技巧,需要的朋友可以參考下2017-07-07
深入學(xué)習(xí)Kotlin?枚舉的簡潔又高效進(jìn)階用法
這篇文章主要為大家介紹了深入學(xué)習(xí)Kotlin?枚舉簡潔又高效的進(jìn)階用法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
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ì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03
解決Android Studio 出現(xiàn)“Cannot resolve symbo
今天在調(diào)試的時候,Android Studio報(bào)了一個莫名其妙的錯誤Cannot resolve symbol'R'讓人不知所措,因?yàn)檫@東西根本不歸我管啊,怎么會出現(xiàn) Cannot resolve symbol 這種錯誤呢?下面給大家分享Android Studio 出現(xiàn)“Cannot resolve symbol”解決方案,需要的朋友可以參考下2023-03-03

