华域联盟 Andriod android 实现侧边弹窗特效代码

android 实现侧边弹窗特效代码

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

先大概讲下基本原理吧,其实很简单,就是一个进出动效,用 位移 加 透明度 效果比较好,
比如你的侧边弹窗是在左边,那就是从左往右位置 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>

最后是代码去触发动画:

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 实现侧边弹窗特效代码的详细内容,更多关于android侧边弹窗的资料请关注华域联盟其它相关文章!

本文由 华域联盟 原创撰写:华域联盟 » android 实现侧边弹窗特效代码

转载请保留出处和原文链接:https://www.cnhackhy.com/109453.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

详解dex优化对Arouter查找路径的影响

Android实现简易计算功能

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们