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;
}
}