百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 热门文章 > 正文

android非侵入式弹窗(仿bottomSheet)

bigegpt 2024-08-21 12:01 2 浏览

首先看效果图

仿地图弹窗

当然了这个高度和宽度是可控的。想详细修改的话去代码里面修改吧!


使用方法

BouncingMenu.makeMenu(findViewById(R.id.rl), R.layout.layout_rv_sweet, adapter).show();

是的,就是这一行代码。只需要传入view,布局id,以及adapter(这里使用的是recycleview.Adapter)。想修改其他的话直接去修改代码吧。

具体实现

  • 首先是activity的布局,很简单,就是放了各种ImageView,然后上面放了个searchView(纯粹为了增加样式,囧,这个不重要)


弹出视图

  1. RecycleView部分。从上往下的动画效果使用的是LayoutAnimation.

    布局:

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@android:color/transparent"

android:orientation="vertical">

<RelativeLayout

android:id="@+id/freeGrowUpParent"

android:layout_width="match_parent"

android:layout_height="@dimen/sheet_height"

android:layout_gravity="bottom"

android:background="@android:color/transparent">

<!--自定义控件,就是那个波动view-->

<com.xie.designpatterns.widget.BouncingView

android:id="@+id/sv"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginLeft="10dp"

android:layout_marginRight="10dp" />

<android.support.v7.widget.RecyclerView

android:id="@+id/rv"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_gravity="bottom"

android:layout_marginLeft="10dp"

android:layout_marginRight="10dp"

android:layout_marginTop="30dp"

android:clipChildren="false"

android:overScrollMode="never" />

</RelativeLayout>

</FrameLayout>

2. BouncingMenu(弹出的主视图)

public class BouncingMenu {

private final ViewGroup mParentViewGroup;

private View rootView;

private final BouncingView boucingView;

private final RecyclerView recycleView;

private RecyclerView.Adapter adapter;

private int screenWidht;

private int screenHeight;

/**

* @param resId 布局资源id

* @param adapter

*/

public BouncingMenu(View view, int resId, RecyclerView.Adapter adapter) {

this.adapter = adapter;

//不断地往上追溯找到 系统id为content的帧布局,添加进去.这里就是利用了这个。

// @android:id/content 就是锚点view

mParentViewGroup = findRootParent(view);

//渲染

rootView = LayoutInflater.from(view.getContext()).inflate(resId, null, false);

//拿到高度,然后把rootview的高度设置为0

Logger.i("视图的跟布局是" + rootView);

boucingView = (BouncingView) rootView.findViewById(R.id.sv);

boucingView.setAnimationListener(new MyAnimationListener());

recycleView = (RecyclerView) rootView.findViewById(R.id.rv);

recycleView.setAnimation(AnimationUtils.loadAnimation(view.getContext(), R.anim.rv_layout_animation));

recycleView.setLayoutManager(new LinearLayoutManager(view.getContext()));

getScreentWight(view.getContext());

}

private ViewGroup findRootParent(View view) {

do {

//判断是帧布局

if (view instanceof FrameLayout) {

if (view.getId() == android.R.id.content) {

//判断布局id为content

return (ViewGroup) view;

}

}

//没找到 ,就不断的往上找

if (view != null) {

ViewParent parent = view.getParent();

//如果父布局是view的话往上继续找

view = parent instanceof View ? (View) parent : null;

}

} while (view != null);

return null;

}

public static BouncingMenu makeMenu(View view, int resId, RecyclerView.Adapter adapter) {

return new BouncingMenu(view, resId, adapter);

}

/**

* 显示

*

* @return

*/

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)

public BouncingMenu show() {

//显示,往帧布局里面addView(xxxView)

if (rootView.getParent() != null) {

//防止已经show了再次show

mParentViewGroup.removeView(rootView);

}

FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);

rootView.setLayoutParams(params);

mParentViewGroup.addView(rootView);

boucingView.show();

return this;

}

/**

* 关闭

*/

public void dissmiss() {

//属性动画实现消失

ObjectAnimator animator = ObjectAnimator.ofFloat(rootView, "translationY", 0, rootView.getHeight());

animator.setDuration(1000);

animator.addListener(new AnimatorListenerAdapter() {

@Override

public void onAnimationEnd(Animator animation) {

//动画结束

super.onAnimationEnd(animation);

mParentViewGroup.removeView(rootView);

rootView = null;

}

});

animator.start();

}

private class MyAnimationListener implements BouncingView.AnimationListener {

@Override

public void onShowContent() {

recycleView.setVisibility(View.VISIBLE);

recycleView.setAdapter(adapter);

recycleView.scheduleLayoutAnimation();

}

}

private void getScreentWight(Context context) {

WindowManager wm = (WindowManager) context

.getSystemService(Context.WINDOW_SERVICE);

screenWidht = wm.getDefaultDisplay().getWidth();

screenHeight = wm.getDefaultDisplay().getHeight();

}

}

2. BouncingView(就是主视图上面弹动的那一部分,这里做成了一个自定义view)

public class BouncingView extends View {

private Paint mPaint;

private int mArcHeight;//当前弧度

private int mMaxArcHeight;//最高弧度

private Status mStatus = Status.NONE;

private Path mPath = new Path();

private AnimationListener animationListener;

private enum Status {

NONE,

STATUS_SMOOTH_UP,

STATUS_DOWN

}

public BouncingView(Context context) {

super(context);

init();

}

public BouncingView(Context context, AttributeSet attrs) {

super(context, attrs);

init();

}

private void init() {

mPaint = new Paint();

mPaint.setAntiAlias(true);

mPaint.setStyle(Paint.Style.FILL);

mPaint.setColor(getResources().getColor(android.R.color.white));

mMaxArcHeight = getResources().getDimensionPixelSize(R.dimen.arc_max_height);

}

//绘制贝塞尔曲线

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

int currentPointY = 0;

//判断当前状态

switch (mStatus) {

case NONE:

currentPointY = 0;

break;

case STATUS_SMOOTH_UP://往上走

//currentPointY 的值 跟mArcHeight变化率是一样的

//getHeight()~0 (自定义控件最上面是0) 跟mArcHeight:0~mMaxArcHeight

currentPointY = (int) (getHeight() * (1 - (float) mArcHeight / mMaxArcHeight) + mMaxArcHeight);

break;

case STATUS_DOWN://往下走

currentPointY = mMaxArcHeight;

break;

}

mPath.reset();

//先落笔到左上角

mPath.moveTo(0, currentPointY);

//画上面的线二阶贝塞尔曲线 ,需要起始点 终点,跟拐点。起点就是(0,currentPotintY),拐点就是最中间(getWidth()/2,)

mPath.quadTo(getWidth() / 2, currentPointY - mArcHeight, getWidth(), currentPointY);

//右边线

mPath.lineTo(getWidth(), getHeight());

//最下面的直线

mPath.lineTo(0, getHeight());

//自动画最后一条线

mPath.close();

canvas.drawPath(mPath, mPaint);

}

public void show() {

if (animationListener != null) {

this.postDelayed(new Runnable() {

@Override

public void run() {

//延迟显示数据

animationListener.onShowContent();

}

}, 600);

}

//不短的控制mArcHeight的高度

mStatus = Status.STATUS_SMOOTH_UP;

ValueAnimator valueAnimator = ValueAnimator.ofInt(0, mMaxArcHeight);

valueAnimator.setDuration(800);

valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

@Override

public void onAnimationUpdate(ValueAnimator animation) {

mArcHeight = (int) animation.getAnimatedValue();

if (mArcHeight == mMaxArcHeight) {

//出现谈一下动画

bounce();

}

//不断的刷新调用ondraw方法

invalidate();

}

});

valueAnimator.start();

}

/**

* 回弹动画

*/

private void bounce() {

mStatus = Status.STATUS_DOWN;

ValueAnimator valueAnimator = ValueAnimator.ofInt(mMaxArcHeight, 0);

valueAnimator.setDuration(800);

valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

@Override

public void onAnimationUpdate(ValueAnimator animation) {

mArcHeight = (int) animation.getAnimatedValue();

//不断的刷新调用ondraw方法

invalidate();

}

});

valueAnimator.start();

}

public void setAnimationListener(AnimationListener listener) {

this.animationListener = listener;

}

public interface AnimationListener {

void onShowContent();

}

}

有什么具体需求可以直接修改,代码总共就这么多。

同时上传到github:https://github.com/Xiemarc/DesignPatterns

相关推荐

悠悠万事,吃饭为大(悠悠万事吃饭为大,什么意思)

新媒体编辑:杜岷赵蕾初审:程秀娟审核:汤小俊审签:周星...

高铁扒门事件升级版!婚宴上‘冲喜’老人团:我们抢的是社会资源

凌晨两点改方案时,突然收到婚庆团队发来的视频——胶东某酒店宴会厅,三个穿大红棉袄的中年妇女跟敢死队似的往前冲,眼瞅着就要扑到新娘的高额钻石项链上。要不是门口小伙及时阻拦,这婚礼造型团队熬了三个月的方案...

微服务架构实战:商家管理后台与sso设计,SSO客户端设计

SSO客户端设计下面通过模块merchant-security对SSO客户端安全认证部分的实现进行封装,以便各个接入SSO的客户端应用进行引用。安全认证的项目管理配置SSO客户端安全认证的项目管理使...

还在为 Spring Boot 配置类加载机制困惑?一文为你彻底解惑

在当今微服务架构盛行、项目复杂度不断攀升的开发环境下,SpringBoot作为Java后端开发的主流框架,无疑是我们手中的得力武器。然而,当我们在享受其自动配置带来的便捷时,是否曾被配置类加载...

Seata源码—6.Seata AT模式的数据源代理二

大纲1.Seata的Resource资源接口源码2.Seata数据源连接池代理的实现源码3.Client向Server发起注册RM的源码4.Client向Server注册RM时的交互源码5.数据源连接...

30分钟了解K8S(30分钟了解微积分)

微服务演进方向o面向分布式设计(Distribution):容器、微服务、API驱动的开发;o面向配置设计(Configuration):一个镜像,多个环境配置;o面向韧性设计(Resista...

SpringBoot条件化配置(@Conditional)全面解析与实战指南

一、条件化配置基础概念1.1什么是条件化配置条件化配置是Spring框架提供的一种基于特定条件来决定是否注册Bean或加载配置的机制。在SpringBoot中,这一机制通过@Conditional...

一招解决所有依赖冲突(克服依赖)

背景介绍最近遇到了这样一个问题,我们有一个jar包common-tool,作为基础工具包,被各个项目在引用。突然某一天发现日志很多报错。一看是NoSuchMethodError,意思是Dis...

你读过Mybatis的源码?说说它用到了几种设计模式

学习设计模式时,很多人都有类似的困扰——明明概念背得滚瓜烂熟,一到写代码就完全想不起来怎么用。就像学了一堆游泳技巧,却从没下过水实践,很难真正掌握。其实理解一个知识点,就像看立体模型,单角度观察总...

golang对接阿里云私有Bucket上传图片、授权访问图片

1、为什么要设置私有bucket公共读写:互联网上任何用户都可以对该Bucket内的文件进行访问,并且向该Bucket写入数据。这有可能造成您数据的外泄以及费用激增,若被人恶意写入违法信息还可...

spring中的资源的加载(spring加载原理)

最近在网上看到有人问@ContextConfiguration("classpath:/bean.xml")中除了classpath这种还有其他的写法么,看他的意思是想从本地文件...

Android资源使用(android资源文件)

Android资源管理机制在Android的开发中,需要使用到各式各样的资源,这些资源往往是一些静态资源,比如位图,颜色,布局定义,用户界面使用到的字符串,动画等。这些资源统统放在项目的res/独立子...

如何深度理解mybatis?(如何深度理解康乐服务质量管理的5个维度)

深度自定义mybatis回顾mybatis的操作的核心步骤编写核心类SqlSessionFacotryBuild进行解析配置文件深度分析解析SqlSessionFacotryBuild干的核心工作编写...

@Autowired与@Resource原理知识点详解

springIOCAOP的不多做赘述了,说下IOC:SpringIOC解决的是对象管理和对象依赖的问题,IOC容器可以理解为一个对象工厂,我们都把该对象交给工厂,工厂管理这些对象的创建以及依赖关系...

java的redis连接工具篇(java redis client)

在Java里,有不少用于连接Redis的工具,下面为你介绍一些主流的工具及其特点:JedisJedis是Redis官方推荐的Java连接工具,它提供了全面的Redis命令支持,且...