Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android drawCircle with different colour border

Is it possible to draw a circle on a canvas in Android with a different colour border using only one drawCircle method?

I have noticed the PaintStyle of FILL_AND_STROKE but cant seem to have different colours for both the fill and the border.

I really don't want to have to call two drawCircle methods.

like image 457
skyfoot Avatar asked Oct 23 '10 14:10

skyfoot


3 Answers

Definition of Paint.Style says:

Paint.Style The Style specifies if the primitive being drawn is filled, 
stroked, or both (in the same color). 

So it seems it can't be done in one go.

If you do this a lot you can create a static helper method that does two calls to draw bordered circle.

Or you could create a custom android.graphics.drawable.shapes.Shape object and override its draw(..) method.

like image 191
Peter Knego Avatar answered Nov 19 '22 12:11

Peter Knego


Thanks Peter Knego!

if in case any one in need of Shape xml here it is

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
 android:shape="oval">
    <solid android:color="#FFFFFF"/>
    <stroke android:width="5dp" android:color="#FFFF00" />
    <size
        android:width="50dp"
        android:height="50dp" />

    <corners android:radius="20dp" />
</shape> 
like image 36
Jana Avatar answered Nov 19 '22 14:11

Jana


Try making a class and creating the circle with borders by making two one smaller than the other then use the class as your shape instead of the predefined shapes

like image 36
ross Avatar answered Nov 19 '22 13:11

ross