当前位置: 代码迷 >> Android >> Android surfaceview使用——满载onTouchEvent
  详细解决方案

Android surfaceview使用——满载onTouchEvent

热度:90   发布时间:2016-04-28 05:57:06.0
Android surfaceview使用——重载onTouchEvent

surface上绘制bitmap,同时重载onTouchEnvet
public class bmpdrow extends SurfaceView implements SurfaceHolder.Callback {   private Bitmap bmp;   private Resources res;   private SurfaceHolder holder;   private int bmp_w;   private int bmp_h;   private int height;   private int width;   private Canvas canvas;   private Rect rect;	public bmpdrow(Context context) {		super(context);		res=getResources();		bmp=BitmapFactory.decodeResource(res, R.drawable.pic5_3);		//x=bmp.get		holder=getHolder();		holder.addCallback(this);		bmp_w=bmp.getWidth();		bmp_h=bmp.getHeight();		// TODO Auto-generated constructor stub	}   	@Override	public void surfaceCreated(SurfaceHolder holder) {		// TODO Auto-generated method stub		height=this.getHeight();		width=this.getWidth();		/*		 * view什么时候绘制完毕,什么时候才知道大小		 */		mydrow(width/2,height/2);		Log.e("height", ""+height);		Log.e("width",""+width);	}	@Override	public void surfaceChanged(SurfaceHolder holder, int format, int width,			int height) {		// TODO Auto-generated method stub			}	@Override	public void surfaceDestroyed(SurfaceHolder holder) {		// TODO Auto-generated method stub			}	public void mydrow(int x,int y){		/*		 * 在屏幕的(x,y)位置绘制bitmap;		 * Rect(int left, int top, int right, int bottom)		 */		int temp=y-bmp_h;		if(temp<=0)			temp=0;		rect=new Rect(x,y,x+bmp_w,temp);		try{		canvas=holder.lockCanvas();	   if(canvas!=null){		  // canvas.drawBitmap(bmp, matrix, paint)		   //从left,top开始用paint  draw bmp;		   //lock 需要lock矩形区域吗?		  Log.e("canvas", "drow");		   canvas.drawBitmap(bmp, x, y, null);	   }		}finally{			if(canvas!=null){				holder.unlockCanvasAndPost(canvas);			}		}	}	 public boolean onTouchEvent(MotionEvent event){		 /*		  * 重载获得x,y把星星或者小图片会知道指定的位置		  * bmp-》创建canvas		  */		 Log.e("touch", "doing");		 int  x=(int) event.getX();		 int y=(int) event.getY();		 mydrow(x,y);		return true;			}}

  相关解决方案