温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

android怎么实现侧边弹窗特效

发布时间:2022-04-16 16:50:57 来源:亿速云 阅读:362 作者:iii 栏目:开发技术

本篇内容主要讲解“android怎么实现侧边弹窗特效”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“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) {             }         });

到此,相信大家对“android怎么实现侧边弹窗特效”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI