歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android底圖局部加載移動

Android底圖局部加載移動

日期:2017/3/1 10:27:59   编辑:Linux編程
  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. public void mapDown(){
  29. Bitmap bmp = PicMgr.getInstance().getBackGroundBitmap();
  30. if(b_y+height<bmp.getHeight()){
  31. b_y = b_y + bmp.getHeight()/map_num;
  32. if(b_y+height>bmp.getHeight()){
  33. b_y = bmp.getHeight() - height;
  34. }
  35. }
  36. bmpView = Bitmap.createBitmap(bmp, b_x, b_y, width, height);
  37. }
  38. public void mapUp(){
  39. Bitmap bmp = PicMgr.getInstance().getBackGroundBitmap();
  40. if(b_y>0){
  41. b_y = b_y - bmp.getHeight()/map_num;
  42. if(b_y<0){
  43. b_y = 0;
  44. }
  45. }
  46. bmpView = Bitmap.createBitmap(bmp, b_x, b_y, width, height);
  47. }
  48. public void mapLeft(){
  49. Bitmap bmp = PicMgr.getInstance().getBackGroundBitmap();
  50. if(b_x>0){
  51. b_x = b_x - bmp.getWidth()/map_num;
  52. if(b_x<0){
  53. b_x = 0;
  54. }
  55. }
  56. bmpView = Bitmap.createBitmap(bmp, b_x, b_y, width, height);
  57. }
  58. public void mapRight(){
  59. Bitmap bmp = PicMgr.getInstance().getBackGroundBitmap();
  60. if(b_x+width<bmp.getWidth()){
  61. b_x = b_x + bmp.getWidth()/map_num;
  62. if(b_x+width>bmp.getWidth()){
  63. b_x = bmp.getHeight() - width;
  64. }
  65. }
  66. bmpView = Bitmap.createBitmap(bmp, b_x, b_y, width, height);
  67. }
  68. public void draw(Canvas canvas){
  69. Paint paint = new Paint();
  70. if(bmpView!=null){
  71. canvas.drawBitmap(bmpView,0, 0, paint);
  72. }
  73. }
  74. }

調用

  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等,可以根據小圖片粘貼實現局部加載內容。

Copyright © Linux教程網 All Rights Reserved