简体   繁体   English

Android-将一张图片插入另一张图片的透明像素

[英]Android - Insert one image into another's transparent pixels

In Android, I'm attempting to layer two images (In this case, Bitmap objects) in a FrameLayout. 在Android中,我尝试在FrameLayout中分层两个图像(在本例中为Bitmap对象)。 One image is placed on top, with the other image inserted below it at that image's first transparent pixel. 一个图像放在顶部,另一图像插入到该图像的第一个透明像素下方。

Currently, I'm attempting the following using Bitmap.getPixel() to locate the transparent pixel: 当前,我正在尝试使用Bitmap.getPixel()进行以下操作来定位透明像素:

for (int i = 0; i < image_count; i++)
{
    //load page from internal memory
    topImages[i] = getImage("p"+i+".png");
    for (int x = 0; x < topImages[i].getWidth(); x++)
    {
        for (int y = 0; y < topImages[i].getHeight(); y++)
        {
            if (topImages[i].getPixel(x, y) == Color.TRANSPARENT)
            {
                bottomX[i] = x;
                bottomY[i] = y;
                break;
            }
        }
    }
}

I then add the images to a Frame Layout, using two ImageViews, and use these values to position the lower image: 然后,我使用两个ImageView将图像添加到“帧布局”中,并使用这些值来定位下部图像:

bottomView.setTranslationX(bottomX[position]);
bottomView.setTranslationY(bottomY[position]);

However, the placement of the bottom image is always inaccurate, usually by a margin down and to the right that is different (but always the same) for each given image. 但是,底部图像的放置总是不准确,通常每个给定图像的右下角和右下角都不同(但始终相同)。 I've also tried setX() and setY() , as well as using parameters to set the TOP and LEFT values similarly, with the same results). 我也尝试过setX()setY() ,以及使用参数类似地设置TOPLEFT值,并得到相同的结果)。 What can I do to ensure that the placement is always consistent, with the image's top-left at (or close to) the first transparent pixel? 我该怎么做才能确保位置始终一致,并且图像的top-left位于(或靠近)第一个透明像素?

If you want to only display the images, you can add them as different frames in FrameLayout. 如果只想显示图像,则可以将它们添加为FrameLayout中的不同框架。 In your xml, first declare the one you want to show as transparent pixels, then declare the one which will have transparent pixels. 在xml中,首先声明要显示为透明像素的像素,然后声明具有透明像素的像素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM