Android实现左右拖动,可以悬浮吸附边界的悬浮框

  • Post author:
  • Post category:其他


实现思路如下:

1.实现拖动效果需要将拖动视图放入RelativeLayout或者FrameLayout视图内;

2.定义拖动视图和视图拖动范围;

3.重写拖动视图的onTouchEvent()方法;

4.实现手势抬起后动画吸附屏幕左侧或右侧;

实现可拖动功能的View:


1.实现拖动效果需要将拖动视图放入RelativeLayout或者FrameLayout视图内;

布局xml添加

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

或者通过代码将拖动View添加到RelativeLayout布局下;


2.定义拖动视图和视图拖动范围;

拖动视图不能超过屏幕四周边缘,(0,0,screenwidth,screenheight);可以需要调整拖动视图的边缘;


3.重写拖动视图的onTouchEvent()方法;

//保存按下的X,Y轴坐标
private int lastX, lastY; 
//可设置距离边界的距离,正负值均可,单位为dp
private int hideSize = 50;
@Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            //1.记录当前按下屏幕上的X,Y轴坐标
            case MotionEvent.ACTION_DOWN:
                lastX = (int)event.getRawX();
                lastY = (int)event.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                //2.记录当前屏幕上X,Y轴屏幕手指移动距离
                int moveX = (int)(event.getRawX() - lastX);
                int moveY = (int)(event.getRawY() - lastY);
                //记录当前拖动视图距离上下左右边缘的的距离
                int top = getTop();
                int left = getLeft();
                int right = getRight();
                int bottom = getBottom();
                //3.高度范围,计算是否超过顶部和底部边界
                if(top<0){
                    top = 0;
                    bottom = getHeight();
                }

                if(bottom>screenHeight()){
                    top = screenHeight()-getHeight();
                    bottom = screenHeight();
                }

                //4.宽度范围,计算是否超过顶部和底部边界
                if(left<-hideSize){
                    left = -hideSize;
                    right = getWidth()-hideSize;
                }
                if(right>screenWidth()+hideSize){
                    left = screenWidth() - getWidth()+hideSize;
                    right = screenWidth()+hideSize;
                }
                //5.重新布局移动视图到指定位置
                layout(left+moveX,top+moveY, right+moveX, bottom+moveY);
                //6.记录当前手势位置
                lastX = (int)event.getRawX();
                lastY = (int)event.getRawY();
                break;
            ...
        }
        return true;//super.onTouchEvent(event);
    }

public void animSlide(View view, int from, int to, int duration){
        ValueAnimator valueAnimator = ValueAnimator.ofInt(from, to);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int viewLeft = (int)animation.getAnimatedValue();
                layout(viewLeft,getTop(),viewLeft+view.getWidth(),getBottom());
            }
        });
        //为防止溢出边界时,duration时间为负值,做下0判断
        valueAnimator.setDuration(duration < 0 ? 0 : duration);
        valueAnimator.start();
    }


4.实现手势抬起后动画吸附屏幕左侧或右侧;

onTouchEvent(){
...
case MotionEvent.ACTION_UP:
                //从中间位置移动边缘的最大时间
                int maxDuration = 500;
                int duration = 0;
                int leftLimit = (screenWidth() - getWidth())/2; 
                //7.
                if(getLeft()<leftLimit){
                    //若不用动画,直接布局指定位置
//                    layout(-hideSize,getTop(),getWidth()-hideSize,getBottom());
                    //根据距离边界的距离,弹性计算动画执行时间,防止距离边界很近的时候执行时间仍是过长
                    duration = maxDuration*(getLeft()+hideSize)/(leftLimit+hideSize);
                    animSlide(this,getLeft(),-hideSize,duration);
                }else{
                    duration = maxDuration*(screenWidth()-getRight()+hideSize)/(leftLimit+hideSize);
                    animSlide(this,getLeft(),screenWidth()-getWidth()+hideSize,500);
//                    layout(screenWidth()-getWidth()+hideSize,getTop(),screenWidth()+hideSize,getBottom());
                }
                break;
}


5.完整代码

package com.gome.childrenmanager.views;

import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.gome.childrenmanager.R;

/**
 * 拖扯控件可以左右吸附
 */
public class PageDragView extends LinearLayout {

    private int hideSize = 50;

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

    public PageDragView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public PageDragView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr,0);
    }

    public PageDragView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initViews();
    }

    private void initViews() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.cockroach, this);
    }


    private int lastX, lastY;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                lastX = (int)event.getRawX();
                lastY = (int)event.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                int moveX = (int)(event.getRawX() - lastX);
                int moveY = (int)(event.getRawY() - lastY);

                int top = getTop();
                int left = getLeft();
                int right = getRight();
                int bottom = getBottom();
                //高度范围
                if(top<0){
                    top = 0;
                    bottom = getHeight();
                }

                if(bottom>screenHeight()){
                    top = screenHeight()-getHeight();
                    bottom = screenHeight();
                }

                //宽度范围
                if(left<-hideSize){
                    left = -hideSize;
                    right = getWidth()-hideSize;
                }
                if(right>screenWidth()+hideSize){
                    left = screenWidth() - getWidth()+hideSize;
                    right = screenWidth()+hideSize;
                }
                layout(left+moveX,top+moveY, right+moveX, bottom+moveY);

                lastX = (int)event.getRawX();
                lastY = (int)event.getRawY();
                break;
            case MotionEvent.ACTION_UP:
                int maxDuration = 500;
                int duration = 0;
                int leftLimit = (screenWidth() - getWidth())/2;
                if(getLeft()<leftLimit){
//                    layout(0,getTop(),getWidth(),getBottom());
                    duration = maxDuration*(getLeft()+hideSize)/(leftLimit+hideSize);
                    animSlide(this,getLeft(),-hideSize,duration);
                }else{
                    duration = maxDuration*(screenWidth()-getRight()+hideSize)/(leftLimit+hideSize);
                    animSlide(this,getLeft(),screenWidth()-getWidth()+hideSize,500);
//                    layout(screenWidth()-getWidth(),getTop(),screenWidth(),getBottom());
                }
                break;
        }
        return true;//super.onTouchEvent(event);
    }

    public void animSlide(View view, int from, int to, int duration){
        ValueAnimator valueAnimator = ValueAnimator.ofInt(from, to);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int viewLeft = (int)animation.getAnimatedValue();
                layout(viewLeft,getTop(),viewLeft+view.getWidth(),getBottom());
            }
        });
        //为防止溢出边界时,duration时间为负值,做下0判断
        valueAnimator.setDuration(duration < 0 ? 0 : duration);
        valueAnimator.start();
    }

    public float dpToPx(Context context, float dp){
        return context == null ? -1.0f : dp * context.getResources().getDisplayMetrics().density;
    }

    public float pxToDp(Context context, float px){
        return context == null ? -1.0f : px / getContext().getResources().getDisplayMetrics().density;
    }

    public int screenWidth(){
        return getContext().getResources().getDisplayMetrics().widthPixels;
    }

    public int screenHeight(){
        return getContext().getResources().getDisplayMetrics().heightPixels;
    }
}

参考:


Android 实现可拖动、吸附左右边界的悬浮框_hsw-CSDN博客



版权声明:本文为ahou2468原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。