Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复在有延时操作的情况下setLetters方法设置后的文字无法在竖直方向上居中的问题 #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class QuickSideBarView extends View {
private int mHeight;
private float mItemHeight;
private float mItemStartY;
//计算位置
private Rect mRect = new Rect();

public QuickSideBarView(Context context) {
this(context, null);
Expand Down Expand Up @@ -69,6 +71,7 @@ private void init(Context context, AttributeSet attrs) {

protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mItemStartY = (mHeight - mLetters.size()*mItemHeight)/2;
for (int i = 0; i < mLetters.size(); i++) {
mPaint.setColor(mTextColor);

Expand All @@ -81,12 +84,9 @@ protected void onDraw(Canvas canvas) {
mPaint.setTextSize(mTextSizeChoose);
}


//计算位置
Rect rect = new Rect();
mPaint.getTextBounds(mLetters.get(i), 0, mLetters.get(i).length(), rect);
float xPos = (int) ((mWidth - rect.width()) * 0.5);
float yPos = mItemHeight * i + (int) ((mItemHeight - rect.height()) * 0.5) + mItemStartY;
mPaint.getTextBounds(mLetters.get(i), 0, mLetters.get(i).length(), mRect);
float xPos = (int) ((mWidth - mRect.width()) * 0.5);
float yPos = mItemHeight * i + (int) ((mItemHeight - mRect.height()) * 0.5) + mItemStartY;


canvas.drawText(mLetters.get(i), xPos, yPos, mPaint);
Expand All @@ -100,7 +100,6 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mHeight = getMeasuredHeight();
mWidth = getMeasuredWidth();
mItemStartY = (mHeight - mLetters.size()*mItemHeight)/2;
}

@Override
Expand Down