欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android开发之底图局部加载移动的方法示例

程序员文章站 2023-12-04 12:37:28
本文实例讲述了android开发之底图局部加载移动的方法。分享给大家供大家参考,具体如下: public class mapmgr { public sta...

本文实例讲述了android开发之底图局部加载移动的方法。分享给大家供大家参考,具体如下:

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等,可以根据小图片粘贴实现局部加载内容。

更多关于android相关内容感兴趣的读者可查看本站专题:《android图形与图像处理技巧总结》、《android开发入门与进阶教程》、《android调试技巧与常见问题解决方法汇总》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。