如何以编程方式完全围绕位图添加填充?
How to add padding completely around a bitmap programmatically?
我正在尝试以编程方式向位图添加填充,但是,当我将填充添加到顶部时,它会从底部移除。当我将它添加到底部时,它会从顶部移除。
在顶部添加内边距
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:____________________________________________
我也尝试过这个解决方案:
为位图添加边框,但它只是在顶部和左侧添加边框。不包括底部和右侧
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
);
这可能对您有帮助,也可能对您没有帮助,但不要填充位图,将其添加到视图中的容器,如线性布局,然后填充线性布局。对付起来应该会容易很多。
int left = (canvas.getWidth()-mainBitmap.getWidth())/2;
int top = (canvas.getHeight()-mainBitmap.getHeight())/2;
canvas.drawBitmap(mainBitmap, left, top, null);
这应该使位图相对于 canvas 居中。
如果你需要更多信息,我是从这里得到的 How to align the canvas object to center of the screen?
主要问题是我的主位图中没有足够的填充 space
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;
}
我正在尝试以编程方式向位图添加填充,但是,当我将填充添加到顶部时,它会从底部移除。当我将它添加到底部时,它会从顶部移除。
在顶部添加内边距
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:____________________________________________
我也尝试过这个解决方案:
为位图添加边框,但它只是在顶部和左侧添加边框。不包括底部和右侧
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
);
这可能对您有帮助,也可能对您没有帮助,但不要填充位图,将其添加到视图中的容器,如线性布局,然后填充线性布局。对付起来应该会容易很多。
int left = (canvas.getWidth()-mainBitmap.getWidth())/2;
int top = (canvas.getHeight()-mainBitmap.getHeight())/2;
canvas.drawBitmap(mainBitmap, left, top, null);
这应该使位图相对于 canvas 居中。 如果你需要更多信息,我是从这里得到的 How to align the canvas object to center of the screen?
主要问题是我的主位图中没有足够的填充 space
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;
}