Skip to content

Commit 8ac00dc

Browse files
author
hailang.jiang
committedJun 20, 2017
Summary: add onMeasure process
1 parent 3412fe4 commit 8ac00dc

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed
 

‎.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎app/src/main/java/rich/ivan/monthbill/MonthBillView.java

+24
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import android.graphics.Path;
99
import android.support.annotation.Nullable;
1010
import android.util.AttributeSet;
11+
import android.util.Log;
1112
import android.view.View;
1213

1314
/**
@@ -16,6 +17,7 @@
1617
*/
1718

1819
public class MonthBillView extends View {
20+
private static final String TAG = "MonthBillView";
1921

2022
private int[] mMonthArray;
2123
private double[] mBillValues;
@@ -141,6 +143,28 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
141143
mBillRange = (mBillRange == 0 ? 1 : mBillRange);
142144
}
143145

146+
@Override
147+
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
148+
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
149+
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
150+
151+
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
152+
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
153+
154+
Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
155+
float fontHeight = fontMetrics.descent - fontMetrics.ascent;
156+
157+
mWidth = widthSize;
158+
159+
if (heightMode == MeasureSpec.EXACTLY) {
160+
mHeight = heightSize;
161+
} else {
162+
mHeight = (int) (4 * fontHeight + 4 * fontHeight + getPaddingTop() + getPaddingBottom());
163+
}
164+
165+
setMeasuredDimension(mWidth, mHeight);
166+
}
167+
144168
@Override
145169
protected void onDraw(Canvas canvas) {
146170
super.onDraw(canvas);

‎app/src/main/res/layout/activity_main.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<rich.ivan.monthbill.MonthBillView
1111
android:id="@+id/month_bill_view"
1212
android:layout_width="match_parent"
13-
android:layout_height="150dp"/>
13+
android:layout_height="wrap_content"/>
1414

1515
<TextView
1616
android:layout_width="match_parent"

0 commit comments

Comments
 (0)