Skip to content

Commit 60add24

Browse files
committed
ClipPagerTitleView支持测量内容区域
1 parent 6cf62cf commit 60add24

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

app/src/main/res/layout/activity_fragment_container_example_layout.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,4 @@
2626
android:layout_height="0dp"
2727
android:layout_weight="1" />
2828

29-
<net.lucode.hackware.magicindicator.MagicIndicator
30-
android:id="@+id/magic_indicator2"
31-
android:layout_width="match_parent"
32-
android:layout_height="@dimen/common_navigator_height" />
33-
3429
</LinearLayout>

magicindicator/src/main/java/net/lucode/hackware/magicindicator/buildins/commonnavigator/titles/ClipPagerTitleView.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
import android.view.View;
88

99
import net.lucode.hackware.magicindicator.buildins.UIUtil;
10-
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
10+
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IMeasurablePagerTitleView;
1111

1212
/**
1313
* 类似今日头条切换效果的指示器标题
1414
* 博客: http://hackware.lucode.net
1515
* Created by hackware on 2016/6/26.
1616
*/
17-
public class ClipPagerTitleView extends View implements IPagerTitleView {
17+
public class ClipPagerTitleView extends View implements IMeasurablePagerTitleView {
1818
private String mText;
1919
private int mTextColor;
2020
private int mClipColor;
@@ -83,7 +83,7 @@ private int measureHeight(int heightMeasureSpec) {
8383
protected void onDraw(Canvas canvas) {
8484
int x = (getWidth() - mTextBounds.width()) / 2;
8585
Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
86-
int y = (int) (getHeight() / 2 - fontMetrics.bottom / 2 - fontMetrics.top / 2);
86+
int y = (int) ((getHeight() - fontMetrics.bottom - fontMetrics.top) / 2);
8787

8888
// 画底层
8989
mPaint.setColor(mTextColor);
@@ -162,4 +162,30 @@ public void setClipColor(int clipColor) {
162162
mClipColor = clipColor;
163163
invalidate();
164164
}
165+
166+
@Override
167+
public int getContentLeft() {
168+
int contentWidth = mTextBounds.width();
169+
return getLeft() + getWidth() / 2 - contentWidth / 2;
170+
}
171+
172+
@Override
173+
public int getContentTop() {
174+
Paint.FontMetrics metrics = mPaint.getFontMetrics();
175+
float contentHeight = metrics.bottom - metrics.top;
176+
return (int) (getHeight() / 2 - contentHeight / 2);
177+
}
178+
179+
@Override
180+
public int getContentRight() {
181+
int contentWidth = mTextBounds.width();
182+
return getLeft() + getWidth() / 2 + contentWidth / 2;
183+
}
184+
185+
@Override
186+
public int getContentBottom() {
187+
Paint.FontMetrics metrics = mPaint.getFontMetrics();
188+
float contentHeight = metrics.bottom - metrics.top;
189+
return (int) (getHeight() / 2 + contentHeight / 2);
190+
}
165191
}

0 commit comments

Comments
 (0)