V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
hzw1199
V2EX  ›  Android

撸了一个支持系统级、Activity 级、ViewGroup 级拖动控件的库

  •  1
     
  •   hzw1199 · 2019-07-17 00:00:38 +08:00 · 9039 次点击
    这是一个创建于 1961 天前的主题,其中的信息可能已经有所发展或是发生改变。

    https://github.com/hzw1199/FloatingView

    来这儿吆喝一波 手指拖动的库已经很多了,但是这个库的亮点是支持系统级、Activity 级、ViewGroup 级三种模式,满足各种需求

    FloatingView

    Platform API License

    Github

    Features

    • 支持OverlaySystemOverlayActivityOverlayViewGroup 三种模式
    • OverlaySystem模式下可在其他 APP 上显示,自动申请权限
    • OverlayActivity模式下只在指定的 Activity 上显示
    • OverlayViewGroup模式下只在指定的 ViewGroup 上显示
    • 可随着手指拖动
    • 可指定 9 个初始位置
    • 可指定初始 paddings

    Usage

    Step 1

    在 project 的 build.gradle 中加入以下语句:

    allprojects {
        repositories {
            ...
            maven { url "https://jitpack.io" }
        }
    }
    

    在 module 的 build.gradle 中加入以下语句:

    dependencies {
        compile 'com.github.hzw1199:FloatingView:1.0.0'
    }
    

    Step 2

    创建配置并显示

    • OverlaySystem模式

    onCreate:

    FloatingViewConfig config = new FloatingViewConfig.Builder()
            .setPaddingLeft(paddingLeft)
            .setPaddingTop(paddingTop)
            .setPaddingRight(paddingRight)
            .setPaddingBottom(paddingBottom)
            .setGravity(gravity)
            .build();
    floatingView = new FloatingView(OverlaySystemActivity.this, R.layout.view_floating, config);
    floatingView.showOverlaySystem();
    

    onDestroy:

    if (floatingView != null) {
        floatingView.hide();
    }
    
    • OverlayActivity模式

    onAttachedToWindow:

    FloatingViewConfig config = new FloatingViewConfig.Builder()
            .setPaddingLeft(paddingLeft)
            .setPaddingTop(paddingTop)
            .setPaddingRight(paddingRight)
            .setPaddingBottom(paddingBottom)
            .setGravity(gravity)
            .build();
    floatingView = new FloatingView(OverlaySystemActivity.this, R.layout.view_floating, config);
    floatingView.showOverlayActivity();
    

    onDetachedFromWindow:

    if (floatingView != null) {
        floatingView.hide();
    }
    
    • OverlayViewGroup模式

    onCreate:

    lyViewGroup.post(new Runnable() {
        @Override
        public void run() {
    		FloatingViewConfig config = new FloatingViewConfig.Builder()
    		        .setPaddingLeft(paddingLeft)
    		        .setPaddingTop(paddingTop)
    		        .setPaddingRight(paddingRight)
    		        .setPaddingBottom(paddingBottom)
    		        .setGravity(gravity)
    		        .setDisplayWidth(lyViewGroup.getWidth())
    		        .setDisplayHeight(lyViewGroup.getHeight())
    		        .build();
    		floatingView = new FloatingView(OverlaySystemActivity.this, R.layout.view_floating, config);
    		floatingView.showOverlayViewGroup(lyViewGroup);
        }
    });
    

    onDestroy:

    if (floatingView != null) {
        floatingView.hide();
    }
    

    lyViewGroup是用来放置 FloatingView 的 ViewGroup

    Step 3

    点击事件

    floatingView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            
        }
    });
    

    Proguard

    无需配置混淆规则, consumerProguardFiles 已经配置过了

    Tip

    • 使用前请查看 demo
    • 若对你有帮助请加星
    • Blog

    License

    The MIT License (MIT)
    
    Copyright (c) 2017 AndroidGpsStatus
    
    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:
    
    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.
    
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
    
    5 条回复    2019-07-18 09:18:37 +08:00
    hyyou2010
        1
    hyyou2010  
       2019-07-17 00:22:25 +08:00
    赞,先 star 支持一下
    wulikanhuaz
        2
    wulikanhuaz  
       2019-07-17 15:42:26 +08:00
    大佬厉害呀
    hzw1199
        3
    hzw1199  
    OP
       2019-07-17 16:02:29 +08:00
    @hyyou2010 就是喜欢反手就是一个 star 的同学
    hzw1199
        4
    hzw1199  
    OP
       2019-07-17 16:02:55 +08:00
    @wulikanhuaz 来个 star 吗(手动滑稽
    wjh3936
        5
    wjh3936  
       2019-07-18 09:18:37 +08:00
    骚年,看到我手中的 star 了吗(狗头
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3362 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 19ms · UTC 11:32 · PVG 19:32 · LAX 03:32 · JFK 06:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.