日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

在SurfaceView上拖動(dòng)一架飛機(jī)

系統(tǒng) 1787 0
接上一篇在 SurfaceView上拖動(dòng)一張小圖片
什么叫拖動(dòng)飛機(jī)呢?且看

在SurfaceView上拖動(dòng)一架飛機(jī)
怎么樣?厲害吧,飛機(jī)都能拖動(dòng)。:P

    
public class AppView extends SurfaceView implements SurfaceHolder.Callback,Runnable,OnTouchListener{

	private static final String tag="AppView";
	private Context context;
	private SurfaceHolder holder;
	private Bitmap player;
	private int playerWidth,playerHeight;
	private int indexX,indexY;//圖片的索引幀
	private int x,y;
	private Paint paint;
	private boolean running=true;
	private Rect view;//游戲視窗
	public AppView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		this.context=context;
		holder = this.getHolder();//獲取holder  
        holder.addCallback(this);
        this.setOnTouchListener(this);
       
	}

	public AppView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		this.context=context;
		holder = this.getHolder();//獲取holder  
        holder.addCallback(this);
        this.setOnTouchListener(this);
	}

	private void init(){
        
        view=new Rect(this.getLeft(),this.getTop(),this.getRight(),this.getBottom());
		player =getImage(context,R.drawable.player);
		playerWidth=player.getWidth()/3;//有3幀
		playerHeight=player.getHeight();
		indexX=1;//初始化幀索引
		indexY=0;
		x=(view.width()-playerWidth)>>1;//初始化角色位置
		y=(view.height()-playerHeight)>>1;
		rect=new Rect(x,y,x+playerWidth,y+playerHeight);//圖片的可拖動(dòng)rect
		paint=new Paint();
	}
	private void close(){
		
	}
	private void logic(){
		
	}
	private void draw(){
		 Canvas canvas = holder.lockCanvas();//獲取畫布 
		 canvas.drawColor(Color.BLACK);
		 canvas.save();
		 canvas.clipRect(rect);
		 canvas.drawBitmap(player, rect.left-playerWidth*indexX,rect.top-playerHeight*indexY,null);
//		 canvas.clipRect(screen);
		 canvas.restore();
		 holder.unlockCanvasAndPost(canvas);// 解鎖畫布,提交畫好的圖像  
	}
	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		Log.i(tag, "=================surfaceCreated======================");
		init();
		
		running=true;
		new Thread(this).start();
		
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		Log.i(tag, "=================surfaceDestroyed======================");
		running=false;
		close();
	}
	
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub
		Log.i(tag, "=================surfaceChanged======================");
		
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		int SLEEP_TIME=100;
		while (running) {
			long start=System.currentTimeMillis();
			logic();
			draw();
            long end=System.currentTimeMillis();  
            if(end-start<SLEEP_TIME){  
                try {  
                    Thread.sleep(SLEEP_TIME-(end-start));  
                } catch (InterruptedException e) {  
                    // TODO Auto-generated catch block  
                    e.printStackTrace();  
                }  
            }  
		}
	}

//	Region region=new Region();
	private Point point=new Point();//點(diǎn)擊點(diǎn)
	private Rect rect;//圖片的可拖動(dòng)rect
	private boolean canDrag=false;//判斷是否點(diǎn)擊在圖片上,否則拖動(dòng)無效
	private int offsetX=0,offsetY=0;//點(diǎn)擊點(diǎn)離圖片左上角的距離
	private VelocityTracker tracker = null;//速度跟蹤器,用于判斷飛機(jī)偏左還是偏右移動(dòng)
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			point.x=(int)event.getX();
			point.y=(int)event.getY();
			if(rect.contains(point.x, point.y)){
				canDrag=true;
				offsetX=point.x-rect.left;
				offsetY=point.y-rect.top;
				if(tracker == null){
					tracker = VelocityTracker.obtain();  
	            }else{
	            	tracker.clear(); 
	            }
				tracker.addMovement(event);
			}
			break;
		case MotionEvent.ACTION_MOVE:
			if(canDrag){
				rect.left=(int)event.getX()-offsetX;
				rect.top=(int)event.getY()-offsetY;
				rect.right=rect.left+playerWidth;
				rect.bottom=rect.top+playerHeight;
				tracker.addMovement(event);  
				tracker.computeCurrentVelocity(1000);
				float  XVelocity=tracker.getXVelocity();
//				Log.i(tag, "XVelocity="+XVelocity);
				if(XVelocity<-100){//偏左
					indexX=0;
					indexY=0;
				}else if(XVelocity>100){//偏右
					indexX=2;
					indexY=0;
				}else{
					indexX=1;
					indexY=0;
				}
				if (rect.left < 0) {  
					rect.left = 0;
					rect.right =  rect.left+playerWidth;
					indexX=1;
					indexY=0;
	            }  
	            if (rect.right >  getMeasuredWidth()) {  
	            	rect.right =  getMeasuredWidth();
	            	rect.left = rect.right-playerWidth;
	            	indexX=1;
					indexY=0;
	            }  
	            if (rect.top < 0) {
	            	rect.top = 0;
	            	rect.bottom = rect.top+playerHeight;
	            }  
	            if (rect.bottom > getMeasuredHeight()) {
	            	rect.bottom = getMeasuredHeight();
	            	rect.top = rect.bottom-playerHeight;
	            }
			}
			break;
		case MotionEvent.ACTION_UP:
//		case MotionEvent.ACTION_CANCEL:
			if(canDrag){
				tracker.recycle();
				canDrag=false;
				indexX=1;
				indexY=0;
			}
			break;

		default:
			break;
		}
		return true;
	}

	//****************************************************************************
	public static final Bitmap getImage(Context context, int imageId) {  
		return BitmapFactory.decodeResource(context.getResources(),imageId);  
	}  
	
}

  

布局隨便弄一個(gè):
public void onCreate(Bundle savedInstanceState) {
??????? super.onCreate(savedInstanceState);
??????? AppView view=new AppView(this);
//??????? setContentView(view);
??????? setContentView(R.layout.main);
}
圖片文件:



一個(gè)游戲角色在屏幕行走的demo
http://blog.csdn.net/xiaominghimi/article/details/6090631

在SurfaceView上拖動(dòng)一架飛機(jī)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 增城市| 礼泉县| 开鲁县| 伊通| 遂宁市| 鄂州市| 蕉岭县| 灌阳县| 项城市| 长泰县| 霍山县| 永济市| 高雄市| 重庆市| 麟游县| 会同县| 河间市| 贵南县| 广汉市| 靖宇县| 陇川县| 航空| 缙云县| 瑞安市| 鹤庆县| 万源市| 旬阳县| 琼结县| 宝鸡市| 乐业县| 佛学| 子洲县| 和田市| 靖远县| 新竹市| 宜州市| 鄂托克前旗| 贵定县| 虞城县| 北辰区| 沾益县|