【问题标题】:How to add padding completely around a bitmap programmatically?如何以编程方式在位图周围完全添加填充?
【发布时间】:2021-05-27 00:30:01
【问题描述】:

我正在尝试以编程方式向位图添加填充,但是,当我将填充添加到顶部时,它会从底部移除。当我将它添加到底部时,它会从顶部移除。

在顶部添加填充

Bitmap backgroundBitmap = ((BitmapDrawable) backgroundDrawable).getBitmap();


            int padding = 60;

            int positionLeft = 0;
            int positionTop = 0;
            Bitmap mainBitmap = Bitmap.createBitmap(
                    backgroundBitmap.getWidth(), //Adding top padding here
                    backgroundBitmap.getHeight() + padding,
                    Bitmap.Config.ARGB_8888
            );
            Canvas canvas = new Canvas(mainBitmap);
            canvas.drawBitmap(
                    backgroundBitmap,
                    positionLeft + padding,
                    positionTop + padding,
                    null
            );

在底部添加内边距会移除顶部内边距

 Bitmap backgroundBitmap = ((BitmapDrawable) backgroundDrawable).getBitmap();


            int padding = 60;

            int positionLeft = 0;
            int positionTop = 0;
            Bitmap mainBitmap = Bitmap.createBitmap(
                    backgroundBitmap.getWidth(),
                    backgroundBitmap.getHeight() + padding,
                    Bitmap.Config.ARGB_8888
            );
            Canvas canvas = new Canvas(mainBitmap);
            canvas.drawBitmap(
                    backgroundBitmap,
                    positionLeft + padding,
                    positionTop, //Adding bottom padding here
                    null
            );

想不通

如何向我的backgroundBitmap 位图的所有 4 个面添加填充?

编辑 1:_______________________________

我也试过这个解决方案:

https://stackoverflow.com/a/15525394/11110509

向位图添加边框,但它只是将边框添加到顶部和左侧。不包括底部和右侧

private Bitmap addWhiteBorder(Bitmap bmp, int borderSize) {
    Bitmap bmpWithBorder = Bitmap.createBitmap(bmp.getWidth() + borderSize * 2, bmp.getHeight() + borderSize * 2, bmp.getConfig());
    Canvas canvas = new Canvas(bmpWithBorder);
    canvas.drawColor(Color.BLUE);
    canvas.drawBitmap(bmp, borderSize, borderSize, null);
    return bmpWithBorder;
}
Bitmap backgroundBitmap = ((BitmapDrawable) backgroundDrawable).getBitmap();


            int padding = 60;

            int positionLeft = 0;
            int positionTop = 0;
            Bitmap mainBitmap = Bitmap.createBitmap(
                    backgroundBitmap.getWidth(),
                    backgroundBitmap.getHeight(),
                    Bitmap.Config.ARGB_8888
            );
            Canvas canvas = new Canvas(mainBitmap);
            canvas.drawBitmap(
                    addWhiteBorder(backgroundBitmap, padding),
                    positionLeft,
                    positionTop,
                    null
            );

【问题讨论】:

    标签: android android-canvas android-bitmap


    【解决方案1】:

    这可能会或可能不会帮助您,但不要填充位图,将其添加到视图中的容器中,如线性布局,然后填充线性布局。它应该更容易处理。

    int left = (canvas.getWidth()-mainBitmap.getWidth())/2;
    int top = (canvas.getHeight()-mainBitmap.getHeight())/2;
    canvas.drawBitmap(mainBitmap, left, top, null);
    

    这应该使位图相对于画布居中。 如果您需要更多信息,我从这里得到它How to align the canvas object to center of the screen?

    【讨论】:

    • 我不能在我的情况下使用它,因为我需要整个位图。如果我尝试将添加到视图的位图转换回位图,当我将其保存到我的应用程序的照片目录时它会很小。
    • 我也尝试过这个解决方案:stackoverflow.com/a/15525394/11110509 为位图添加边框,但它只是在顶部和左侧添加边框。不包括底部和右侧。 i.imgur.com/ILunzSl.png
    【解决方案2】:

    主要问题是我的主位图中没有足够的填充空间

                Bitmap backgroundBitmap = ((BitmapDrawable) backgroundDrawable).getBitmap();
                Bitmap appLogoBitmap = ((BitmapDrawable) appLogoDrawable).getBitmap();
    
                int padding = 60;
    
                int positionLeft = 0;
                int positionTop = 0;
                Bitmap mainBitmap = Bitmap.createBitmap(
                        backgroundBitmap.getWidth() + (padding * 2), //Adding the * 2 helped display the padding
                        backgroundBitmap.getHeight()  + (padding * 2),
                        Bitmap.Config.ARGB_8888
                );
                Canvas canvas = new Canvas(mainBitmap);
                canvas.drawBitmap(
                        addWhiteBorder(backgroundBitmap, padding),
                        positionLeft,
                        positionTop,
                        null
                );
    

    以及添加填充的功能:

    private Bitmap addWhiteBorder(Bitmap bmp, int borderSize) {
            Bitmap bmpWithBorder = Bitmap.createBitmap(bmp.getWidth() + borderSize * 2, bmp.getHeight() + borderSize * 2, bmp.getConfig());
            Canvas canvas = new Canvas(bmpWithBorder);
            canvas.drawColor(Color.parseColor("#63A4FF"));
            canvas.drawBitmap(bmp, borderSize, borderSize, null);
            return bmpWithBorder;
        }
    

    【讨论】:

      猜你喜欢
      • 2012-03-29
      • 1970-01-01
      • 2012-02-28
      • 2018-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-16
      • 2017-04-16
      相关资源
      最近更新 更多