I am trying to gain konwledges on View and custom views. I read the series of articles here, and there. Then I decided to take a look on how Google coded some Views.
I retrieved the code for LabelView and I created an app that consists on a RelativLayout in which I add dynamically a (modified) LabelView. I am trying to modify the position of a label overriding setX, setY. Instead of having x, y for the upper left side, I want to have x, y the center of the view. What I did does not work (on the figure below "3" and "30" should have their centers aligned on the same y)
I added some log and that where questions raised.
- Why are measureHeight and measureWidth called several time?
- Why are getWidht, getHeight, getRight and getBottom returning 0?
Below the log (I removed what was not relevant):
... I/MainActivity: customLabel.width = 0
... I/MainActivity: customLabel.height = 0
... I/MainActivity: customLabel.getRight = 0
... I/MainActivity: customLabel.getBottom = 0
... I/MainActivity: customLabel2.width = 0
... I/MainActivity: customLabel2.height = 0
... I/MainActivity: customLabel2.getRight = 0
... I/MainActivity: customLabel2.getBottom = 0
...
... I/CustomLabelView: (lbl2) measureWidth: result = 96
... I/CustomLabelView: (lbl2) measureHeight: result = 99
... I/CustomLabelView: (lbl1) measureWidth: result = 33
... I/CustomLabelView: (lbl1) measureHeight: result = 61
... I/CustomLabelView: (lbl2) measureWidth: result = 96
... I/CustomLabelView: (lbl2) measureHeight: result = 99
... I/CustomLabelView: (lbl1) measureWidth: result = 33
... I/CustomLabelView: (lbl1) measureHeight: result = 61
...
... I/CustomLabelView: (lbl2) measureWidth: result = 96
... I/CustomLabelView: (lbl2) measureHeight: result = 99
... I/CustomLabelView: (lbl1) measureWidth: result = 33
... I/CustomLabelView: (lbl1) measureHeight: result = 61
... I/CustomLabelView: (lbl2) measureWidth: result = 96
... I/CustomLabelView: (lbl2) measureHeight: result = 99
... I/CustomLabelView: (lbl1) measureWidth: result = 33
... I/CustomLabelView: (lbl1) measureHeight: result = 61
... I/CustomLabelView: (lbl2) measureWidth: result = 96
... I/CustomLabelView: (lbl2) measureHeight: result = 99
... I/CustomLabelView: (lbl1) measureWidth: result = 33
... I/CustomLabelView: (lbl1) measureHeight: result = 61
... I/CustomLabelView: (lbl2) measureWidth: result = 96
... I/CustomLabelView: (lbl2) measureHeight: result = 99
... I/CustomLabelView: (lbl1) measureWidth: result = 33
... I/CustomLabelView: (lbl1) measureHeight: result = 61
Below MainActivity
public class MainActivity extends AppCompatActivity {
private final String TAG = getClass().getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout container = (RelativeLayout) findViewById(R.id.container);
CustomLabelView customLabel = new CustomLabelView(this, "lbl1");
customLabel.setText("3");
customLabel.setX(150);
customLabel.setY(200);
container.addView(customLabel);
Log.i(TAG, "customLabel.width = " + customLabel.getWidth());
Log.i(TAG, "customLabel.height = " + customLabel.getHeight());
Log.i(TAG, "customLabel.getRight = " + customLabel.getRight());
Log.i(TAG, "customLabel.getBottom = " + customLabel.getBottom());
CustomLabelView customLabel2 = new CustomLabelView(this, "lbl2");
customLabel2.setText("66");
customLabel2.setX(450);
customLabel2.setY(200);
customLabel2.setTextSize(80);
container.addView(customLabel2);
Log.i(TAG, "customLabel2.width = " + customLabel2.getWidth());
Log.i(TAG, "customLabel2.height = " + customLabel2.getHeight());
Log.i(TAG, "customLabel2.getRight = " + customLabel2.getRight());
Log.i(TAG, "customLabel2.getBottom = " + customLabel2.getBottom());
}
}
Below the LabelView
/*
* based on android-21/legacy/ApiDemos/src/com/example/android/apis/view/LabelView.java
*/
public class CustomLabelView extends View {
private final String TAG = getClass().getSimpleName();
private Paint mTextPaint;
private String mText;
private int mAscent;
//tag to identify instance in log
private String mTag;
/**
* Constructor. This version is only needed if you will be instantiating
* the object manually (not from a layout XML file).
*
* @param context
*/
public CustomLabelView(Context context, String tag) {
super(context);
initLabelView();
mTag = tag;
}
private final void initLabelView() {
mTextPaint = new Paint();
mTextPaint.setAntiAlias(true);
// Must manually scale the desired text size to match screen density
mTextPaint.setTextSize(16 * getResources().getDisplayMetrics().density);
mTextPaint.setColor(0xFF000000);
setPadding(3, 3, 3, 3);
}
/**
* Sets the text to display in this label
*
* @param text The text to display. This will be drawn as one line.
*/
public void setText(String text) {
mText = text;
requestLayout();
invalidate();
}
/**
* Sets the text size for this label
*
* @param size Font size
*/
public void setTextSize(int size) {
// This text size has been pre-scaled by the getDimensionPixelOffset method
mTextPaint.setTextSize(size);
requestLayout();
invalidate();
}
/**
* Sets the text color for this label.
*
* @param color ARGB value for the text
*/
public void setTextColor(int color) {
mTextPaint.setColor(color);
invalidate();
}
/**
* @see android.view.View#measure(int, int)
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(measureWidth(widthMeasureSpec),
measureHeight(heightMeasureSpec));
}
/**
* Determines the width of this view
*
* @param measureSpec A measureSpec packed into an int
* @return The width of the view, honoring constraints from measureSpec
*/
private int measureWidth(int measureSpec) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
// We were told how big to be
result = specSize;
} else {
// Measure the text
result = (int) mTextPaint.measureText(mText) + getPaddingLeft()
+ getPaddingRight();
if (specMode == MeasureSpec.AT_MOST) {
// Respect AT_MOST value if that was what is called for by measureSpec
result = Math.min(result, specSize);
}
}
Log.i(TAG, "(" + mTag + ") measureWidth: result = " + result);
return result;
}
/**
* Determines the height of this view
*
* @param measureSpec A measureSpec packed into an int
* @return The height of the view, honoring constraints from measureSpec
*/
private int measureHeight(int measureSpec) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
mAscent = (int) mTextPaint.ascent();
if (specMode == MeasureSpec.EXACTLY) {
// We were told how big to be
result = specSize;
} else {
// Measure the text (beware: ascent is a negative number)
result = (int) (-mAscent + mTextPaint.descent()) + getPaddingTop()
+ getPaddingBottom();
if (specMode == MeasureSpec.AT_MOST) {
// Respect AT_MOST value if that was what is called for by measureSpec
result = Math.min(result, specSize);
}
}
Log.i(TAG, "(" + mTag + ") measureHeight: result = " + result);
return result;
}
/**
* Render the text
*
* @see android.view.View#onDraw(android.graphics.Canvas)
*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawText(mText, getPaddingLeft(), getPaddingTop() - mAscent, mTextPaint);
}
@Override
public void setX(float x) {
float x_center = x + getWidth() / 2;
super.setX(x_center);
super.setX(x_center);
}
@Override
public void setY(float y) {
float y_center = y - getHeight() / 2;
super.setY(y_center);
}
}