欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Android简单实现悬浮吸附的FloatingView

程序员文章站 2022-06-16 10:26:02
/** * @ProjectName: yqf-android * @Package: com.jao.pointsystem.weight * @ClassName: ScrollFloatinigButton * @Description: java类作用描述 * @Author: OlderJiao * @CreateDate: 2020/3/24 10:47 * @UpdateUser: 更新者: * @UpdateDate: 2020/3/24 10:47 * @Version....

Android简单实现悬浮吸附的FloatingView

/**
 * @ProjectName: yqf-android
 * @Package: com.jao.pointsystem.weight
 * @ClassName: ScrollFloatinigButton
 * @Description: java类作用描述
 * @Author: OlderJiao
 * @CreateDate: 2020/3/24 10:47
 * @UpdateUser: 更新者:
 * @UpdateDate: 2020/3/24 10:47
 * @Version: 1.0
 */
@SuppressLint("AppCompatCustomView")
public class ScrollFloatinigButton extends FrameLayout {
    private static final String TAG = "ScrollFloatinigButton";
    private float mX;
    private float mY;
    private int mParentWidth;
    private int mParentHeight;
    private boolean mScrollEnable = true;
    private int mScrollLeft;
    private int mScrollTop;
    private int mRight;
    private int mScrollRight;
    private int mScrollBottom;
    private boolean hasScroll;
    boolean isScroll = false;
    private boolean isAdsorb = true;//是否自动吸附到两边

    public ScrollFloatinigButton(Context context) {
        this(context, null);
    }

    public ScrollFloatinigButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ScrollFloatinigButton(Context context, AttributeSet attrs, final int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ScrollFloatinigView);
        mScrollEnable = ta.getBoolean(R.styleable.ScrollFloatinigView_scrollEnable, true);
        ta.recycle();
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        //防止布局重置时重置ScrollFloatinigView的位置
        ViewParent parent = getParent();
        if (parent instanceof ViewGroup) {
            ((ViewGroup) getParent()).addOnLayoutChangeListener(new OnLayoutChangeListener() {
                @Override
                public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                    if (hasScroll && mScrollRight != 0 && mScrollBottom != 0)
                        layout(mScrollLeft, mScrollTop, mScrollRight, mScrollBottom);
                }
            });
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (getParent() instanceof ViewGroup) {
            mParentWidth = ((ViewGroup) getParent()).getWidth();
            mParentHeight = ((ViewGroup) getParent()).getHeight();
        }
        Log.i(TAG, "onSizeChanged: ");
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (!mScrollEnable) return super.onTouchEvent(ev);
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mX = ev.getX();
                mY = ev.getY();
                super.onTouchEvent(ev);
                return true;
            case MotionEvent.ACTION_MOVE:
                int scaledTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
                float x = ev.getX();
                float y = ev.getY();
                x = x - mX;
                y = y - mY;
                if (Math.abs(scaledTouchSlop) < Math.abs(x) || Math.abs(scaledTouchSlop) < Math.abs(y)) {
                    isScroll = true;
                }
                if (isScroll) {
                    mScrollLeft = (int) (getX() + x);
                    mScrollTop = (int) (getY() + y);
                    mScrollRight = (int) (getX() + getWidth() + x);
                    mScrollBottom = (int) (getY() + getHeight() + y);
                    //防止滑出父界面
                    if (mScrollLeft < 0 || mScrollRight > mParentWidth) {
                        mScrollLeft = (int) getX();
                        mScrollRight = (int) getX() + getWidth();
                    }
                    if (mScrollTop < 0 || mScrollBottom > mParentHeight) {
                        mScrollTop = (int) getY();
                        mScrollBottom = (int) getY() + getHeight();
                    }
                    layout(mScrollLeft, mScrollTop, mScrollRight, mScrollBottom);
                    hasScroll = true;
                    return true;
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                if (isScroll) {
                    isScroll = false;
                    setPressed(false);//重置点击状态
                    if (isAdsorb) {//判断是否开启吸附
                        //获取屏幕中间值
                        int mind = getScreenWidth() / 2;
                        //获取控件宽度的中间值
                        int viewWithMind = getWidth() / 2;
                        //手指抬起时 自动吸附到屏幕两边
                        if (mScrollLeft + viewWithMind > mind) {
                            mScrollRight = getScreenWidth();
                            mScrollLeft = getScreenWidth() - getWidth();
                        } else {
                            mScrollRight = getWidth();
                            mScrollLeft = 0;
                        }
                        layout(mScrollLeft, mScrollTop, mScrollRight, mScrollBottom);
                    }
                    return true;
                }
                break;
        }
        return super.onTouchEvent(ev);
    }

    public boolean isAdsorb() {
        return isAdsorb;
    }

    public void setAdsorb(boolean adsorb) {
        isAdsorb = adsorb;
    }

    /**
     * 得到屏幕宽度
     *
     * @return
     */
    private int getScreenWidth() {
        WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(displayMetrics);
        return displayMetrics.widthPixels;
    }
}

布局代码,引用一下控件就OK了

<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=".ZoomActivity">

    <com.example.test.widget.ScrollFloatinigButton
        android:layout_width="100dp"
        android:layout_height="150dp"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="50dp"
        android:background="@color/gray"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

自定义的属性

<declare-styleable name="ScrollFloatinigView">
        <attr name="scrollEnable" format="boolean" />
    </declare-styleable>

有问题欢迎指正,共同学习,共同进步

本文地址:https://blog.csdn.net/qq_44946212/article/details/110648463