Reputation: 16781
I read different questions here about this topic, but I still can't find an answer. Feel free to close this question for any reason.
I have a simple Circle
class that extends View
.
The code for this class is:
public class ProgressCircle extends View {
Paint mCirclePaint;
float extRadius;
float viewWidth, viewHeight;
float centerX, centerY;
public ProgressCircle(Context context, AttributeSet attrs) {
super(context, attrs);
setWillNotDraw(false);
init();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
float xpad = (float) getPaddingLeft() + getPaddingRight();
float ypad = (float) getPaddingTop() + getPaddingBottom();
float ww = (float)w - xpad; float hh = (float)h - ypad;
extRadius = Math.min(ww, hh) / 2;
viewWidth = this.getWidth();
viewHeight = this.getHeight();
centerX = viewWidth / 2; centerY = viewHeight / 2;
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(centerX, centerY, extRadius, mCirclePaint);
canvas.drawText("ciao", 0, 0, mCirclePaint);
}
private void init() {
mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mCirclePaint.setColor(0x666666);
mCirclePaint.setStyle(Paint.Style.FILL_AND_STROKE);
}
I confirmed that every method in this classed is called when the main activity is created (through the use of some Log.d()
s). I added a <com.mypackage.Circle>
element in my main activity's LinearLayout
and after that I added a sample testing button.
What I achieved is that the button is shown while my Circle
isn't, but still the button (which in the LinearLayout
comes after the Circle
) is not the first element of the layout: that makes me think that something actually happens, but nothing gets drawn.
Upvotes: 5
Views: 2467
Reputation: 16781
It was just a silly problem: the color in mCirclePaint.setColor(0x666666)
was an invalid one. It worked with mCirclePaint.setColor(Color.RED)
or with any other color defined in the res
folder.
If you need to specify a color value, you'll have to include the transparency byte (otherwise it is not a 32bit integer that you are specifying, but a 24bit). So 0x666666 is invalid, but 0xff666666 is a valid color and will draw.
Upvotes: 3
Reputation: 7890
I notice you're not overriding View.onMeasure().
Because you're not overriding this method onsizeChanged()
might be passed size 0. You can check this by putting a breakpoint in the onSizechanged()
method or printing to Logcat.
Upvotes: 0
Reputation: 13541
After reading the documentation(http://developer.android.com/guide/topics/graphics/2d-graphics.html):
The Android framework will only call onDraw() as necessary. Each time that your application is prepared to be drawn, you must request your View be invalidated by calling invalidate(). This indicates that you'd like your View to be drawn and Android will then call your onDraw() method (though is not guaranteed that the callback will be instantaneous).
Also another thing worth checking is the dimensions you're drawing insure nothing is invalid like a height of 0 etc..
Upvotes: 0