Android imageview with layerdrawable



我的程序具有带有imageView(s)的子观点的gridview。我想通过在原始图像上显示半透明层和tick标记来单击特定的ImageView时向用户提供反馈。我使用的是带有3层的Layerdable。我通过编写一个小应用程序来测试代码,并且它运行得很好。但是它在GridView内部不起作用。

public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
    baseimage = ((ImageView)v).getDrawable(); //This is the original image thumbnail
layers = new Drawable[3];
layers[0] = baseimage;
layers[1] = white_color;// white_color is a ColorDrawable with colour = 2147483647
layers[2] = tick_mark;
    if(!selected[(int)id])//selected is a boolean array to store if an imageview is selected or not
    {
Toast.makeText(this,"selected",Toast.LENGTH_SHORT).show();
//layers[1].setVisible(true,true);
//layers[2].setVisible(true,true);
translucent = new LayerDrawable(layers);
((ImageView)v).setImageDrawable(translucent);
v.invalidate();
selected[(int)id] = true;
    }
    else
    {
    //((ImageView)v).setImageDrawable(baseimage);
    //layers[1].setVisible(false,true);
//layers[2].setVisible(false,true);
translucent.invalidateSelf();
v.invalidate();
((ImageView)v).setImageDrawable(null);
((ImageView)v).setImageDrawable(baseimage);
Toast.makeText(this,((ImageView)v).getDrawable().toString(),Toast.LENGTH_SHORT).show();
selected[(int)id] = false;
    }
}

当我运行代码时,第一次正确显示了layerdrable,当我再次单击它时,imageView不会显示任何更改。如果我第三次单击它,则将新的layerDrabable添加到已经存在的layerdrawable中,每次都会在上面添加白色半透明层。

请帮助我。

我看到了你的问题。

您第一次输入onItemClick()时,baseimage=v.getDrawable()只是一个图像。但是,第二次调用onItemClick()时,baseimage=v.getDrawable()可以代表图像 白色 tick覆盖。因此,即使if(!selected[(int)id])案例执行,您仍然会看到覆盖层。

最新更新