sábado, 18 de setembro de 2010

Drawing In an Android View

When I was drawing in a view using onDraw method, I noted that if you are drawing a line, the y scale starts at 1px but if you are drawing a text, the scale starts at height of text, i.e. The text have 10px height, you have to specify 10px at y position to display text at top of view. I think the android are drawing the text above the pixel scale. If you specify to draw the text at 1px, it will be drawed starting at -10px. Drawing a bitmap works fine, the top left of the image is x: 0px y: 0px

You can test a lot of scales by putting the above code at onDraw event of a view:

private void printScales(Canvas canvas){
Rect clipBounds = canvas.getClipBounds();
String clip_bounds = Integer.toString(clipBounds.right)+"x"+Integer.toString(clipBounds.bottom)+
" - "+Integer.toString(clipBounds.left) + "x" + Integer.toString(clipBounds.top);
Log.i("clip bounds", clip_bounds);
String canvas_string = Integer.toString(canvas.getWidth())+"x"+Integer.toString(canvas.getHeight());
Log.i("canvas", canvas_string);
String this_string = Integer.toString(getWidth())+"x"+Integer.toString(getHeight())+
" - "+Integer.toString(getLeft()) + "x" + Integer.toString(getTop());
Log.i("this", this_string);
String measured = Integer.toString(getMeasuredWidth())+"x"+Integer.toString(getMeasuredHeight());
Log.i("measured",measured);
String padding = Integer.toString(getPaddingLeft())+
"x"+Integer.toString(getPaddingTop())+
"x"+Integer.toString(getPaddingRight())+
"x"+Integer.toString(getPaddingBottom());
Log.i("padding", padding);
getDrawingRect(clipBounds);
String drawing_rect = Integer.toString(clipBounds.right)+"x"+Integer.toString(clipBounds.bottom)+
" - "+Integer.toString(clipBounds.left) + Integer.toString(clipBounds.top);
Log.i("drawing rect", drawing_rect);
Log.i("finished","here");
}

Nenhum comentário:

Postar um comentário