[英]Android zoomable/scrollable ImageView with overlays
I would like to display an image (including scroll and zoom functionality). 我想显示图像(包括滚动和缩放功能)。 Additionally, I need to add some overlays to the image dynamically.
另外,我需要动态地为图像添加一些叠加层。 The overlays are in a relationship to the content within the image.
叠加层与图像中的内容有关系。
In other words: I'd like to implement a Map with some markers, but I provide my own map material. 换句话说:我想用一些标记来实现Map,但我提供了自己的地图材质。
My main question is: How could I do this? 我的主要问题是:我怎么能这样做?
What I have tried so far 到目前为止我尝试过的
Questions 问题
Is there a simple solution or approach to my problem? 我的问题有一个简单的解决方案或方法吗?
What is the right way of adding and moving views at runtime in Android? 在Android中运行时添加和移动视图的正确方法是什么? (Usually, I would use a basic layout and play with the margin - would that be best practice?)
(通常,我会使用基本布局并使用保证金 - 这是最佳做法吗?)
Could somebody point me to the right direction, please. 请有人指出我正确的方向。
Min. 闵。 API Level: 10
API等级:10
I would suggest extending the View
class and overriding onDraw
method. 我建议扩展
View
类并重写onDraw
方法。 Take a look at the Canvas API . 看看Canvas API 。
You'll probably have an onDraw method that looks like : 您可能有一个onDraw方法,如下所示:
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
canvas.translate(mPosX, mPosY); //position of your actual data
canvas.scale(mScaleFactor, mScaleFactor); // zoom factor
map.draw(canvas); //you draw your map here
canvas.restore(); //go back to saved state
//for each marker you have, do the same "save - translate - scale - draw" loop
}
Since you don't need your markers to be zoomable, you'll probably wont need scale step but you need to calculate their position properly. 由于您不需要缩放标记,因此您可能不需要缩放步骤,但您需要正确计算它们的位置。
Depending on your # of objects etc, you may need to have a more optimized way of drawing your markers (eg re-drawing only changed rectangular areas). 根据您的#对象等,您可能需要更优化的方式来绘制标记(例如,仅重新绘制已更改的矩形区域)。 see View.invalidate(Rect)
请参阅View.invalidate(Rect)
Have you tried subclassing View and handling yourself user gestures? 您是否尝试过子类化View并处理自己的用户手势? It is my first choice for complex behaviours as such.
这是我复杂行为的首选。 It should look something like this:
它应该看起来像这样:
Plenty of material about this individual tasks on StackOverflow, let me know if you need adittional help in any of these. 关于StackOverflow上这些单独任务的大量资料,请告诉我您是否需要其中任何一个的adittional帮助。
After a long time, I found the exact answer to my solution: TileView 很长一段时间后,我找到了解决方案的确切答案: TileView
Setting up the map view: 设置地图视图:
@Override
protected void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
TileView tileView = new TileView( this );
tileView.setSize( 2000, 3000 ); // the original size of the untiled image
tileView.addDetailLevel( 1f, "tile-%d-%d.png");
setContentView( tileView );
}
Adding the marker: 添加标记:
tileView.addMarker( someView, 250, 500, -0.5f, -1.0f );
Code from the documentation. 文档中的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.