Quantcast
Channel: CSDN博客移动开发推荐文章
Viewing all articles
Browse latest Browse all 5930

Android开发-自定义View-AndroidStudio(九)手势监听类GestureDetector

$
0
0
转载请注明出处:http://blog.csdn.net/iwanghang/
觉得博文有用,请点赞,请评论,请关注,谢谢!~


由于我自己对手势监听类没什么概念,用过很多次,但是没有完整的了解过,找了一个Demo加以改进,帮助大家一起理解。
老规矩,先上GIF动态图,看个效果,如果符合你的项目或者确定你要了解的内容,再往下看吧:


MainActivity.java:
package com.iwanghang.mygesturedetector;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.View;
import android.widget.Button;
import android.util.Log;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View.OnTouchListener;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView textView,tv_1,tv_2,tv_3,tv_4,tv_5;
    private Integer tv_times_now;
    private Integer tv_times = 0;
    private Button mButton;
    private GestureDetector mGestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv_1 = (TextView) findViewById(R.id.tv_1);
        tv_2 = (TextView) findViewById(R.id.tv_2);
        tv_3 = (TextView) findViewById(R.id.tv_3);
        tv_4 = (TextView) findViewById(R.id.tv_4);
        tv_5 = (TextView) findViewById(R.id.tv_5);

        mGestureDetector = new GestureDetector(this, new MyOnGestureListener());

        mButton = (Button) findViewById(R.id.btn_textgesture);
        mButton.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.i(getClass().getName(), "onTouch-----" + getActionName(event.getAction()));
                tv_times_get();
                textView.setText("onTouch-----" + getActionName(event.getAction()));
                mGestureDetector.onTouchEvent(event);
                // 一定要返回true,不然获取不到完整的事件
                return true;
            }
        });
    }

    public void Clean(View view){
        tv_1.setText("");
        tv_2.setText("");
        tv_3.setText("");
        tv_4.setText("");
        tv_5.setText("");
    }

    private void tv_times_get() {
        tv_times_now = tv_times % 5;
        switch (tv_times_now){
            case 0:
                textView = tv_1;
                break;
            case 1:
                textView = tv_2;
                break;
            case 2:
                textView = tv_3;
                break;
            case 3:
                textView = tv_4;
                break;
            case 4:
                textView = tv_5;
                break;
        }
        tv_times = tv_times + 1;
    }

    private String getActionName(int action) {
        String name = "";
        switch (action) {
            case MotionEvent.ACTION_DOWN: {
                name = "ACTION_DOWN";
                break;
            }
            case MotionEvent.ACTION_MOVE: {
                name = "ACTION_MOVE";
                break;
            }
            case MotionEvent.ACTION_UP: {
                name = "ACTION_UP";
                break;
            }
            default:
                break;
        }
        return name;
    }

    class MyOnGestureListener extends SimpleOnGestureListener {
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            Log.i(getClass().getName(), "onSingleTapUp-----" + getActionName(e.getAction()));
            tv_times_get();
            textView.setText("onSingleTapUp-----" + getActionName(e.getAction()));
            return false;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            Log.i(getClass().getName(), "onLongPress-----" + getActionName(e.getAction()));
            tv_times_get();
            textView.setText("onLongPress-----" + getActionName(e.getAction()));
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            Log.i(getClass().getName(), "onScroll-----" + getActionName(e2.getAction()) + ",(" + e1.
                    getX() + "," + e1.getY() + ") ,(" + e2.getX() + "," + e2.getY() + ")");
            tv_times_get();
            textView.setText( "onScroll-----" + getActionName(e2.getAction()) + ",(" + e1.getX() +
                    "," + e1.getY() + ") ,(" + e2.getX() + "," + e2.getY() + ")");
            return false;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            Log.i(getClass().getName(), "onFling-----" + getActionName(e2.getAction()) + ",(" + e1.
                    getX() + "," + e1.getY() + ") ,(" + e2.getX() + "," + e2.getY() + ")");
            tv_times_get();
            textView.setText( "onFling-----" + getActionName(e2.getAction()) + ",(" + e1.getX() +
                    "," + e1.getY() + ") ,(" + e2.getX() + "," + e2.getY() + ")");
            return false;
        }

        @Override
        public void onShowPress(MotionEvent e) {
            Log.i(getClass().getName(), "onShowPress-----" + getActionName(e.getAction()));
            tv_times_get();
            textView.setText("onShowPress-----" + getActionName(e.getAction()));
        }

        @Override
        public boolean onDown(MotionEvent e) {
            Log.i(getClass().getName(), "onDown-----" + getActionName(e.getAction()));
            tv_times_get();
            textView.setText("onDown-----" + getActionName(e.getAction()));
            return false;
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            Log.i(getClass().getName(), "onDoubleTap-----" + getActionName(e.getAction()));
            tv_times_get();
            textView.setText("onDoubleTap-----" + getActionName(e.getAction()));
            return false;
        }

        @Override
        public boolean onDoubleTapEvent(MotionEvent e) {
            Log.i(getClass().getName(), "onDoubleTapEvent-----" + getActionName(e.getAction()));
            tv_times_get();
            textView.setText("onDoubleTapEvent-----" + getActionName(e.getAction()));
            return false;
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            Log.i(getClass().getName(), "onSingleTapConfirmed-----" + getActionName(e.getAction()));
            tv_times_get();
            textView.setText("onSingleTapConfirmed-----" + getActionName(e.getAction()));
            return false;
        }
    }


}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.iwanghang.mygesturedetector.MainActivity">

    <Button
        android:text="清除"
        android:onClick="Clean"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tv_1"
        android:textColor="#ff0000"
        android:text="@string/app_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <!--我是分割-->
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0.1dp"
        android:background="#000000" />

    <TextView
        android:id="@+id/tv_2"
        android:textColor="#ff0000"
        android:text="@string/app_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <!--我是分割-->
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0.1dp"
        android:background="#000000" />

    <TextView
        android:id="@+id/tv_3"
        android:textColor="#ff0000"
        android:text="@string/app_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <!--我是分割-->
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0.1dp"
        android:background="#000000" />

    <TextView
        android:id="@+id/tv_4"
        android:textColor="#ff0000"
        android:text="@string/app_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <!--我是分割-->
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="0.1dp"
        android:background="#000000" />

    <TextView
        android:id="@+id/tv_5"
        android:textColor="#ff0000"
        android:text="@string/app_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/btn_textgesture"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="@string/app_name" />

</LinearLayout>


转载请注明出处:http://blog.csdn.net/iwanghang/



欢迎移动开发爱好者交流
沈阳或周边城市公司有意开发Android,请与我联系
联系方式

微信:iwanghang
QQ:413711276
邮箱:iwanghang@qq.com



觉得博文有用,请点赞,请评论,请关注,谢谢!~

作者:iwanghang 发表于2016/12/21 14:57:53 原文链接
阅读:171 评论:0 查看评论

Viewing all articles
Browse latest Browse all 5930

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>