在我的 android 应用程序中,我有一辆车,用户可以从中单击并选择不同的面板。图像相对复杂(与此处粘贴的图像相反),因此很难将按钮覆盖在正确的位置。此外还有很多不同的图像。我想尝试的解决方案:
- 使用此处建议的颜色掩码检测选择了哪个面板:https ://blahti.wordpress.com/2012/06/26/images-with-clickable-areas/
- 根据选择的面板(在我的示例中为蓝色和绿色)生成蒙版。
- 根据面罩的不同,在汽车上覆盖一层红色 - 只需一个彩色滤光片就可以了。
(第一个图像代表用于确定单击哪个面板的颜色,第二个图像代表生成的掩码,最后一个图像代表“结果”)。
我遇到的唯一问题是:如何动态创建蒙版?我想过使用填充类型的方法来创建一个带有所选面板的“蒙版”的新画布。但是,我担心它可能计算量太大。有更简单的建议吗?[
更新:好的,所以我已经走了很远。正如预期的那样,蒙版的创建时间太长(小图像需要 2-4 秒)。但是,后来我发现了 RenderScripts!!我想我仍然可以让它工作。我现在唯一的小问题是:我如何传递已按下的颜色?
我当前的代码如下所示:
// create a bitmap for the mask.
ImageView img = (ImageView) findViewById (mask);
img.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(img.getDrawingCache());
// Create a tiny bitmap to store the colours of the panels that are
//'selected'
Bitmap.Config conf = Bitmap.Config.ARGB_8888; // see other conf types
Bitmap myBitmap = Bitmap.createBitmap(pickedPanels.size(), 1, conf);
int [] myInts = new int[pickedPanels.size()];
for (int i = 0; i<pickedPanels.size(); i++){
myInts[i] = pickedPanels.get(i).intValue();
}
myBitmap.setPixels(myInts, 0, myBitmap.getWidth(), 0, 0,
myBitmap.getWidth(),0);
//Run thescript and set the output
final RenderScript rs = RenderScript.create(this);
final Allocation input = Allocation.createFromBitmap(rs, bitmap,
Allocation.MipmapControl.MIPMAP_NONE,Allocation.USAGE_SCRIPT);
final Allocation output = Allocation.createTyped(rs, input.getType());
final ScriptC_singlesource script = new
ScriptC_singlesource(rs);
script.set_image(Allocation.createFromBitmap(rs, myBitmap,
Allocation.MipmapControl.MIPMAP_NONE,Allocation.USAGE_SCRIPT));
script.set_imgWidth(pickedPanels.size());
script.forEach_root(input, output);
output.copyTo(bitmap);
img.setImageBitmap(bitmap);
ImageView destim = (ImageView) findViewById (dest);
destim.setDrawingCacheEnabled(true);
destim.setImageBitmap(bitmap);
这是脚本:
#pragma version(1)
#pragma rs java_package_name(za.co.overtake)
rs_allocation image;
int imgWidth;
uchar4 RS_KERNEL root(uchar4 in, uint32_t x, uint32_t y) {
for(int col = 0; col < imgWidth; col++){
const uchar4 colour = *(const uchar4*)rsGetElementAt(image, col,0);
if (in.r == colour.r && in.g == colour.g && in.b == colour.b){
in.r = 255;
in.g = 0;
in.b = 0;
break;
} else {
in.r = 0;
in.g = 255;
in.b = 0;
rsDebug("HELLLLLP>>", colour);
}
}
return in;
}
但是,当我尝试从 myBitmap(或脚本中的图像)读取像素值时,RGB 始终为 0。
(对不起命名不好等。我一直在疯狂地试图弄清楚这一点)