Android底图局部加载移动


  1. public class MapMgr {  
  2.     public static MapMgr mapMgr = null;  
  3.     private int map_num = 28;  
  4.     private int b_x = 0;  
  5.     private int b_y = 0;  
  6.     private int width = 0;  
  7.     private int height = 0;  
  8.     private Bitmap bmpView = null;  
  9.     //create by danielinbiti,前提,你图片确实比屏幕大,如果不比屏幕大,下面注释行修改一下即可。   
  10.     public static void init(int width,int height){  
  11.         if(mapMgr==null){  
  12.             mapMgr = new MapMgr(width,height);  
  13.         }  
  14.     }  
  15.     public static MapMgr getInstance(){  
  16.         return mapMgr;  
  17.     }  
  18.     public MapMgr(int width,int height){  
  19.         this.width = width;  
  20.         this.height = height;  
  21.         Bitmap bmp = PicMgr.getInstance().getBackGroundBitmap();  
  22.         b_x = (bmp.getWidth()-width)/2;//保证图片比屏幕大   
  23.         b_y = (bmp.getHeight()-height)/2;  
  24.         bmpView = Bitmap.createBitmap(bmp, b_x, b_y, width, height);  
  25.     }  
  26.     public void logic(){  
  27.           
  28.     }  
  29.     public void mapDown(){  
  30.         Bitmap bmp = PicMgr.getInstance().getBackGroundBitmap();  
  31.         if(b_y+height<bmp.getHeight()){  
  32.             b_y = b_y + bmp.getHeight()/map_num;  
  33.             if(b_y+height>bmp.getHeight()){  
  34.                 b_y = bmp.getHeight() - height;  
  35.             }  
  36.         }  
  37.         bmpView = Bitmap.createBitmap(bmp, b_x, b_y, width, height);  
  38.     }  
  39.     public void mapUp(){  
  40.         Bitmap bmp = PicMgr.getInstance().getBackGroundBitmap();  
  41.         if(b_y>0){  
  42.             b_y = b_y - bmp.getHeight()/map_num;  
  43.             if(b_y<0){  
  44.                 b_y = 0;  
  45.             }  
  46.         }  
  47.         bmpView = Bitmap.createBitmap(bmp, b_x, b_y, width, height);  
  48.     }  
  49.     public void mapLeft(){  
  50.         Bitmap bmp = PicMgr.getInstance().getBackGroundBitmap();  
  51.         if(b_x>0){  
  52.             b_x = b_x - bmp.getWidth()/map_num;  
  53.             if(b_x<0){  
  54.                 b_x = 0;  
  55.             }  
  56.         }  
  57.         bmpView = Bitmap.createBitmap(bmp, b_x, b_y, width, height);  
  58.     }  
  59.     public void mapRight(){  
  60.         Bitmap bmp = PicMgr.getInstance().getBackGroundBitmap();  
  61.         if(b_x+width<bmp.getWidth()){  
  62.             b_x = b_x + bmp.getWidth()/map_num;  
  63.             if(b_x+width>bmp.getWidth()){  
  64.                 b_x = bmp.getHeight() - width;  
  65.             }  
  66.         }  
  67.         bmpView = Bitmap.createBitmap(bmp, b_x, b_y, width, height);  
  68.     }  
  69.     public void draw(Canvas canvas){  
  70.         Paint paint = new Paint();  
  71.         if(bmpView!=null){  
  72.             canvas.drawBitmap(bmpView,00, paint);  
  73.         }  
  74.     }  
  75. }  

调用

  1. public void onKeyDownDeal(int keyCode){  
  2.         if(keyCode==KeyEvent.KEYCODE_DPAD_UP){  
  3.             MapMgr.getInstance().mapUp();  
  4.         }else if(keyCode==KeyEvent.KEYCODE_DPAD_DOWN){  
  5.             MapMgr.getInstance().mapDown();  
  6.         }else if(keyCode==KeyEvent.KEYCODE_DPAD_LEFT){  
  7.             MapMgr.getInstance().mapLeft();  
  8.         }else if(keyCode==KeyEvent.KEYCODE_DPAD_RIGHT){  
  9.             MapMgr.getInstance().mapRight();  
  10.         }  
  11.     }  
然后使用线程调用draw刷新即可。

对于触摸移动只是坐标计算方式不同,其它都类似。另外扩充到GIS等,可以根据小图片粘贴实现局部加载内容。

相关内容