简体   繁体   English

如何在Android中制作可滚动矩阵?

[英]How can I make a scrollable matrix in Android?

I am trying to build a simple app that will show a bidimensional, scrollable matrix of square tiles. 我正在尝试构建一个简单的应用,该应用将显示二维,可滚动的正方形方块矩阵。 Imagine a 2D Rubick's Cube, where you can choose to scroll a single row or a single column as if it was a ring. 想象一下2D Rubick's Cube,您可以在其中选择滚动单行还是单列,就像它是圆环一样。

I've placed a RelativeLayout inside the main view, and placed all the tiles of the matrix inside of it, through a custom implementation of the ImageView component. 我通过自定义实现ImageView组件,在主视图中放置了RelativeLayout ,并在其中放置了矩阵的所有图块。 I set the exact position of the single tile manipulating its RelativeLayout.LayoutParams value: 我设置单个图块的确切位置,以操纵其RelativeLayout.LayoutParams值:

RelativeLayout rl = ...;
TileView tile = ...;
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(size, size);
rlp.setMargins(left, top, 0, 0);
rl.addView(tile, rlp);

I really do not like this approach since I would like to change the top and left parameters of the tile instead of the margins, but that's the best I have been able to do. 我真的不喜欢这种方法,因为我想更改图块的顶部和左侧参数而不是边缘,但这是我所能做到的最好的。

I attached a listener to each tile to handle the dragging of the relative row or column, depending on the direction of the dragging event. 我在每个图块上附加了一个侦听器,以处理相对行或列的拖动,具体取决于拖动事件的方向。 Everything seems to work quite fine, except for a few problems: 除以下几个问题外,一切似乎都可以正常工作:

  1. When I scroll a row to the right, the last tile on the right margin will progressively shrink as if it was trying to fill the remaining space, and will eventually disappear. 当我向右滚动一行时,右边距上的最后一个图块将逐渐缩小,就好像它试图填充剩余的空间一样,并最终消失。
  2. When I then scroll back to the left the disappeared tiles will not reappear. 然后,当我向左滚动时,消失的瓷砖将不会重新出现。

Furthermore, I still have to figure what is the best way to give the user the illusion of an infinite row/column. 此外,我仍然必须弄清楚给用户以无限行/列的错觉的最佳方法是什么。 I think I should replicate all the tiles on both sides of each row/column, probably only when that row/column is being scrolled. 我想我应该复制每行/列两侧的所有磁贴,可能仅在滚动该行/列时才复制。

I am asking you for help just to understand what you think is the best way to realize this interface, since I think I am messing up with the code. 我要求您提供帮助只是为了了解您认为实现此接口的最佳方法,因为我认为我正在弄乱代码。 If you need any more details, just ask me. 如果您需要更多详细信息,请问我。

Thank you! 谢谢!

If I were doing this, my inclination would be to go at it at a lower level than you are. 如果我这样做的话,我的意愿是将其降低到比您更低的水平。 I'd use a SurfaceView as the main View and render the tiles with Canvas.drawBitmap() . 我将使用SurfaceView作为主视图,并使用Canvas.drawBitmap()渲染图块。 It probably sounds like more work, but I think that it's the right way to do what you're trying to do, and that ultimately you'll be happier with the results. 听起来可能需要做更多的工作,但是我认为这是做您想做的事情的正确方法,最终您会对结果感到满意。

To address your two problems: 解决您的两个问题:

  1. The version of drawBitmap() I linked to lets you specify the subset of the Bitmap to draw, so you can clip the tile rather than shrinking it as it goes off the screen. 我链接到的drawBitmap()的版本允许您指定要绘制的位图的子集,因此您可以裁剪图块,而不是在其离开屏幕时缩小它。
  2. You'll be responsible for determining what goes where (and drawing it there), so your data structure representing the tile matrix needs to represent off-screen tiles as well. 您将负责确定要去的地方(并将其绘制到那里),因此,表示图块矩阵的数据结构也需要表示屏幕外的图块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM