使位图在 x 方向可绘制平铺,但在 y 方向拉伸
我有一个图像用作相对布局的背景。图像需要水平平铺才能形成图案。
我可以使用以下代码使图像水平平铺:
BitmapDrawable b3 = (BitmapDrawable)getResources().getDrawable(R.drawable.background);
b3.setTileModeX(Shader.TileMode.REPEAT);
v.findViewById(R.id.layout).setBackgroundDrawable(b3);
问题是图像也垂直平铺。它似乎在垂直方向上以“夹紧”模式平铺,但在水平方向上以“重复”模式平铺。这是一个屏幕截图:
如您所见,图像比它所占用的空间小一点点,并且底部边缘被“夹紧”。
如何将图像设置为垂直拉伸但水平平铺?
I have an image which I'm using as a background for a RelativeLayout. The image needs to be tiled horizontally to make a pattern.
I'm able to get the image to tile horizontally by using this code:
BitmapDrawable b3 = (BitmapDrawable)getResources().getDrawable(R.drawable.background);
b3.setTileModeX(Shader.TileMode.REPEAT);
v.findViewById(R.id.layout).setBackgroundDrawable(b3);
The problem is that the image also tiles vertically. It seems to tile in the "clamp" mode in the vertical, but "repeat" mode in the horizontal. Here is a screenshot:
As you can see, the image is just a little bit smaller than the space it occupies, and the bottom edge is "clamped".
How can I set the image to stretch vertically but tile horizontally?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
此方法调用创建新位图,但看起来它正在实现您的目标
。请注意,它仅在视图已布局的情况下才有效,因此方法 lile
onWindowFocusChanged
是放置此代码的好地方。This method invokes creation of new bitmap but it looks like it's acrhiving your goal
Please note that it only works if view was already layouted, so a method lile
onWindowFocusChanged
is a good place for this code.我觉得它很简单:(此代码将在 Y 中平铺并在 x 中重复)
在您的 onWindowFoucsChanged 中您调用:
I feel it is straight forward:(This code will tile in Y and repeat in x)
In your onWindowFoucsChanged you call:
对你来说太晚了,但对其他人可能有用。
您可以创建自定义视图来执行此操作。只需将源位图缩放到与视图一样高,然后在画布上重复绘制它:
Far too late for you, but may be useful for others.
You can create a custom View in order to do this. Simply scale your source bitmap to be as high as your view and then draw it repeatedly on the canvas:
我自己也有同样的问题。尝试使用 9patch 图像,但似乎您不能将 9patch 图像与平铺一起使用,无论您在平铺属性中定义什么,9patch 都会拉伸图像。
最后我要做的就是要求图像的创建者将其垂直拉伸或自己做。如果有人找到一个更好的解决方案,我希望有一个更好的解决方案。
Having the same problem myself. Tried using 9patch image but it seems you cannot use 9patch images along with tiling, the 9patch stretches the image no matter what you define in the tiling property.
At the end what i will do is ask the creator of the image to stretch it vertically or do it myself. I would love a better solution if anyone finds one.