2011-02-11 77 views
1

我目前在Android中實現一個視圖,其中涉及使用大於屏幕大小的位圖作爲背景,然後繪製可繪製的ontop。這是爲了模擬可以水平滾動和垂直滾動的「地圖」。Android:可滾動(位圖)屏幕

這是通過使用畫布,然後繪製到完整的「地圖」位圖,然後將其他圖像放在頂部作爲覆蓋,然後只畫這個可視位來完成。

覆蓋觸摸事件以在滾動/跳動時重畫屏幕。我可以肯定這可能有一個巨大的開銷(通過創建一個完整的圖像畫布,同時使用(繪圖)只有五分之一),並可以以不同的方式完成解釋,但我只是想知道人們會在這種情況下做什麼,或許是例子?

如果您需要更多的信息只是讓我知道,

感謝,

西蒙

回答

0

我寫這個類到我正在開發的一個項目中。

public class ScrollableImage extends View { 
    private Bitmap bmLargeImage; // bitmap large enough to be scrolled 
    private Rect displayRect = null; // rect we display to 
    private Rect scrollRect = null; // rect we scroll over our bitmap with 
    private int scrollRectX = 0; // current left location of scroll rect 
    private int scrollRectY = 0; // current top location of scroll rect 
    private float scrollByX = 0; // x amount to scroll by 
    private float scrollByY = 0; // y amount to scroll by 

    private int width, height; 

    private Paint background; 

    public ScrollableImage(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public ScrollableImage(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    public void setSize(int width, int height) { 
     background = new Paint(); 
     background.setColor(Color.WHITE); 
     this.width = width; 
     this.height = height; 

     // Destination rect for our main canvas draw. It never changes. 
     displayRect = new Rect(0, 0, width, height); 
     // Scroll rect: this will be used to 'scroll around' over the 
     // bitmap in memory. Initialize as above. 
     scrollRect = new Rect(0, 0, width, height); 
     // scrollRect = new Rect(0, 0, bmp.getWidth(), bmp.getHeight()); 
    } 

    public void setImage(Bitmap bmp) { 
     if (bmLargeImage != null) 
      bmLargeImage.recycle(); 

    bmLargeImage = bmp; 
     scrollRect = new Rect(0, 0, width, height); 
     scrollRectX = 0; 
     scrollRectY = 0; 
     scrollByX = 0; 
     scrollByY = 0; 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     return true; // done with this event so consume it 
    } 

    public void notifyScroll(float distX, float distY) { 
     scrollByX = distX; // move update x increment 
     scrollByY = distY; // move update y increment 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 

     if (bmLargeImage == null) 
      return; 

     if (scrollByX != 0 || scrollByY != 0) { 
      // Our move updates are calculated in ACTION_MOVE in the opposite  direction 
      // from how we want to move the scroll rect. Think of this as 
      // dragging to 
      // the left being the same as sliding the scroll rect to the right. 
      int newScrollRectX = scrollRectX - (int) scrollByX; 
      int newScrollRectY = scrollRectY - (int) scrollByY; 
      scrollByX = 0; 
      scrollByY = 0; 

      // Don't scroll off the left or right edges of the bitmap. 
      if (newScrollRectX < 0) 
       newScrollRectX = 0; 
      else if (newScrollRectX > (bmLargeImage.getWidth() - width)) 
       newScrollRectX = (bmLargeImage.getWidth() - width); 

      // Don't scroll off the top or bottom edges of the bitmap. 
      if (newScrollRectY < 0) 
       newScrollRectY = 0; 
      else if (newScrollRectY > (bmLargeImage.getHeight() - height)) 
       newScrollRectY = (bmLargeImage.getHeight() - height); 
      scrollRect.set(newScrollRectX, newScrollRectY, newScrollRectX 
        + width, newScrollRectY + height); 

      scrollRectX = newScrollRectX; 
      scrollRectY = newScrollRectY; 
     } 

     canvas.drawRect(displayRect, background); 
     // We have our updated scroll rect coordinates, set them and draw. 
     canvas.drawBitmap(bmLargeImage, scrollRect, displayRect, background); 

    } 
} 

而在手勢聽衆我有這個實施onScroll

IMG在哪裏你ScrollableImage實例。

請記住在大圖像中使用setImage。 編輯:另請使用setSize設置顯示器的大小。

 public boolean onScroll(MotionEvent e1, MotionEvent e2, 
       float distanceX, float distanceY) { 
       img.notifyScroll(-distanceX, -distanceY); 
       img.invalidate(); 
      return true; 
     } 
0

我會分裂的巨大形象成片,然後繪製根據適當的地磚上的圖像的一部分必須顯示。幾乎Google Maps的功能。您可以檢查http://openzoom.org/。 Android沒有任何東西,但我認爲你可以採用同樣的方法。

+0

感謝您的建議會給建議一個閱讀,看看它是否比上述更容易實施。 – somin 2011-03-11 10:57:47

+0

對於在內存中保留一個大的位圖,我只是有點懷疑。 Android有很多位圖的內存問題,並試圖找到一個聰明的方式來處理它們。我將自己處理Openzoom類型的實現。 :) – Abhinav 2011-03-11 11:45:06