我有一个无缝背景,我已经成功地使用下面的代码在屏幕上反复显示它:
BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), bitmap);
bitmapDrawable.setTileModeX(Shader.TileMode.REPEAT);
bitmapDrawable.setTileModeY(Shader.TileMode.REPEAT);
mBackground.setBackground(bitmapDrawable);
但是,我在稍后保存位图时遇到了问题。背景在宽度和高度上与屏幕尺寸相匹配。当我尝试使用以下代码获取位图时,保存的图像只是原始的无缝图案。
((BitmapDrawable) mBackground.getBackground()).getBitmap()
我不知道如何生成重复的背景图像。
最佳答案
这段代码可能有帮助:
int width = 100;//initialise
int height= 100;//initialise
//you say "The background is matched screen size in the question"
width = mBackground.getWidth();// get width of screen
height = mBackground.getHeight();// get height of screen
//Create bitmap for canvas
Bitmap tiledBitmap = Bitmap.createBitmap( width , height, Bitmap.Config.ARGB_8888 );//create a Bipmap for us to save later
canvas = new Canvas( tiledBitmap );//associate bitmap to canvas
bitmapDrawable.setBounds( 0, 0, width , height );//set bounds
bitmapDrawable.draw( canvas );//draw tiled image to canvas ( and so Bitmap )
//tiledBitmap now contains your tiled image
关于android - 获取平铺位图/重复位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45275306/