今天试了一天,在BookDetail实现public boolean onMotionEvent(MotionEvent event)
一直都没有效果!
晚上才发现,原来要实现触摸方式。必须在自己的view里面实现
贴上一个例子做为参考:
- /*
- *Copyright(C)2007GoogleInc.
- *
- *LicensedundertheApacheLicense,Version2.0(the"License");
- *youmaynotusethisfileexceptincompliancewiththeLicense.
- *YoumayobtainacopyoftheLicenseat
- *
- *http://www.apache.org/licenses/LICENSE-2.0
- *
- *Unlessrequiredbyapplicablelaworagreedtoinwriting,software
- *distributedundertheLicenseisdistributedonan"ASIS"BASIS,
- *WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.
- *SeetheLicenseforthespecificlanguagegoverningpermissionsand
- *limitationsundertheLicense.
- */
- packagecom.google.android.samples.graphics;
- importandroid.app.Activity;
- importandroid.content.Context;
- importandroid.graphics.Bitmap;
- importandroid.graphics.Canvas;
- importandroid.graphics.Paint;
- importandroid.graphics.Rect;
- importandroid.os.Bundle;
- importandroid.view.MotionEvent;
- importandroid.view.View;
- //Needthefollowingimporttogetaccesstotheappresources,sincethis
- //classisinasub-package.
- /**
- *Demonstratesthehandlingoftouchscreeneventstoimplementasimple
- *paintingapp.
- */
- publicclassTouchPaintextendsActivity{
- @Override
- protectedvoidonCreate(Bundleicicle){
- super.onCreate(icicle);
- setContentView(newMyView(this));
- }
- publicclassMyViewextendsView{
- BitmapmBitmap;
- CanvasmCanvas;
- privatefinalRectmRect=newRect();
- privatefinalPaintmPaint;
- privatebooleanmCurDown;
- privateintmCurX;
- privateintmCurY;
- privatefloatmCurPressure;
- privatefloatmCurSize;
- privateintmCurWidth;
- publicMyView(Contextc){
- super(c);
- mPaint=newPaint();
- mPaint.setAntiAlias(true);
- mPaint.setARGB(255,255,255,255);
- }
- @Override
- protectedvoidonSizeChanged(intw,inth,intoldw,intoldh){
- intcurW=mBitmap!=null?mBitmap.width():0;
- intcurH=mBitmap!=null?mBitmap.height():0;
- if(curW>=w&&curH>=h){
- return;
- }
- if(curW<w)curW=w;
- if(curH<h)curH=h;
- BitmapnewBitmap=Bitmap.createBitmap(curW,curH,false);
- CanvasnewCanvas=newCanvas();
- newCanvas.setDevice(newBitmap);
- if(mBitmap!=null){
- newCanvas.drawBitmap(mBitmap,0,0,null);
- }
- mBitmap=newBitmap;
- mCanvas=newCanvas;
- }
- @Override
- protectedvoidonDraw(Canvascanvas){
- if(mBitmap!=null){
- canvas.drawBitmap(mBitmap,0,0,null);
- }
- }
- @Override
- publicbooleanonMotionEvent(MotionEventevent){
- intaction=event.getAction();
- mCurDown=action==MotionEvent.ACTION_DOWN
- ||action==MotionEvent.ACTION_MOVE;
- mCurX=(int)event.getX();
- mCurY=(int)event.getY();
- mCurPressure=event.getPressure();
- mCurSize=event.getSize();
- mCurWidth=(int)(mCurSize*(getWidth()/3));
- if(mCurWidth<1)mCurWidth=1;
- if(mCurDown&&mBitmap!=null){
- intpressureLevel=(int)(mCurPressure*255);
- mPaint.setARGB(pressureLevel,255,255,255);
- mCanvas.drawCircle(mCurX,mCurY,mCurWidth,mPaint);
- mRect.set(mCurX-mCurWidth-2,mCurY-mCurWidth-2,
- mCurX+mCurWidth+2,mCurY+mCurWidth+2);
- invalidate(mRect);
- }
- returntrue;
- }
- }
- }