在自定义视图中设置绘制对象 Paint 的颜色

8
我正在尝试制作自定义视图,并声明了如下的样式属性:

  <resources>
 <declare-styleable name="NewCircleView">
    <attr name="radius" format="integer"/>
    <attr name="circlecolor" format="color"/>
</declare-styleable>

 </resources> 

在CustomView的构造函数中,这些值可以通过以下方式获得:
    circleradius=a.getInt(R.styleable.NewCircleView_radius, 0);//global var
    circlecolor=a.getColor(R.styleable.NewCircleView_circlecolor, 0);//global var and a is the typed array

视图可通过以下方式声明xml来使用:
 <com.customviews.NewCircleView
        android:layout_below="@id/thetext"
        android:layout_width="match_parent"
        android:layout_height="fill_parent" 
        app:radius="10000"
        app:circlecolor="@color/black"<!--this is defined in colors.xml
      />

在自定义视图中,当我将绘制对象设置为:

thePaintObj.setColor(circlecolor);//circlecolor logs to an integer as expected

我不明白在xml中定义的“黑色”颜色是什么意思,但当我将颜色设置为
thePaintObj.setColor(Color.GRAY)

我在视图中获取颜色

有人可以告诉我可能做错了什么吗?

(注:如果您想让我发布更多代码,请告诉我)

编辑1:发布我的colors.xml。 看起来我的代码注释不清楚:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="red">#7f00</color>
<color name="blue">#770000ff</color>
<color name="green">#7700ff00</color>
<color name="yellow">#77ffff00</color>
<color name="black">#000000</color>
 </resources>

你需要在colors.xml文件中定义颜色。 - Raghunandan
2个回答

14

在 colors.xml 文件中

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="black_color">#000000</color>
</resources>

获取

Resources res = getResources();
int color = res.getColor(R.color.black_color);

然后将颜色设置为油漆

thePaintObj.setColor(color);

更多信息,请查看:

http://developer.android.com/guide/topics/resources/more-resources.html#Color

编辑:

MyCustomView

public class CustomView extends View{

    Paint p;
    int color ;
    public CustomView(Context context) {
        this(context, null);
    }

    public CustomView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // real work here
        TypedArray a = context.getTheme().obtainStyledAttributes(
                attrs,
                R.styleable.NewCircleView,
                0, 0
        );

        try {

         color = a.getColor(R.styleable.NewCircleView_circlecolor, 0xff000000);
        } finally {
            // release the TypedArray so that it can be reused.
            a.recycle();
        }
        init();
    }

public void init()
{
      p = new Paint();
      p.setColor(color);
}

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        if(canvas!=null)
        {
        canvas.drawCircle(100, 100,30,p );
        }
    }

}

属性文件.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <declare-styleable name="NewCircleView">
    <attr name="radius" format="integer"/>
    <attr name="circlecolor" format="color" />
</declare-styleable>
</resources>

颜色.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="black_color">#000000</color>
</resources>

xml 中的 MyCustomView

<com.example.circleview.CustomView
       xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:app="http://schemas.android.com/apk/res/com.example.circleview"
        android:id="@+id/cv"
        android:layout_width="match_parent"
        android:layout_height="fill_parent" 
        app:radius="30"
        app:circlecolor="@color/black_color"
      />

快照

enter image description here


我已经在colors.xml中声明了颜色,如我的代码注释所述,并通过xml检索它,在自定义视图中使用app:circlecolor="@color/black"来获取它,我想这与上面的代码相同。如果我错了,请告诉我。 - Rasmus
@itamecodes 请查看此链接 https://dev59.com/03A75IYBdhLWcg3wKlqM - Raghunandan
我知道这是默认颜色,在你上面的代码中就是这种情况。它只是使用了默认颜色而不是从xml中读取。 - Rasmus
@itamecodes 不,如果你想的话,我可以用不同的颜色重新检查。我使用的是黑色,而默认的是 0xff000000。我会更改为其他颜色并进行检查,但我认为这不是问题。 - Raghunandan
好的,根据你说的接受了。但我仍然不明白我的代码中出现的错误,这恰好与你的相同。 - Rasmus
显示剩余3条评论

0
如果我理解正确的话,常量0x000000会产生透明黑色,因为没有指定Alpha分量。Alpha值是四字节颜色值的第一个字节。不透明(实心)黑色的常量为0xff000000。换句话说,颜色0x000000(与0x00000000相同),将使你完全透明地绘制。红色的常量看起来也有问题,会导致绿色透明。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接