码迷,mamicode.com
首页 > 移动开发 > 详细

Android 自定义View实现单击和双击事件

时间:2015-11-13 01:01:48      阅读:295      评论:0      收藏:0      [点我收藏+]

标签:

自定义View,

1. 自定义一个Runnable线程TouchEventCountThread ,  用来统计500ms内的点击次数

2. 在MyView中的 onTouchEvent 中调用 上面的线程

3. 自定义一个Handler, 在TouchEventHandler 中 处理 统计到的点击事件, 单击, 双击, 三击, 都可以处理

 

核心代码如下: 

public class MyView extends View {

   ......

    // 统计500ms内的点击次数
    TouchEventCountThread mInTouchEventCount = new TouchEventCountThread();
    // 根据TouchEventCountThread统计到的点击次数, perform单击还是双击事件
    TouchEventHandler mTouchEventHandler = new TouchEventHandler();

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (0 == mInTouchEventCount.touchCount) // 第一次按下时,开始统计
                    postDelayed(mInTouchEventCount, 500);
                break;
            case MotionEvent.ACTION_UP:
                // 一次点击事件要有按下和抬起, 有抬起必有按下, 所以只需要在ACTION_UP中处理
                mInTouchEventCount.touchCount++;
break; case MotionEvent.ACTION_MOVE: break; case MotionEvent.ACTION_CANCEL: break; default: break; } return super.onTouchEvent(event); } public class TouchEventHandler extends Handler { @Override public void handleMessage(Message msg) { Toast.makeText(mContext, "touch " + msg.arg1 + " time.", Toast.LENGTH_SHORT).show(); } } public class TouchEventCountThread implements Runnable { public int touchCount = 0; @Override public void run() { Message msg = new Message(); msg.arg1 = touchCount; mTouchEventHandler.sendMessage(msg); touchCount = 0; } } ...... }

 

全部代码

技术分享
package com.carloz.test.myapplication.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import com.carloz.test.myapplication.R;

/**
 * Created by root on 15-11-9.
 */
public class MyView extends View {

    private Paint mPaint = new Paint();
    private boolean mNotDestroy = true;
    private int mCount = 0;
    private MyThread myThread;
    Bitmap bitmap;
    // attrs
    private String mText;
    private boolean mStartChange;
    Context mContext;


    public MyView(Context context) {
        super(context);
        init();
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyView);
        mText = ta.getString(R.styleable.MyView_text);
        mStartChange = ta.getBoolean(R.styleable.MyView_startChange, false);
        Log.d("ASDF", "mText=" + mText + ", mStartChange=" + mStartChange);
        ta.recycle();

        init();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setTextSize(50);
        canvas.drawText(mText + mCount++, 20f, 100f, mPaint);
        canvas.save();
        canvas.rotate(60, getWidth() / 2, getHeight() / 2);
        canvas.drawBitmap(bitmap, 20f, 50f, mPaint);
        canvas.restore();

        if (null == myThread) {
            myThread = new MyThread();
            myThread.start();
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return super.dispatchTouchEvent(ev);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        mNotDestroy = true;
    }

    @Override
    protected void onDetachedFromWindow() {
        mNotDestroy = false;
        super.onDetachedFromWindow();
    }

    // 统计500ms内的点击次数
    TouchEventCountThread mInTouchEventCount = new TouchEventCountThread();
    // 根据TouchEventCountThread统计到的点击次数, perform单击还是双击事件
    TouchEventHandler mTouchEventHandler = new TouchEventHandler();

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (0 == mInTouchEventCount.touchCount) // 第一次按下时,开始统计
                    postDelayed(mInTouchEventCount, 500);
                break;
            case MotionEvent.ACTION_UP:
                // 一次点击事件要有按下和抬起, 有抬起必有按下, 所以只需要在ACTION_UP中处理
                mInTouchEventCount.touchCount++; //
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_CANCEL:
                break;
            default:
                break;
        }

        return super.onTouchEvent(event);
    }

    public class TouchEventHandler extends Handler {

        @Override
        public void handleMessage(Message msg) {
            Toast.makeText(mContext, "touch " + msg.arg1 + " time.", Toast.LENGTH_SHORT).show();
        }
    }

    public class TouchEventCountThread implements Runnable {
        public int touchCount = 0;

        @Override
        public void run() {
            Message msg = new Message();
            msg.arg1 = touchCount;
            mTouchEventHandler.sendMessage(msg);
            touchCount = 0;
        }
    }

    class MyThread extends Thread {

        @Override
        public void run() {
            super.run();
            while (mNotDestroy) {
                if (mStartChange) {
                    postInvalidate();
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public void init() {
        mContext = getContext();
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
    }

    public void setText(String mText) {
        this.mText = mText;
    }

    public void setStartChange(boolean mStartChange) {
        this.mStartChange = mStartChange;
    }

    public boolean getStartChange() {
        return this.mStartChange;
    }
}
View Code

 

Android 自定义View实现单击和双击事件

标签:

原文地址:http://www.cnblogs.com/carlo/p/4960664.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!