我目前正在Android中实现一个视图,该视图涉及使用大于屏幕尺寸的位图作为背景,然后在此处绘制可绘制的绘图。这是为了模拟可以水平滚动以及垂直滚动的“地图”。
通过使用画布然后绘制完整的“地图”位图,然后将其他图像作为叠加层放在顶部,然后仅将其中的可视位绘制到屏幕来完成。
覆盖触摸事件以在滚动/拖动时重绘屏幕。
我确信这可能有很大的开销(通过创建完整图像的画布,同时使用(绘图)只有五分之一)并且可以以不同的方式完成,但是我只是想知道在这种情况下人们会做什么,也许还有例子?
如果您需要更多信息,请告诉我们,
谢谢,
西蒙
答案 0 :(得分:3)
我使用an example API编写了BitmapRegionDecoder
如何执行此操作的方法。该示例有一个大的(6000,4000)image of the world,用户可以全分辨率滚动。 initial tag非常小且易于理解。
答案 1 :(得分:0)
我会将巨大的图像划分为图块,然后根据图像的哪个部分显示来绘制相应的图块。几乎就是谷歌地图的功能。您可以查看http://openzoom.org/。 Android中没有任何内容,但我认为您可以采用相同的方法。
答案 2 :(得分: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;
}