使位图绘制平铺在X,但舒展y中 [英] Make bitmap drawable tile in x but stretch in y
问题描述
我有我使用作为背景的RelativeLayout的图像。的图像需要被横向平铺使一个图案。
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.
我能够获得图像水平平铺使用该code:
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?
推荐答案
这个方法调用创建新位图,但它看起来像它的acrhiving你的目标
This method invokes creation of new bitmap but it looks like it's acrhiving your goal
View view = findViewById(R.id.layout);
BitmapDrawable bd = (BitmapDrawable) getResources().getDrawable(R.drawable.tile);
int width = view.getWidth();
int intrinsicHeight = bd.getIntrinsicHeight();
Rect bounds = new Rect(0,0,width,intrinsicHeight);
bd.setTileModeX(TileMode.REPEAT);
bd.setBounds(bounds);
Bitmap bitmap = Bitmap.createBitmap(bounds.width(), bounds.height(), bd.getBitmap().getConfig());
Canvas canvas = new Canvas(bitmap);
bd.draw(canvas);
BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
view.setBackgroundDrawable(bitmapDrawable);
请注意,只有当有人已经layouted,这样的方法LILE onWindowFocusChanged
是该code的好地方。
Please note that it only works if view was already layouted, so a method lile onWindowFocusChanged
is a good place for this code.
这篇关于使位图绘制平铺在X,但舒展y中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!