【问题标题】:Placing an image in the transparent area of another image android将图像放置在另一个图像android的透明区域
【发布时间】:2016-08-03 06:21:03
【问题描述】:

我正在尝试用另一个图像替换框架图像的透明区域,我只想用我的另一个图像替换那个透明区域。 这就是我从帧的位图中获取透明像素并存储它的方法。但它不起作用。

 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;
            }
        }
    }

下面是我正在使用的透明框架。

如上所述,我想用另一张图片填充透明区域?
任何帮助或指导将不胜感激谢谢!

【问题讨论】:

  • 当你说它不起作用时,什么不起作用?图像具有透明像素。您是找不到它们还是无法将其他图像放在此图像上。 i 是什么?
  • 我需要一些帮助来更好地理解这个问题,你想在你的苹果里面放一张图片吗?

标签: android image imageview transparent


【解决方案1】:

您可以尝试创建一个自定义视图,它会做 3 件事:

  1. 将蒙版与背景相结合
  2. 将结果绘制到画布上
  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 时才有效

【讨论】:

    【解决方案2】:

    如果您只想用另一张图像的相应 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图像)后的结果如下:

    如果您想替换苹果图像的非透明区域(白色像素),结果将如下所示:

    【讨论】:

      猜你喜欢
      • 2016-03-12
      • 1970-01-01
      • 2012-08-02
      • 1970-01-01
      • 2013-01-02
      • 2011-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多