android 實現(xiàn)側邊彈窗特效代碼
大家好哇,又是我,夢辛工作室的靈,今天來給大家講解下如何實現(xiàn) 安卓的側邊彈窗,
先大概講下基本原理吧,其實很簡單,就是一個進出動效,用 位移 加 透明度 效果比較好,
比如你的側邊彈窗是在左邊,那就是從左往右位置 100%(代表動效目標的寬或高)
不過需要注意:
初始位置一定要先最后應該顯示的位置,不要將該View使用Margin或其他位移至其他位置,不然動效結束后,點擊視圖沒有響應,因為此時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) {
//一定要記得,動畫結束后清除動畫,然后及時View 處于 View.GONE狀態(tài)時也會觸發(fā)點擊兇過
view.clearAnimation();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
其實還可以進階一下,監(jiān)聽界面左邊部分的手勢,當按下點與抬起點之間的橫向距離達到一定值時啟動,入場動畫或者出場動畫,就可以達到通過手勢觸發(fā)或關閉側邊彈窗效果了,總體還是很簡單的,大家可以試試
以上就是android 實現(xiàn)側邊彈窗特效代碼的詳細內(nèi)容,更多關于android側邊彈窗的資料請關注腳本之家其它相關文章!
相關文章
Android編程連接MongoDB及增刪改查等基本操作示例
這篇文章主要介紹了Android編程連接MongoDB及增刪改查等基本操作,簡單介紹了MongoDB功能、概念、使用方法及Android操作MongoDB數(shù)據(jù)庫的基本技巧,需要的朋友可以參考下2017-07-07
解決Android Studio 出現(xiàn)“Cannot resolve symbo
今天在調(diào)試的時候,Android Studio報了一個莫名其妙的錯誤Cannot resolve symbol'R'讓人不知所措,因為這東西根本不歸我管啊,怎么會出現(xiàn) Cannot resolve symbol 這種錯誤呢?下面給大家分享Android Studio 出現(xiàn)“Cannot resolve symbol”解決方案,需要的朋友可以參考下2023-03-03

