【问题标题】:ImageView: Overlaying multiple images with alphaImageView:用 alpha 覆盖多个图像
【发布时间】:2014-07-02 20:37:04
【问题描述】:

我希望能够将多个图像加载到ImageView 并根据需要打开/关闭它们。看起来很简单,但给我带来了很多麻烦,因为我发现图像在更改后没有更新,即使我使用invalidate()

在自定义ImageView内:

private Drawable[] drawables = new Drawable[6];
private Context _context;
private LayerDrawable layers;

private void ourClassDef(Context context)
{
    _context=context;

    Resources r = _context.getResources();

    // TODO Auto-generated constructor stub
    drawables[0] = r.getDrawable(R.drawable.gun_blank);
    drawables[1] = r.getDrawable(R.drawable.gun_base_green);
    drawables[2] = r.getDrawable(R.drawable.gun_base_red);
    drawables[3] = r.getDrawable(R.drawable.gun_coil_green);
    drawables[4] = r.getDrawable(R.drawable.gun_coil_red);
    drawables[5] = r.getDrawable(R.drawable.gun_trigger);

    layers = new LayerDrawable(drawables);        
    setImageDrawable(layers);       
}

public GunIconControl(Context context) {
    super(context);
    ourClassDef(context);
}

...

然后,打开\关闭每一层

layers.getDrawable(5).setAlpha(on?1:0);

-或-

layers.getDrawable(5).setVisible(on,true);

有没有更好的方法来控制图层?

【问题讨论】:

    标签: android imageview layer


    【解决方案1】:

    尝试使用layerDrawable.setLayerInset

    编辑:附加OP解决方案

     if(on) {
          layers.getDrawable(5).setBounds(normal_bounds); 
     }
     else{
          layers.getDrawable(5).setBounds(out_of_bounds);
     }
    

    【讨论】:

    • 不错的解决方案。我最终得到: if(on) layers.getDrawable(5).setBounds(normal_bounds);否则 layers.getDrawable(5).setBounds(out_of_bounds);这很好用。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-27
    • 1970-01-01
    • 2011-02-13
    • 1970-01-01
    • 1970-01-01
    • 2018-06-02
    相关资源
    最近更新 更多