Skip to content

Commit

Permalink
ClipPagerTitleView支持测量内容区域
Browse files Browse the repository at this point in the history
  • Loading branch information
hackware1993 committed Nov 22, 2016
1 parent 6cf62cf commit 60add24
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,4 @@
android:layout_height="0dp"
android:layout_weight="1" />

<net.lucode.hackware.magicindicator.MagicIndicator
android:id="@+id/magic_indicator2"
android:layout_width="match_parent"
android:layout_height="@dimen/common_navigator_height" />

</LinearLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
import android.view.View;

import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IMeasurablePagerTitleView;

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

// 画底层
mPaint.setColor(mTextColor);
Expand Down Expand Up @@ -162,4 +162,30 @@ public void setClipColor(int clipColor) {
mClipColor = clipColor;
invalidate();
}

@Override
public int getContentLeft() {
int contentWidth = mTextBounds.width();
return getLeft() + getWidth() / 2 - contentWidth / 2;
}

@Override
public int getContentTop() {
Paint.FontMetrics metrics = mPaint.getFontMetrics();
float contentHeight = metrics.bottom - metrics.top;
return (int) (getHeight() / 2 - contentHeight / 2);
}

@Override
public int getContentRight() {
int contentWidth = mTextBounds.width();
return getLeft() + getWidth() / 2 + contentWidth / 2;
}

@Override
public int getContentBottom() {
Paint.FontMetrics metrics = mPaint.getFontMetrics();
float contentHeight = metrics.bottom - metrics.top;
return (int) (getHeight() / 2 + contentHeight / 2);
}
}

0 comments on commit 60add24

Please sign in to comment.