Giter Club home page Giter Club logo

directioncontrolview-master's Introduction

####效果如图:


我的博客:详解

这是一个可识别上滑,下滑,左滑,右滑,方向识别与控制的自定义View。核心类就一个:DirectionControlView.java

使用方式如下:

1,把DirectionControlView.java拷贝到你的工程里,布局文件中直接使用:

    <com.zhan.directioncontrolview.widget.DirectionControlView
        android:id="@+id/main_dcv"
        android:background="#41E194"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

2,找到DirectionControlView对象,设置状态监听。

	
		mDirectionControlView = (DirectionControlView) findViewById(R.id.main_dcv);
		mDirectionControlView.setControlStateListener(this);
	
	    @Override
	    public void singleClick() {
	        //单击
	    }
	
	    @Override
	    public void longClick() {
	        //长按
	    }
	
	    @Override
	    public void doubleClick() {
	       //双击
	    }
	
	    @Override
	    public void leftSlide() {
	        //左滑
	    }
	
	    @Override
	    public void rightSlide() {
	        //右滑
	    }
	
	    @Override
	    public void upSlide() {
	        //上滑
	    }
	
	    @Override
	    public void downSlide() {
	       //下滑
	    }

现在可以正常使用DirectionControlView了。


注意,上下左右滑动,在这里只做了最简单的判断和处理,方向识别核心代码如下:

	    @Override
	    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
	        Log.i(TAG, "onFling");
	
	        float offsetX = e1.getX() - e2.getX();//X方向偏移量
	        float offsetY = e1.getY() - e2.getY();//Y方向偏移量
	
	        if (Math.abs(offsetX) > Math.abs(offsetY)) {//左滑或者右滑
	            if (e1.getX() - e2.getX() > MIN_OFFSET_VALUE) {
	                if (mDirectionControlListener != null) {//左滑
	                    mDirectionControlListener.leftSlide();
	                }
	            } else {
	                if (mDirectionControlListener != null) {//右滑
	                    mDirectionControlListener.rightSlide();
	                }
	            }
	        } else {//上滑或者下滑
	            if (e1.getY() - e2.getY() > MIN_OFFSET_VALUE) {
	                if (mDirectionControlListener != null) {//上滑
	                    mDirectionControlListener.upSlide();
	                }
	            } else {
	                if (mDirectionControlListener != null) {//下滑
	                    mDirectionControlListener.downSlide();
	                }
	            }
	        }
	        return true;
	    }

如果你想让这个View做更多的事,或者优化的更好,可以自行修改DirectionControlView.java

directioncontrolview-master's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.