Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

android - Setting color of a Paint object in custom view

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...