Android開發(fā)之底圖局部加載移動的方法示例
本文實例講述了Android開發(fā)之底圖局部加載移動的方法。分享給大家供大家參考,具體如下:
public class MapMgr { public static MapMgr mapMgr = null; private int map_num = 28; private int b_x = 0; private int b_y = 0; private int width = 0; private int height = 0; private Bitmap bmpView = null; //create by danielinbiti,前提,你圖片確實比屏幕大,如果不比屏幕大,下面注釋行修改一下即可。 public static void init(int width,int height){ if(mapMgr==null){ mapMgr = new MapMgr(width,height); } } public static MapMgr getInstance(){ return mapMgr; } public MapMgr(int width,int height){ this.width = width; this.height = height; Bitmap bmp = PicMgr.getInstance().getBackGroundBitmap(); b_x = (bmp.getWidth()-width)/2;//保證圖片比屏幕大 b_y = (bmp.getHeight()-height)/2; bmpView = Bitmap.createBitmap(bmp, b_x, b_y, width, height); } public void logic(){ } public void mapDown(){ Bitmap bmp = PicMgr.getInstance().getBackGroundBitmap(); if(b_y+height<bmp.getHeight()){ b_y = b_y + bmp.getHeight()/map_num; if(b_y+height>bmp.getHeight()){ b_y = bmp.getHeight() - height; } } bmpView = Bitmap.createBitmap(bmp, b_x, b_y, width, height); } public void mapUp(){ Bitmap bmp = PicMgr.getInstance().getBackGroundBitmap(); if(b_y>0){ b_y = b_y - bmp.getHeight()/map_num; if(b_y<0){ b_y = 0; } } bmpView = Bitmap.createBitmap(bmp, b_x, b_y, width, height); } public void mapLeft(){ Bitmap bmp = PicMgr.getInstance().getBackGroundBitmap(); if(b_x>0){ b_x = b_x - bmp.getWidth()/map_num; if(b_x<0){ b_x = 0; } } bmpView = Bitmap.createBitmap(bmp, b_x, b_y, width, height); } public void mapRight(){ Bitmap bmp = PicMgr.getInstance().getBackGroundBitmap(); if(b_x+width<bmp.getWidth()){ b_x = b_x + bmp.getWidth()/map_num; if(b_x+width>bmp.getWidth()){ b_x = bmp.getHeight() - width; } } bmpView = Bitmap.createBitmap(bmp, b_x, b_y, width, height); } public void draw(Canvas canvas){ Paint paint = new Paint(); if(bmpView!=null){ canvas.drawBitmap(bmpView,0, 0, paint); } } }
調用
public void onKeyDownDeal(int keyCode){ if(keyCode==KeyEvent.KEYCODE_DPAD_UP){ MapMgr.getInstance().mapUp(); }else if(keyCode==KeyEvent.KEYCODE_DPAD_DOWN){ MapMgr.getInstance().mapDown(); }else if(keyCode==KeyEvent.KEYCODE_DPAD_LEFT){ MapMgr.getInstance().mapLeft(); }else if(keyCode==KeyEvent.KEYCODE_DPAD_RIGHT){ MapMgr.getInstance().mapRight(); } }
然后使用線程調用draw刷新即可。
對于觸摸移動只是坐標計算方式不同,其它都類似。另外擴充到GIS等,可以根據(jù)小圖片粘貼實現(xiàn)局部加載內容。
更多關于Android相關內容感興趣的讀者可查看本站專題:《Android圖形與圖像處理技巧總結》、《Android開發(fā)入門與進階教程》、《Android調試技巧與常見問題解決方法匯總》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。
相關文章
Android中RecyclerView實現(xiàn)滑動刪除與拖拽功能
這篇文章主要使用了RecyclerView的ItemTouchHelper類實現(xiàn)了Item的拖動和刪除功能,ItemTouchHelper是v7包下的一個類,下面來看看詳細的介紹吧,需要的朋友可以參考學習。2017-02-02Android從0到完整項目(1)使用Android studio 創(chuàng)建項目詳解
本篇文章主要介紹了Android從0到完整項目(1)使用Android studio 創(chuàng)建項目詳解,具有一定的參考價值,有興趣的可以了解一下2017-07-07Android?WebView緩存機制優(yōu)化加載慢問題
我知道你一定在煩惱Android?Webview的性能問題,特別突出的是-加載速度慢、消耗流量,針對Android?Webview的性能問題,提出一些有效解決方案2023-02-02Android Map數(shù)據(jù)結構全面總結分析
這篇文章主要為大家介紹了Android Map數(shù)據(jù)結構全面總結分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12