android - 获取平铺位图/重复位图

标签 android bitmap imageview

我有一个无缝背景,我已经成功地使用下面的代码在屏幕上反复显示它:

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/

相关文章:

java - 如何在 android 中的对象框数据库中插入对象列表?喜欢房间数据库

c++ - 在 WxWidgets 中使用位图

android - Activity 转换后 ImageView 位图被拉伸(stretch)

android .so 文件未在/data/data/<package>/lib 中提取

android - 如何在 ActionBarSherlock 中将 "setOnEditorActionListener"设置为 EditText?

Android:如何确保在我调用另一个方法之前终止 AsyncTask?

c# - 从桌面拖动图像并将其保存为位图

java - 微小的图像,没有旋转,但仍然得到 OutOfMemoryError : bitmap size exceeds VM budget

java - 在 ImageView 中居中对齐图像 - Android

android - 使用搜索栏在 ImageView 中放大/缩小效果