SoFunction
Updated on 2025-03-10

Example of method of partial loading and moving basemap in Android development

This article describes the method of partial loading and moving basemap in Android development. Share it for your reference, as follows:

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, the premise is that your picture is indeed larger than the screen. If it is not larger than the screen, just modify the comment line below.  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){
     = width;
     = height;
    Bitmap bmp = ().getBackGroundBitmap();
    b_x = (()-width)/2;//Make sure the picture is larger than the screen    b_y = (()-height)/2;
    bmpView = (bmp, b_x, b_y, width, height);
  }
  public void logic(){
  }
  public void mapDown(){
    Bitmap bmp = ().getBackGroundBitmap();
    if(b_y+height<()){
      b_y = b_y + ()/map_num;
      if(b_y+height>()){
        b_y = () - height;
      }
    }
    bmpView = (bmp, b_x, b_y, width, height);
  }
  public void mapUp(){
    Bitmap bmp = ().getBackGroundBitmap();
    if(b_y>0){
      b_y = b_y - ()/map_num;
      if(b_y<0){
        b_y = 0;
      }
    }
    bmpView = (bmp, b_x, b_y, width, height);
  }
  public void mapLeft(){
    Bitmap bmp = ().getBackGroundBitmap();
    if(b_x>0){
      b_x = b_x - ()/map_num;
      if(b_x<0){
        b_x = 0;
      }
    }
    bmpView = (bmp, b_x, b_y, width, height);
  }
  public void mapRight(){
    Bitmap bmp = ().getBackGroundBitmap();
    if(b_x+width<()){
      b_x = b_x + ()/map_num;
      if(b_x+width>()){
        b_x = () - width;
      }
    }
    bmpView = (bmp, b_x, b_y, width, height);
  }
  public void draw(Canvas canvas){
    Paint paint = new Paint();
    if(bmpView!=null){
      (bmpView,0, 0, paint);
    }
  }
}

Call

public void onKeyDownDeal(int keyCode){
    if(keyCode==KeyEvent.KEYCODE_DPAD_UP){
      ().mapUp();
    }else if(keyCode==KeyEvent.KEYCODE_DPAD_DOWN){
      ().mapDown();
    }else if(keyCode==KeyEvent.KEYCODE_DPAD_LEFT){
      ().mapLeft();
    }else if(keyCode==KeyEvent.KEYCODE_DPAD_RIGHT){
      ().mapRight();
    }
}

Then use the thread to call draw to refresh.

The coordinate calculation method for touch movement is different, and the others are similar. In addition, it can be expanded to GIS, etc., and local loading content can be achieved based on small pictures.

For more information about Android related content, please check out the topic of this site:Summary of Android graphics and image processing skills》、《Android development introduction and advanced tutorial》、《Android debugging skills and solutions to common problems》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.