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

