【问题标题】:android make bitmap drawable tile in x BUT not stratchable in yandroid在x中制作位图可绘制图块但在y中不可分层
【发布时间】:2023-03-12 03:27:01
【问题描述】:

我正在尝试使位图可绘制 repeat tile in x,对齐此可绘制视图 底部(按颜色填充可用空间),因此我的可绘制对象不应该分层是的。
而且我必须达到一个目标。

来源背景图:

在大屏幕中填充此图片上方的可用空间的颜色:#FF5cc8f2

这是第一个变体

BitmapDrawable tile = (BitmapDrawable) res
                .getDrawable(R.drawable.background);
        tile.setTileModeX(Shader.TileMode.REPEAT);

        view.setBackgroundColor(res.getColor(R.color.background));
        view.setBackgroundDrawable(tile);

结果:

如你所见,云层下方有多余的空白区域。

第二种变体:

BitmapDrawable bd = (BitmapDrawable) res.getDrawable(R.drawable.background);

            WindowManager wm = (WindowManager) AvApplication.getInstance().getSystemService(Context.WINDOW_SERVICE);
            Display display = wm.getDefaultDisplay();
            int width;
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR2) {
                width = display.getWidth();  
            } else {
                Point size = new Point();
                display.getSize(size);
                width = size.x;
            }

            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(res, bitmap);
            view.setBackgroundDrawable(bitmapDrawable);

结果:

现在图像在底部,但被拉伸了

不要在意一个屏幕截图是横向的,第二个是纵向的 - 两个方向都会检查结果。

我需要你的帮助..

【问题讨论】:

    标签: android drawable


    【解决方案1】:

    我会尝试:

    BitmapDrawable tile = (BitmapDrawable) res.getDrawable(R.drawable.background);
    tile.setTileModeX(Shader.TileMode.REPEAT);
    
    tile.setGravity(Gravity.bottom)
    tile.setTileModeY(Shader.TileMode.CLAMP); 
    // it replicates the edge color if the shader draws outside of its original bounds
    //thus you don't even need to set a background color.
    
    view.setBackgroundDrawable(tile);
    

    【讨论】:

    • 感谢您的回答,但结果与我的第一个案例相同。据我所知,不幸的是,启用平铺模式时,重力不起作用。
    • 哦,你是对的,看起来 CLAMP 只用于底部和右侧......奇怪。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-06
    • 1970-01-01
    • 1970-01-01
    • 2012-03-25
    • 1970-01-01
    • 2014-05-31
    • 1970-01-01
    相关资源
    最近更新 更多