如何在android中设置可绘制的颜色与背景?

时间:2013-10-17 16:53:55

标签: android android-drawable alpha-transparency

我的布局文件的片段: 带有背景layout_1

#e9e9e9
<LinearLayout android:id="@+id/layout_1"
          android:layout_width="wrap_content"
          android:background="#e9e9e9"
          android:layout_height="wrap_content"
    >
    ...
    <com.test.android.CustomView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    ...
</LinerLayout>

<LinearLayout android:id="@+id/layout_2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
    >
    ...
    <com.test.android.CustomView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    ...
</LinerLayout>

有我自定义的drawable和CustomView类片段。顶部部分覆盖底部。

public class NodeDrawable extends Drawable {

  //...

  protected ShapeDrawable shapeDrawableBottom;
  protected ShapeDrawable shapeDrawableTop;

  @Override
  public void draw(Canvas canvas) {
          Paint circlePaint = shapeDrawableBottom.getPaint();
          circlePaint.setColor(DEFAULT_COLOR_BOTTOM);//DEFAULT_COLOR_BOTTOM = 0xe2f5ff
          circlePaint.setFlags(Paint.ANTI_ALIAS_FLAG);
          shapeDrawableBottom.setBounds();
          shapeDrawableBottom.draw(canvas);

          Paint circlePaint = shapeDrawableTop.getPaint();
          circlePaint.setColor(DEFAULT_COLOR_TOP);//DEFAULT_COLOR_TOP = 0x1f8fd2
          circlePaint.setFlags(Paint.ANTI_ALIAS_FLAG);
          shapeDrawableBottom.setBounds();
          shapeDrawableTop.draw(canvas);
  }

  //...
}


public class CustomView extends View {

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        buildDrawables();
    }

    private void buildDrawables() {
        mCustomDrawable = new CustomDrawable();
    }

}

但布局背景对底部影响,而底部影响顶部影响。

如何确保它们以正确的RGB颜色显示,我应该如何处理RGBA的alpha值? 我已经尝试将颜色设置为0xe2f5ff,alpha值为1,但我认为布局背景仍然对底部颜色有影响...

1 个答案:

答案 0 :(得分:0)

circlePaint.setColor(Color.parseColor("#00FFFFFF"));

其中前2位数表示Alpha。 00到FF