安卓:填充留下白色的位图

2022-09-03 04:11:55

如何将位图左侧的10行全部设置为白色?我有一个位图,必须在左侧填充。我想我可以为每个位置创建一个新的图像迭代旧的getpixel,并在新的图像(白色或彩色)上设置像素,然后返回新的位图...这是错的吗?有什么建议吗?多谢!


答案 1

您可以改为使用额外的填充像素数创建新的位图。将其设置为画布位图,并用所需的颜色对整个图像着色,然后复制位图。

public Bitmap pad(Bitmap Src, int padding_x, int padding_y) {
    Bitmap outputimage = Bitmap.createBitmap(Src.getWidth() + padding_x,Src.getHeight() + padding_y, Bitmap.Config.ARGB_8888);
    Canvas can = new Canvas(outputimage);
    can.drawARGB(FF,FF,FF,FF); //This represents White color
    can.drawBitmap(Src, padding_x, padding_y, null);
    return outputimage;
}

答案 2
public Bitmap addPaddingTopForBitmap(Bitmap bitmap, int paddingTop) {
    Bitmap outputBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight() + paddingTop, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(outputBitmap);
    canvas.drawColor(Color.RED);
    canvas.drawBitmap(bitmap, 0, paddingTop, null);
    return outputBitmap;
}

public Bitmap addPaddingBottomForBitmap(Bitmap bitmap, int paddingBottom) {
    Bitmap outputBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight() + paddingBottom, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(outputBitmap);
    canvas.drawColor(Color.RED);
    canvas.drawBitmap(bitmap, 0, 0, null);
    return outputBitmap;
}


public Bitmap addPaddingRightForBitmap(Bitmap bitmap, int paddingRight) {
    Bitmap outputBitmap = Bitmap.createBitmap(bitmap.getWidth() + paddingRight, bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(outputBitmap);
    canvas.drawColor(Color.RED);
    canvas.drawBitmap(bitmap, 0, 0, null);
    return outputBitmap;
}

public Bitmap addPaddingLeftForBitmap(Bitmap bitmap, int paddingLeft) {
    Bitmap outputBitmap = Bitmap.createBitmap(bitmap.getWidth() + paddingLeft, bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(outputBitmap);
    canvas.drawColor(Color.RED);
    canvas.drawBitmap(bitmap, paddingLeft, 0, null);
    return outputBitmap;
}