android view的单击和双击事件,Android 自定义View实现单击和双击事件

packagecom.carloz.test.myapplication.view;importandroid.content.Context;importandroid.content.res.TypedArray;importandroid.graphics.Bitmap;importandroid.graphics.BitmapFactory;importandroid.graphics.Canvas;importandroid.graphics.Paint;importandroid.os.Handler;importandroid.os.Message;importandroid.util.AttributeSet;importandroid.util.Log;importandroid.view.MotionEvent;importandroid.view.View;importandroid.widget.Toast;importcom.carloz.test.myapplication.R;/*** Created by root on 15-11-9.*/

public class MyView extendsView {private Paint mPaint = newPaint();private boolean mNotDestroy = true;private int mCount = 0;privateMyThread myThread;

Bitmap bitmap;//attrs

privateString mText;private booleanmStartChange;

Context mContext;publicMyView(Context context) {super(context);

init();

}publicMyView(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();

}

@Overrideprotected voidonFinishInflate() {super.onFinishInflate();

}

@Overrideprotected void onMeasure(int widthMeasureSpec, intheightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);

}

@Overrideprotected void onLayout(boolean changed, int left, int top, int right, intbottom) {super.onLayout(changed, left, top, right, bottom);

}

@Overrideprotected voidonDraw(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= newMyThread();

myThread.start();

}

}

@Overridepublic booleandispatchTouchEvent(MotionEvent ev) {return super.dispatchTouchEvent(ev);

}

@Overrideprotected voidonAttachedToWindow() {super.onAttachedToWindow();

mNotDestroy= true;

}

@Overrideprotected voidonDetachedFromWindow() {

mNotDestroy= false;super.onDetachedFromWindow();

}//统计500ms内的点击次数

TouchEventCountThread mInTouchEventCount = newTouchEventCountThread();//根据TouchEventCountThread统计到的点击次数, perform单击还是双击事件

TouchEventHandler mTouchEventHandler = newTouchEventHandler();

@Overridepublic booleanonTouchEvent(MotionEvent event) {switch(event.getAction()) {caseMotionEvent.ACTION_DOWN:if (0 == mInTouchEventCount.touchCount) //第一次按下时,开始统计

postDelayed(mInTouchEventCount, 500);break;caseMotionEvent.ACTION_UP://一次点击事件要有按下和抬起, 有抬起必有按下, 所以只需要在ACTION_UP中处理

mInTouchEventCount.touchCount++; // break;caseMotionEvent.ACTION_MOVE:break;caseMotionEvent.ACTION_CANCEL:break;default:break;

}return super.onTouchEvent(event);

}public class TouchEventHandler extendsHandler {

@Overridepublic voidhandleMessage(Message msg) {

Toast.makeText(mContext,"touch " + msg.arg1 + " time.", Toast.LENGTH_SHORT).show();

}

}public class TouchEventCountThread implementsRunnable {public int touchCount = 0;

@Overridepublic voidrun() {

Message msg= newMessage();

msg.arg1=touchCount;

mTouchEventHandler.sendMessage(msg);

touchCount= 0;

}

}class MyThread extendsThread {

@Overridepublic voidrun() {super.run();while(mNotDestroy) {if(mStartChange) {

postInvalidate();try{

Thread.sleep(500);

}catch(InterruptedException e) {

e.printStackTrace();

}

}

}

}

}public voidinit() {

mContext=getContext();

bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

}public voidsetText(String mText) {this.mText =mText;

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

}public booleangetStartChange() {return this.mStartChange;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值