android 實現(xiàn)側(cè)邊彈窗特效代碼
大家好哇,又是我,夢辛工作室的靈,今天來給大家講解下如何實現(xiàn) 安卓的側(cè)邊彈窗,
先大概講下基本原理吧,其實很簡單,就是一個進出動效,用 位移 加 透明度 效果比較好,
比如你的側(cè)邊彈窗是在左邊,那就是從左往右位置 100%(代表動效目標的寬或高)
不過需要注意:
初始位置一定要先最后應該顯示的位置,不要將該View使用Margin或其他位移至其他位置,不然動效結(jié)束后,點擊視圖沒有響應,因為此時View還在初始位置,所以你點擊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"> <!--透明度標簽:表示透明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"> <!--透明度標簽:表示透明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ā)點擊兇過 view.clearAnimation(); } @Override public void onAnimationRepeat(Animation animation) { } });
其實還可以進階一下,監(jiān)聽界面左邊部分的手勢,當按下點與抬起點之間的橫向距離達到一定值時啟動,入場動畫或者出場動畫,就可以達到通過手勢觸發(fā)或關(guān)閉側(cè)邊彈窗效果了,總體還是很簡單的,大家可以試試
以上就是android 實現(xiàn)側(cè)邊彈窗特效代碼的詳細內(nèi)容,更多關(guān)于android側(cè)邊彈窗的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android從源碼的角度徹底理解事件分發(fā)機制的解析(下)
這篇文章主要介紹了Android從源碼的角度徹底理解事件分發(fā)機制的解析(下),具有很好的參考價值,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05Android 靜默方式實現(xiàn)批量安裝卸載應用程序的深入分析
本篇文章是對Android 靜默方式實現(xiàn)批量安裝卸載應用程序進行了詳細的分析介紹,需要的朋友參考下2013-06-06android中px和dp,px和sp之間的轉(zhuǎn)換方法
在Android開發(fā)中dp和px,sp和px之間的轉(zhuǎn)換時必不可少的。下面腳本之家小編給大家?guī)砹薬ndroid中px和dp,px和sp之間的轉(zhuǎn)換方法,感興趣的朋友一起看看吧2018-06-06