Setting color of a Paint object in custom view

一世执手 提交于 2019-12-17 16:29:15

问题


I am trying to make a custom view and have declared the styled attributes like the below:-

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

 </resources> 

in the constructor of the customview , these values are obtained like below:-

    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

The view is used by declaring the xml as below:-

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

In the custom view when i set the paint object as :-

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

I dont get the color-"black" defined in the xml

however when i set the color as

thePaintObj.setColor(Color.GRAY)

I get the color in the view

Can someone tell me what would I be doing wrong ?

(N.B:-If you want me to post more code , please let me know)

EDIT1:- Posting my colors.xml. Looks like it is not clear in my code comments:-

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

回答1:


In colors.xml

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

To retrieve

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

Then set color to paint

thePaintObj.setColor(color);

More info @

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

Edit:

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>

MyCustomView in xml

<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"
      />

Snap Shot




回答2:


If I understand correctly, the constant 0x000000 results in transparent black since there is no Alpha component specified. The Alpha value is the first byte of a four byte color value. The constant for opaque (solid) black is 0xff000000. In other words, the color 0x000000, which is the same as 0x00000000, results in you drawing completely transparently. The constant for Red also looks wrong, resulting in transparent green.



来源:https://stackoverflow.com/questions/18681956/setting-color-of-a-paint-object-in-custom-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!