简体   繁体   English

如何在Android中设置背景可绘制的绘画颜色?

[英]how to set the drawable paint color with background in android?

there is the snippet of my layout file: layout_1 with backgound #e9e9e9 我的布局文件的代码段是: 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>

There is my custom drawable and CustomView classes snippets. 有我的自定义drawable和CustomView类片段。 The top one partly cover the bottom one. 顶部部分覆盖底部。

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();
    }

}

But the layout background have an effect on the bottom one and the bottom one effect the top one either. 但是,布局背景会影响底部的背景,而底部的背景则会影响顶部的背景。

How can i ensure them displayed in correct RGB color, what i should do with the RGBA's alpha value? 我如何确保它们以正确的RGB颜色显示,该如何处理RGBA的alpha值? I have tried set the color to 0xe2f5ff with alpha value 1, but i think the layout backgound still have an effect on the bottom color... 我试过将颜色设置为0且ealpha值为1,但是我认为布局背景对底色仍然有影响...

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

where first 2 digit is for Alpha. 前2位数字代表Alpha。 00 to FF 00至FF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将drawable set的颜色改为android:background? - How to change color of drawable set as android:background? Android如何设置状态栏背景为渐变色或drawable? - How to set status bar background as gradient color or a drawable in Android? 在Android中以编程方式设置Drawable的背景颜色? - Programmatically set the Background color of a Drawable in Android? Android:无法将背景可绘制颜色设置为percentRelativeLayout - Android: unable to set background drawable color to percentRelativeLayout 如何在android中绘制背景画布相同的颜色 - how to paint background canvas same color in android 使用 Android 8 中的 Shape Drawable 设置按钮的背景颜色和边框颜色 - Set background color and border color of Button using Shape Drawable in Android 8 设置背景颜色并在android中使用drawable作为背景 - set background color and also use drawable as background in android 如何在Android上设置自定义可绘制背景? - how to set custom drawable background on android? 如何将可绘制集的背景/颜色更改为视图的背景? - How to change background/color of the drawable set as background of a view? 如何在View的背景上设置drawable? Android的 - How to set drawable on background of View? Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM