2012-06-02 53 views
2

我想创建一个视图,它将在画布内包含一个大的位图(大于屏幕)。以便用户可以滚动Bitmap并在其上绘制手指涂料。我正在关注Android API演示中的FingerPaint代码。Android:在画布内滚动位图

但我无法生成滚动BitmapCanvas。增加位图的大小似乎不起作用。

我试图在ScrollView里添加视图类,但是视图的onDraw()没有被调用,我得到一个空白屏幕。

请帮我这个。谢谢。

回答

5

尽量让自定义视图像这样:

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; 
    } 

在此链接,您可以找到如何做到这一点使用BitmapRegionDecoder API的example。这个例子有一个大的(6000,4000)世界图像,用户可以全分辨率滚动。

+0

谢谢K_Anas的答案。但我不想将任何外部图像用作位图。位图必须由Bitmap.createBitmap(宽度,高度,Bitmap.Config.ARGB_8888)创建,并在调用onDraw(Canvas canvas)时在画布上绘制。它就像API演示中的FingerPaint示例一样。但它应该是可滚动的。 – sugandhs

+0

@K_Anas,上面的代码可能会耗尽内存如果位图很大,是否正确?所以你建议呃位图区域解码器还是有一种更简单的方法 – Snake