Andorid手势练习

package com.llm;



import util.CountDownButton;

import util.CountDownButton.OnCountDownListener;

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import android.view.GestureDetector;

import android.view.GestureDetector.OnGestureListener;

import android.view.MotionEvent;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.View.OnTouchListener;

import android.widget.Button;

import android.widget.LinearLayout;

import android.widget.Toast;



public class AddViewDemoActivity

    extends Activity

    implements OnClickListener, OnTouchListener, OnGestureListener {

    /** Called when the activity is first created. */

    private static final int FLING_MIN_DISTANCE = 100;

    private static final int FLING_MIN_VELOCITY = 200;

    private GestureDetector mGestureDetector;

    private Toast toast;

    // 一维数组

    private String[] strs = new String[] {"哈哈1", "Sres2", "fdfd3", "dsdsd4", "fdfd5", "dsdsd6"};

    // 二维数组

    private int[][] numseven = new int[][] { {10, 20, 30}, {40, 50}, {60}, {70, 80, 90}, {100},

            {101, 102, 103, 104, 105, 106}};;

    private int[][] nums = new int[4][5];// 4、5代表的是数组的长度4指0~3,5指0~4



    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

// setContentView(R.layout.main);

        mGestureDetector = new GestureDetector(this);

        toast = ToastUtils.getToastInstance(AddViewDemoActivity.this);

        LinearLayout layout = new LinearLayout(this);

        layout.setOrientation(LinearLayout.VERTICAL);

        Button button1 = new Button(this);

        Button button2 = new Button(this);

        CountDownButton button3 = new CountDownButton(this);

        button1.setText("按钮1");

        button1.setOnClickListener(new OnClickListener() {

            @Override

            public void onClick(View v) {

                // TODO Auto-generated method stub

                toast.setText(String.valueOf(numseven[5][4]));

                toast.show();

            }

        });

        button2.setText("按钮2");

        button3.setCount(15);

        layout.addView(button1);

        layout.addView(button2);

        layout.addView(button3);

        button3.setOnCountDownListener(new OnCountDownListener() {



            @Override

            public void onTick() {

                // TODO Auto-generated method stub

                Log.i("lilongmin", "onTick");

            }



            @Override

            public void onFinish() {

                // TODO Auto-generated method stub

                Log.i("lilongmin", "onFinish");

                AddViewDemoActivity.this.finish();

            }

        });

        setContentView(layout);

        button2.setOnTouchListener(this);

        button2.setLongClickable(true);

    }



    @Override

    public void onClick(View v) {

        // TODO Auto-generated method stub

    }



    @Override

    public boolean onTouch(View v, MotionEvent event) {

        // TODO Auto-generated method stub

// toast.setText("onTouch");

// toast.show();

        return mGestureDetector.onTouchEvent(event);

    }



    // 用户轻触触摸屏,由1个MotionEvent ACTION_DOWN触发

    @Override

    public boolean onDown(MotionEvent e) {

        // TODO Auto-generated method stub

        toast.setText("onDown");

        toast.show();

        return false;

    }



    // 用户按下触摸屏、快速移动后松开,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE, 1个ACTION_UP触发

    @Override

    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

        // 参数解释:

        // e1:第1个ACTION_DOWN MotionEvent

        // e2:最后一个ACTION_MOVE MotionEvent

        // velocityX:X轴上的移动速度,像素/秒

        // velocityY:Y轴上的移动速度,像素/秒

        // 触发条件 : X轴的坐标位移大于FLING_MIN_DISTANCE,且移动速度大于FLING_MIN_VELOCITY个像素/秒

// if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) {

// // Fling left

// toast.setText("Fling Left");

// toast.show();

// }

// else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) {

// // Fling right

// toast.setText("Fling Right");

// toast.show();

// }

        if (e1.getY() - e2.getY() > 100) {

            toast.setText("手势向上滑动");

            toast.show();

            return true;

        }

        else if (e1.getY() - e2.getY() < -100) {

            toast.setText("手势向下滑动");

            toast.show();

            return true;

        }

        else if (e1.getX() - e2.getX() > 100) {

            toast.setText("手势向左滑动");

            toast.show();

            return true;

        }

        else if (e1.getX() - e2.getX() < -100) {

            toast.setText("手势向右滑动");

            toast.show();

            return true;

        }

        return false;

    }



    // 用户长按触摸屏,由多个MotionEvent ACTION_DOWN触发

    @Override

    public void onLongPress(MotionEvent e) {

        // TODO Auto-generated method stub

        toast.setText("onLongPress");

        toast.show();

    }



    // 用户按下触摸屏,并拖动,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE触

    @Override

    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {

        // TODO Auto-generated method stub

        toast.setText("onScroll");

        toast.show();

        return false;

    }



    // 用户轻触触摸屏,尚未松开或拖动,由一个1个MotionEvent ACTION_DOWN触发

    // 注意和onDown()的区别,强调的是没有松开或者拖动的状态

    @Override

    public void onShowPress(MotionEvent e) {

        // TODO Auto-generated method stub

        toast.setText("onShowPress");

        toast.show();

    }



    // 用户(轻触触摸屏后)松开,由一个1个MotionEvent ACTION_UP触发

    @Override

    public boolean onSingleTapUp(MotionEvent e) {

        // TODO Auto-generated method stub

        toast.setText("onSingleTapUp");

        toast.show();

        return false;

    }

}
 
   
下载地址:http://files.cnblogs.com/llm-android/AddViewDemo.rar
 
   

 

 

你可能感兴趣的:(andorid)