作者:吾乃韩小呆

链接:

https://www.jianshu.com/p/8e7b339d7a78

帝都几日降温,终于被撂倒了。but  只要一息尚存就得不断进步!于是,写出 《PopupWindow 使用详解》的第二篇 笔记,先奉上 第一篇链接: 《PopupWindow 使用详解(一) 中文API 文档 赠送 ListPopupWindow 中文 API》 。

https://www.jianshu.com/p/3a8dd7a4b41a

下面给大家展示一下制作的效果gif。

下面进行一个样式一个样式的肢解哈,对了,所有效果笔者都没有制作载入动画和退出动画。有需要的小伙伴可以通过 这个方法 public void setAnimationStyle(int animationStyle) 进行设置,也是很简单、很常用的。

效果一、图片选取功能(带阴影)

1、布局设置

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/ll_pic"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@drawable/shape_pic_select"    android:gravity="bottom"    android:orientation="vertical">

    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginStart="5dp"        android:layout_marginEnd="5dp"        android:orientation="vertical">

        <Button            android:id="@+id/btn_pic_photo"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginBottom="1dp"            android:background="#ffffff"            android:text="相  册"            android:textColor="#3c3c3c"            android:textSize="16sp" />

        <Button            android:id="@+id/btn_pic_camera"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginBottom="1dp"            android:background="#ffffff"            android:text="拍  照"            android:textColor="#3c3c3c"            android:textSize="16sp" />

        <Button            android:id="@+id/btn_pic_cancel"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginBottom="1dp"            android:background="#ffffff"            android:text="取  消"            android:textColor="#3c3c3c"            android:textSize="16sp" />    </LinearLayout></LinearLayout>

2、Java 逻辑代码

/**     * 照片选择器     */    @SuppressLint("InflateParams")    private void showPicSelect() {        view = LayoutInflater.from(this).inflate(R.layout.item_pic_select, null, false);        LinearLayout llPop = view.findViewById(R.id.ll_pic);        Button btnCamera = view.findViewById(R.id.btn_pic_camera);        Button btnPhoto = view.findViewById(R.id.btn_pic_photo);        Button btnCancel = view.findViewById(R.id.btn_pic_cancel);

        btnCamera.setOnClickListener(this);        btnPhoto.setOnClickListener(this);        btnCancel.setOnClickListener(this);        llPop.setOnClickListener(this);

        myPop = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);        myPop.setBackgroundDrawable(new ColorDrawable());        myPop.showAtLocation(rlMain, Gravity.BOTTOM, 0, 0);    }

   @Override    public void onBackPressed() {        if (myPop.isShowing()) {            myPop.dismiss();        } else {            super.onBackPressed();        }    }

3、实现思路

之前笔者看了看网上百度来的答案,实现阴影效果的思路大概是,当 PopupWindow 弹出时将 Activity 设置为半透明,但是这种思路的弊端是 Activity 透明了,你懂得,你可以在 A Activity 界面直接看到了 桌面或者是 B Activity 界面的东西,很蛋疼。

笔者的思路是:为 PopupWindow 设置一个半透明的背景色,然后监听这不背景 layout 的点击事件,和物理键的返回事件。否则会出现点击无效果的现象。具体逻辑如上。

仿qq和微信的长按置顶删除功能

1、布局

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent">

    <LinearLayout        android:id="@+id/ll_qq"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:background="@drawable/shape_qq"        android:orientation="horizontal"        tools:ignore="UselessParent">

        <TextView            android:id="@+id/tv_delete"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:padding="10dp"            android:text="删除"            android:textColor="#ffffff"            android:textSize="16sp" />

        <View            android:layout_width="2dp"            android:layout_height="match_parent"            android:layout_marginTop="5dp"            android:layout_marginBottom="5dp"            android:background="#666666" />

        <TextView            android:id="@+id/tv_be_top"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:padding="10dp"            android:text="置顶"            android:textColor="#ffffff"            android:textSize="16sp" />

    </LinearLayout>    <ImageView        android:id="@+id/iv_three"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/ll_qq"        android:layout_centerHorizontal="true"        android:background="@null"        android:layout_marginTop="-5dp"        android:contentDescription="@string/app_name"        android:src="@mipmap/ic_three" /></RelativeLayout>

2、Java 逻辑

/** * 仿qq 产生水滴按钮 */@SuppressLint("InflateParams")private void showQq() {    view = LayoutInflater.from(this).inflate(R.layout.item_qq, null, false);    TextView tvTop = view.findViewById(R.id.tv_be_top);    TextView tvDelete = view.findViewById(R.id.tv_delete);    tvDelete.setOnClickListener(this);    tvTop.setOnClickListener(this);

    myPop = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);    myPop.setBackgroundDrawable(new ColorDrawable());    myPop.setOutsideTouchable(true);    myPop.getContentView().measure(0, 0);    myPop.showAsDropDown(cvMain, (cvMain.getWidth() - myPop.getContentView().getMeasuredWidth()) / 2,            -(cvMain.getHeight() + myPop.getContentView().getMeasuredHeight()));}

3、实现思路

这个其实没什么好说的,但是需要注意的两点是:

(1)、ui 一定要有的或者是自己会个ps 也行,仔细看笔者布局,有一个地方,设置 margin 属性居然用了 负值 否则无法保证 下面的shape 背景与三角标进行无缝衔接;

(2)、注意这个方法一定要设置即便是不设置值 public void setBackgroundDrawable(Drawable background) 否则会导致 public void setOutsideTouchable(boolean touchable) 这个方法不起作用,即出现点击 PopupWindow 外部区域无法隐藏 PopupWindow 的尴尬局面.

实现悬浮图片轮播

1、布局代码

<!--布局 1--><?xml version="1.0" encoding="utf-8"?><android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:background="#00000000"    app:cardCornerRadius="10dp">

    <android.support.v4.view.ViewPager        android:id="@+id/vp_pop"        android:layout_width="200dp"        android:layout_height="300dp"        android:background="#48BAFF" />

</android.support.v7.widget.CardView>

<!--布局 2--><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="center"    android:orientation="vertical">

    <ImageView        android:layout_width="200dp"        android:layout_height="300dp"        android:contentDescription="@string/app_name"        android:src="@mipmap/pic_1" /></LinearLayout>

2、Java 逻辑代码

/** * 轮播效果 */@SuppressLint("InflateParams")private void showPager() {    views = new ArrayList<>();    view = LayoutInflater.from(this).inflate(R.layout.item_pager, null, false);    ViewPager vpPop = view.findViewById(R.id.vp_pop);    picView01 = LayoutInflater.from(this).inflate(R.layout.item_pop_vp_01, null, false);    picView02 = LayoutInflater.from(this).inflate(R.layout.item_pop_vp_02, null, false);    picView03 = LayoutInflater.from(this).inflate(R.layout.item_pop_vp_03, null, false);    picView04 = LayoutInflater.from(this).inflate(R.layout.item_pop_vp_04, null, false);

    views.add(picView01);    views.add(picView02);    views.add(picView03);    views.add(picView04);    vpPop.setAdapter(new MyPopAdapter());

    myPop = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);    myPop.setOutsideTouchable(true);    //悬浮效果    myPop.setElevation(5);    myPop.setBackgroundDrawable(new ColorDrawable(0x00ffffff));    myPop.showAtLocation(rlMain, Gravity.CENTER, 0, 0);}

/** * 配置  adapter */class MyPopAdapter extends PagerAdapter {

    @Override    public int getCount() {        return views.size();    }

    @Override    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {        return view == o;    }

    @NonNull    @Override    public Object instantiateItem(@NonNull ViewGroup container, int position) {        container.addView(views.get(position));        return views.get(position);    }

    @Override    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {        container.removeView(views.get(position));    }}

@Overrideprotected void onDestroy() {    super.onDestroy();    if (views != null) {        views.remove(picView01);        views.remove(picView02);        views.remove(picView03);        views.remove(picView04);    }    if (myPop.isShowing()) {        myPop.dismiss();    }}

3、实现思路及注意事项

首先,加载图片需要进行相关处理,比如说用过Picasso 或者是 Glide 等框架,当然了也可将进行自己压缩;

其次,由于为了突出美观,笔者用了一个 CardView 可以设置圆角,但是 CardView  的阴影属性失效了,为了凸显层次感可以设置 PopupWindow 的这个方法public void setElevation(float elevation) 该方法可以是你感觉出一种悬浮的效果;

最后,没用的 view 需要进行清理,否则会留在内存哦。

向下弹出水滴效果

1、布局源码

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="wrap_content">

    <ImageView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBottom="@+id/iv_beauty"        android:layout_toEndOf="@+id/iv_beauty"        android:src="@mipmap/ic_right" />

    <ImageView        android:id="@+id/iv_beauty"        android:layout_width="150dp"        android:layout_height="200dp"        android:background="#669"        android:src="@mipmap/pic_5" /></RelativeLayout>

2、Java 逻辑

/** * 向下弹出 */@SuppressLint("InflateParams")private void showDown() {    view = LayoutInflater.from(this).inflate(R.layout.item_anywhere, null, false);

    myPop = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);    myPop.setBackgroundDrawable(new ColorDrawable());    myPop.setOutsideTouchable(true);    myPop.getContentView().measure(0, 0);    myPop.showAsDropDown(btnPopDown, -((myPop.getContentView().getMeasuredWidth() - btnPopDown.getWidth()) / 2), 0);}

3、注意事项

这个没什么可说的了,和 上面 小标题二 相同 ,具体查看上方即可。

实现屏幕右侧向左弹出

1、布局代码

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="wrap_content">

    <ImageView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBottom="@+id/iv_beauty"        android:layout_toEndOf="@+id/iv_beauty"        android:src="@mipmap/ic_right" />

    <ImageView        android:id="@+id/iv_beauty"        android:layout_width="150dp"        android:layout_height="200dp"        android:background="#669"        android:src="@mipmap/pic_5" /></RelativeLayout>

2、Java 逻辑代码

/** * 向左弹出 */@SuppressLint("InflateParams")private void showStart() {    view = LayoutInflater.from(this).inflate(R.layout.item_pop_start, null, false);

    myPop = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);    myPop.setBackgroundDrawable(new ColorDrawable());    myPop.setOutsideTouchable(true);    myPop.getContentView().measure(0, 0);    myPop.showAsDropDown(fabStart, -(myPop.getContentView().getMeasuredWidth()), -(fabStart.getHeight() / 2 + myPop.getContentView().getMeasuredHeight()));}

3、注意事项

这里比较复杂的 就是 PopupWindow 的锚点位置 为 其寄生的 控件的 左下角,而 Popwindow 的起始点为 左上角,但是 PopupWindow 默认不超出界面。这就导致了 PopupWindow 明明在 控件则左侧,但是却无法到达自己的想要位置。

所以 对于该现象,我们只能 在计算偏移量的时候 需要向左 移动 (控件长度+PopupWindow的长度 +其他长度)

实现需要获取焦点的控件使用

1、布局代码

<?xml version="1.0" encoding="utf-8"?><android.support.v7.widget.CardView 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="wrap_content"    android:layout_height="wrap_content"    android:background="#00000000"    app:cardCornerRadius="10dp">

    <RelativeLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="#00000000"        android:padding="10dp">

        <TextView            android:id="@+id/tv_name_p"            android:layout_width="wrap_content"            android:layout_height="40dp"            android:gravity="center_vertical"            android:text="账户:"            android:textSize="16sp" />

        <EditText            android:layout_width="200dp"            android:layout_height="40dp"            android:layout_toEndOf="@+id/tv_name_p"            android:background="@null"            android:gravity="center_vertical"            android:inputType="number"            android:paddingStart="10dp"            android:paddingEnd="10dp"            android:singleLine="true"            android:textSize="16sp"            tools:text="123" />

        <TextView            android:id="@+id/tv_password_p"            android:layout_width="wrap_content"            android:layout_height="40dp"            android:layout_below="@+id/tv_name_p"            android:gravity="center_vertical"            android:text="密码:"            android:textSize="16sp" />

        <EditText            android:layout_width="200dp"            android:layout_height="40dp"            android:layout_below="@+id/tv_name_p"            android:layout_toEndOf="@+id/tv_password_p"            android:background="@null"            android:gravity="center_vertical"            android:inputType="numberPassword"            android:paddingStart="10dp"            android:paddingEnd="10dp"            android:singleLine="true"            android:textSize="16sp"            tools:text="123" />    </RelativeLayout></android.support.v7.widget.CardView>

2、逻辑代码

@SuppressLint("InflateParams")    private void showEnd() {        view = LayoutInflater.from(this).inflate(R.layout.item_end_input, null, false);

        myPop = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);        myPop.setBackgroundDrawable(new ColorDrawable(0x00ffffff));        myPop.setElevation(10);        myPop.setOutsideTouchable(true);        myPop.setFocusable(true);        myPop.getContentView().measure(0, 0);        myPop.showAsDropDown(fadEnd, (int) (fadEnd.getWidth() * 1.3), -((fadEnd.getHeight() + myPop.getContentView().getMeasuredHeight()) / 2));    }

3、注意事项

这里一定要 设置该方法 public void setFocusable(boolean focusable)否则 在切换EditText 的时候只是光标进行了移动,但是 无法召唤软键盘。

1、笔者认为,上面的大概可以满足比较简单的开发需求了,笔者很菜,这些已经足可以满足笔者了目前;

2、关于偏移量这个会涉及导到一些小小的计算和一点点逻辑想法,所以不要只是做 cv 战士,作为文雅的程序员,我们还是需要有点自己的想法的哈;

3、代码上传 github 地址为:PopupWindow

https://github.com/xiangshiweiyu/PopupWindow

4、希望可以帮到你,批评和建议尽管提出来。

喜欢 就关注吧,欢迎投稿!

PopWindow 制作常见的6种花哨效果相关推荐

  1. java 实现loading效果_常见的几种loding效果实现

    这次我们来说一下常见的几种loding效果实现,loding效果实现需要注意哪几点,下面就是实战案例,一起来看一下. Loading .loader { float: left; } .loader ...

  2. python制作冰花_一种冰花效果的UV涂料及其制备方法

    一 种冰花效果的 UV 涂料及其制备方法 本发明涉及一种冰花效果的 UV 涂料及其制作方法,它 以聚酯丙烯酸酯.高性能聚氨酯丙烯酸酯为主要成份,以引 发剂为固化剂.消泡剂.流平剂为辅料,以功能性丙烯酸 ...

  3. PopupWindow 使用详解(二) Popwindow 制作常见花哨效果

    帝都几日降温,终于被撂倒了.but 只要一息尚存就得不断进步!于是,写出 <PopupWindow 使用详解>的第二篇 笔记,先奉上 第一篇链接: <PopupWindow 使用详解 ...

  4. html5 汽车广告,车身广告常见的五种制作方法

    原标题:车身广告常见的五种制作方法 车身广告能够在户外移动的展示广告信息,还能够给户外的环境添加一些惬意,让人不仅能看到车水马龙的交通,也能够欣赏到移动的广告画面.下面是对车身广告制作方法的详细介绍: ...

  5. 东方时尚网上约车的用户名密码是什么_网站制作要学什么、在现代网页设计中,动效常见的几种用法...

    常听到有人这样问:"网站制作要学什么"和"在现代网页设计中,动效常见的几种用法"有什么关系和内在关联?导航设计是网页可用性的基石.记住,如果用户在您的网站里找不 ...

  6. rp导入图片大小_Axure制作图像的放大与缩小效果的四种方式

    1.新建Axure RP项目,取名"Axure制作图像的放大与缩小效果" 2.拖入动态面板控件,取名"放大缩小".设置大小为560*330px.坐标为x:300 ...

  7. 基于VB算法+Picture+Timer控件制作的39种动画效果,类似屏保(完整原程序)

    基于VB算法+Picture+Timer控件制作的39种动画效果,类似屏保(完整原程序) 动画播放器程序,在WIN2003调试通过,详细请自行下载进行学习测试,程序大小13K 下载地址:http:// ...

  8. 基于VB算法+Picture+Timer控件制作的39种动画效果,类似屏保(完整原程序) (转)

    基于VB算法+Picture+Timer控件制作的39种动画效果,类似屏保(完整原程序) (转)[@more@] 基于VB算法+Picture+Timer控件制作的39种动画效果,类似屏保(完整原程序 ...

  9. 大数据可视化常见的三种错误

    可视化是获取并分享观点的绝佳途径,但很多大数据团队却没能选对正确的方式.可视化怎么会出现问题?原因很简单,因为存在多种可能破坏数据可视化效果的实施方式.下面我们就一同来探讨最为常见的三种错误实践. 错 ...

最新文章

  1. tampermonkey参数
  2. i18n - why Chinese resource will be loaded by default
  3. 【摄影测量原理】第二章:单幅影像解析基础
  4. oracle连接满报错日志,Oracle归档日志满了导致Oracle连接(ORA-00257)报错处理
  5. celery 可视化_Django中Celery的实现介绍(一)
  6. 提高级:初等数论 威尔逊定理
  7. android 微信设置圆角边框代码,Android编程实现圆角边框的方法
  8. 1200万!硅谷AI大牛一年赚够北京二环一套房
  9. Linux App Summit(LAS)社区 KDE Gnome
  10. pspice仿真笔记——spice模型转化
  11. matlab实现带通滤波器的方法,数字信号处理课程设计---带通滤波器的设计及其MATLAB实现.doc...
  12. OD点击寄存器变色OD
  13. SQLPLUS登陆命令
  14. 全新多商户版PHP自助发卡平台源码 多模板 自适应手机端
  15. IE 11中 onpropertychange失效
  16. 使用小丸工具箱进行极限视频压缩
  17. 于博士信号完整性揭秘知识点总结
  18. yolov5 win10 数据集制作 各种踩坑
  19. 加密解密验签概念理解
  20. 中国石化广西石油在加油站增设“爱心驿站”

热门文章

  1. Melodyne Studio 4 修音工具鬼畜调音必备
  2. php--根据手机号码获取归属地
  3. 如何在阿里云上配置安全规则用于开放3CX所需的端口?
  4. 【包运行】Java swing实现录音、播放、180多种乐器模拟、电子钢琴等功能
  5. 江南Style/江南风格/GANGNAM STYLE 歌曲罗马音 中文注音
  6. 网络电台-SHOUTcast
  7. 抢答器java_java竞争抢答器
  8. Linux防火墙设置详解
  9. jQuery 回调函数和方法链接使用
  10. 048_words_discoverer