我想獲得視圖和自定義視圖上的konwledges。我讀了一系列文章here和there。然後我決定看看Google如何編碼一些視圖。Android查看內部 - 自定義查看定位
我檢索了LabelView的代碼,並創建了一個應用程序,該應用程序由一個RelativLayout組成,該應用程序動態添加(修改的)LabelView。我試圖修改標籤的位置,覆蓋setX,setY。而不是有x,y爲左上方,我想有x,y視圖的中心。我所做的不工作(下面「3」和「30」這個數字應該有自己的中心在同一Ÿ對齊)
我加了一些日誌和地方提出的問題。
- 爲什麼measureHeight和measureWidth調用幾次?
- 爲什麼getWidht,getHeight,getRight和getBottom返回0?
在日誌下面(我刪除的內容是不相關):
... 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
下面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());
}
}
下面的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);
}
}
Thanks @Bartek!你對第一個問題的回答是在12點50分(https://www.youtube.com/watch?v=NYtB6mlu7vA)確認的,據說「Viewgroups會在eacf的子視圖中調用Measure」 –