使用ScaleDrawable()时语法错误

时间:2012-04-30 02:40:17

标签: java android android-layout android-widget custom-controls

使用ScaleDrawable()时出现语法错误。我一定做错了什么。 有什么想法吗?

2个错误我无法解决。

这就是我现在所拥有的:

protected class testbuttonBackgroundDrawable extends LayerDrawable {
/** my aim - the image used as background for the custom view is increased in
 *size by 25percent and then a yellow color filter is attached to it.
 *This yellow image then has the original background image layered on top of it
 *to create the effect of the image having a glow around it
 *I want to use this later on for when the custom button has focus
 */     
    ColorFilter colourFilter = new LightingColorFilter(Color.YELLOW, 1);

    ScaleDrawable resizedImage = new ScaleDrawable(background, 0, 1.25f, 1.25f);*

*这里的错误是 - 令牌上的语法错误“;”,{此标记后的预期

    nD = resizedImage.getDrawable();
    nD.setColorFilter(colourFilter);

    Drawable[] aD = new Drawable[2];
    aD[0] = nD;
    aD[1] = background;
    LayerDrawable _highlightedDrawable = new LayerDrawable(aD);

//This will make the background image fade if the button is set to disabled
    protected int _disabledAlpha = 100;
/**This is another scale drawable, this time used to shrink the background image of
 *the custom button when it is pressed
 */
    protected ScaleDrawable _pressedDrawable = new ScaleDrawable(background, 0, 0.75f, 0.75f);*

*这里的错误是 - “语法错误,插入”}“以完成阻止”

    public testbuttonBackgroundDrawable(Drawable d) {
          super(new Drawable[] { d });
    }

    @Override
    protected boolean onStateChange(int[] states) {
      boolean enabled = false;
      boolean highlighted = false;
      boolean pressed = false;

      for (int state : states) {
        if (state == android.R.attr.state_enabled)
            enabled = true;
        else if (state == android.R.attr.state_selected)
            highlighted = true;
        else if (state == android.R.attr.state_pressed)
            pressed = true;
      }

      mutate();
      if (enabled && highlighted) {
        setBackgroundDrawable(_highlightedDrawable); // buttons need transparent backgrounds
      } else if (!enabled) {
        setColorFilter(null);
        setAlpha(_disabledAlpha);
      } else if (enabled && pressed){
        setBackgroundDrawable(_pressedDrawable);
      } else {
        setColorFilter(null);
      }

      invalidateSelf();

      return super.onStateChange(states);
    }

}

}

任何帮助修复错误都将受到赞赏。

我认为我没有正确使用ScaleDrawable构造函数。

我该如何正确地做到这一点?

如果那不是问题,那么我们将不胜感激。

1 个答案:

答案 0 :(得分:0)

修好了!

问题在于我在图层可绘制类的错误部分中对我的按钮状态进行了计算。这是固定的:

protected class CustomImageButtonBackgroundDrawable extends LayerDrawable { 

    protected Drawable lowerlayer;
    protected Drawable _highlightedDrawable;

    protected int _disabledAlpha = 100;
    protected Drawable _pressedDrawable;


    public CustomImageButtonBackgroundDrawable(Drawable d) {
          super(new Drawable[] { d });
    }

    @Override
    protected boolean onStateChange(int[] states) {
      boolean enabled = false;
      boolean highlighted = false;
      boolean pressed = false;

      for (int state : states) {
        if (state == android.R.attr.state_enabled)
            enabled = true;
        else if (state == android.R.attr.state_selected)
            highlighted = true;
        else if (state == android.R.attr.state_pressed)
            pressed = true;
      }

      mutate();
      if (enabled && highlighted) {
        ColorFilter colourFilter = new LightingColorFilter(Color.YELLOW, 1);
        ScaleDrawable resizedImage = new ScaleDrawable(background, 0, 1.25f, 1.25f);

        lowerlayer = resizedImage.getDrawable();
        lowerlayer.setColorFilter(colourFilter);

        Drawable[] aD = new Drawable[2];
        aD[0] = lowerlayer;
        aD[1] = background;
        LayerDrawable _highlightedDrawable = new LayerDrawable(aD);

        setBackgroundDrawable(_highlightedDrawable); // buttons need transparent backgrounds

      } else if (!enabled) {
        setColorFilter(null);
        setAlpha(_disabledAlpha);

      } else if (enabled && pressed){
        ScaleDrawable smaller = new ScaleDrawable(background, 0, 0.75f, 0.75f);

        setBackgroundDrawable(smaller.getDrawable());

      } else {
        setColorFilter(null);
      }

      invalidateSelf();

      return super.onStateChange(states);
    }

}