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

时间:2013-09-08 08:43:14

标签: android android-custom-view

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

  <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)

我在视图中获得了颜色

有人能告诉我,我会做错什么吗?

(N.B: - 如果您希望我发布更多代码,请告诉我)

EDIT1: - 发布我的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>

2 个答案:

答案 0 :(得分:12)

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

}

attrs.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>

colors.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

答案 1 :(得分:0)

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