资讯

直播间搭建Android自定义view,跟随手指滑动

本文主要是介绍直播间搭建Android自定义view,跟随手指滑动,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
public class FollowConstraintLayout extends ConstraintLayout {
 
    //移动
    private GestureDetector gestureDetector;
    private OnClickListener l;
    public FollowConstraintLayout(Context context) {
        super(context);
    }
 
    public FollowConstraintLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
 
    public FollowConstraintLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
 
    {
        performClick();
        gestureDetector = new GestureDetector(getContext(),new GestureDetector.SimpleOnGestureListener(){
 
            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                //滑动事件
                setX(e2.getRawX()-(getWidth()+e1.getX())/2);
                setY(e2.getRawY()-(getHeight()+e1.getY())/2);
                return true;
            }
 
            @Override
            public boolean onSingleTapConfirmed(MotionEvent e) {
                //点击事件
                if(l != null)
                    l.onClick(FollowConstraintLayout.this);
                return true;
            }
        });
    }
 
    @Override
    public void setOnClickListener(@Nullable OnClickListener l) {
       this.l = l;
    }
 
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        gestureDetector.onTouchEvent(event);;
        return true;
    }
}
这篇关于直播间搭建Android自定义view,跟随手指滑动的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
您可能喜欢