软件编程
位置:首页>> 软件编程>> Android编程>> android 实现侧边弹窗特效代码

android 实现侧边弹窗特效代码

作者:灵神翁  发布时间:2021-10-18 07:23:02 

标签:android,侧边,弹窗

大家好哇,又是我,梦辛工作室的灵,今天来给大家讲解下如何实现 安卓的侧边弹窗,

先大概讲下基本原理吧,其实很简单,就是一个进出动效,用 位移 加 透明度 效果比较好,
比如你的侧边弹窗是在左边,那就是从左往右位置 100%(代表动效目标的宽或高)
不过需要注意:
初始位置一定要先最后应该显示的位置,不要将该View使用Margin或其他位移至其他位置,不然动效结束后,点击视图没有响应,因为此时View还在初始位置,所以你点击View仅动画修改过后的位置是无效的,除非你使用的是属性动画
下面来看下我的布局,简单写了一个:

android 实现侧边弹窗特效代码


<?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>

最后是代码去触发动画:


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状态时也会触发点击凶过
               view.clearAnimation();
           }

@Override
           public void onAnimationRepeat(Animation animation) {

}
       });

其实还可以进阶一下,监听界面左边部分的手势,当按下点与抬起点之间的横向距离达到一定值时启动,入场动画或者出场动画,就可以达到通过手势触发或关闭侧边弹窗效果了,总体还是很简单的,大家可以试试

来源:https://blog.csdn.net/weixin_41392105/article/details/118023294

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com