我正在尝试用另一个图像替换帧图像的透明区域,我只想用我的其他图像替换该透明区域。 这就是我从帧的位图获取透明像素并存储它。但它不起作用。
for (int x = 0; x < imgBitmap.getWidth(); x++)
{
for (int y = 0; y < imgBitmap.getHeight(); y++)
{
if (imgBitmap.getPixel(x, y) == Color.TRANSPARENT)
{
bottomX[i] = x;
bottomY[i] = y;
break;
}
}
}
正如我上面提到的,我想用另一张图片填充透明区域? 任何帮助或指南将不胜感激,谢谢!
答案 0 :(得分:1)
您可以尝试创建一个自定义视图,它可以做 3 件事:
class TransparentImageView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0,
) : AppCompatImageView(context, attrs, defStyleAttr) {
private val background: Bitmap
private val mask: Bitmap
private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
private val modeIn = PorterDuffXfermode(PorterDuff.Mode.DST_IN)
init {
context.obtainStyledAttributes(attrs, R.styleable.TransparentImageView, 0, 0).apply {
background = BitmapFactory.decodeResource(context.resources, getResourceId(R.styleable.TransparentImageView_bg, -1))
mask = BitmapFactory.decodeResource(context.resources, getResourceId(R.styleable.TransparentImageView_mask, -1))
recycle()
}
}
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
val result = combine()
canvas?.drawBitmap(result, 0f, 0f, null)
// Draw the Masked image on top for shadows
canvas?.drawBitmap(mask, 0f, 0f, null)
}
private fun combine(): Bitmap {
val result = Bitmap.createBitmap(mask.width, mask.height, Bitmap.Config.ARGB_8888)
val tempCanvas = Canvas(result)
paint.xfermode = modeIn
tempCanvas.drawBitmap(background, 0f, 0f, null)
tempCanvas.drawBitmap(mask, 0f, 0f, paint)
paint.xfermode = null
return result
}
}
这里,我使用自定义属性:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TransparentImageView">
<attr name="mask" format="reference" />
<attr name="bg" format="reference" />
</declare-styleable>
</resources>
你可以这样使用它:
<my.custom.TransparentImageView
android:id="@+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
app:bg="@drawable/background"
app:mask="@drawable/mask"/>
这仅在您的蒙版图像是 png 时有效
答案 1 :(得分:1)
如果您只想用另一个图像的相应 x,y 像素替换透明的 x,y 像素,您可以执行如下操作。第一个参数是您的透明图像位图,第二个参数是另一个图像位图,第三个参数是一个布尔标志,用于仅替换透明区域的像素。当然,您需要拥有相同宽度和高度的两个图像才能从另一个图像中获得正确的对应 x,y 像素值。
public static void combineBitmaps(Bitmap transBitmap, Bitmap otherBitmap, boolean replaceTransparentAreaOnly){
try
{
Bitmap outputBitmap = Bitmap.createBitmap(transBitmap.getWidth(), transBitmap.getHeight(), Bitmap.Config.ARGB_8888);
for (int x = 0; x < transBitmap.getWidth(); x++) {
for (int y = 0; y < transBitmap.getHeight(); y++) {
int rgba = transBitmap.getPixel(x, y);
int red = Color.red(rgba);
int blue = Color.blue(rgba);
int green = Color.green(rgba);
int alpha = Color.alpha(rgba);
//replace only transparent area
if(replaceTransparentAreaOnly)
{
//transparent pixel found, replace it with the corresponding x,y pixel of the other image
if (rgba == Color.TRANSPARENT) {
outputBitmap.setPixel(x, y, otherBitmap.getPixel(x, y));
}
//otherwise set x,y pixel based on transparent image RGB colour
else {
outputBitmap.setPixel(x, y, Color.rgb(red, green, blue));
}
}
//replace non-transparent area
else
{
//non-transparent pixel found, replace it with the corresponding x,y pixel of the other image
if (rgba != Color.TRANSPARENT) {
outputBitmap.setPixel(x, y, otherBitmap.getPixel(x, y));
}
//otherwise set x,y pixel based on transparent image RGB colour
else {
outputBitmap.setPixel(x, y, rgba);
}
}
}
}
//save the new outputBitmap here
}catch (Exception e){
}
}
你可以像下面这样调用这个辅助函数:
Bitmap appleBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.apple);
Bitmap androidBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.other_image);
combineBitmaps(appleBitmap, androidBitmap, true);
将 Apple 图像的仅透明区域替换为另一个(Android 图像)后的结果将如下所示:
如果您想替换苹果图像的非透明区域(白色像素),结果将如下所示: