Skip to content

Commit

Permalink
Fix text center display
Browse files Browse the repository at this point in the history
  • Loading branch information
whilu committed Jan 12, 2016
1 parent b4af647 commit d52df4c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 23 deletions.
Expand Up @@ -99,7 +99,7 @@ public class TagContainerLayout extends ViewGroup {
private int[] mViewPos;

/** View theme(default PURE_CYAN)*/
private int mTheme = 1;
private int mTheme = ColorFactory.PURE_CYAN;

/** Default interval(dp)*/
private static final float DEFAULT_INTERVAL = 5;
Expand Down Expand Up @@ -191,6 +191,12 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
}
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mRectF.set(0, 0, w, h);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int availableW = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
Expand All @@ -216,8 +222,6 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mRectF.set(canvas.getClipBounds().left, canvas.getClipBounds().top,
canvas.getClipBounds().right, canvas.getClipBounds().bottom);

mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(mBackgroundColor);
Expand Down
36 changes: 20 additions & 16 deletions androidtagview/src/main/java/co/lujun/androidtagview/TagView.java
Expand Up @@ -3,7 +3,6 @@
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.v4.widget.ViewDragHelper;
import android.text.TextUtils;
Expand Down Expand Up @@ -59,14 +58,14 @@ public class TagView extends View {

private RectF mRectF;

private Rect mTextBound;

private String mAbstractText, mOriginText;

private boolean isUp, isMoved, isExecLongClick;

private int mLastX, mLastY;

private float fontH, fontW;

private Runnable mLongClickHandle = new Runnable() {
@Override
public void run() {
Expand All @@ -86,34 +85,37 @@ public TagView(Context context, String text){
private void init(String text){
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mRectF = new RectF();
mTextBound = new Rect();
mOriginText = text;
mOriginText = text == null ? "" : text;
}

private void onDealText(){
if(!TextUtils.isEmpty(mOriginText)) {
mAbstractText = mOriginText.length() <= mTagMaxLength ? mOriginText
: mOriginText.substring(0, mTagMaxLength - 3) + "...";
}else {
mAbstractText = "";
}
mPaint.setTextSize(mTextSize);
mPaint.getTextBounds(mAbstractText, 0, mAbstractText.length(), mTextBound);
final Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
fontH = fontMetrics.descent - fontMetrics.ascent;
fontW = mPaint.measureText(mAbstractText);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(mHorizontalPadding * 2 + mTextBound.width(),
mVerticalPadding * 2 + mTextBound.height());
setMeasuredDimension(mHorizontalPadding * 2 + (int)fontW,
mVerticalPadding * 2 + (int)fontH);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mRectF.set(canvas.getClipBounds().left + mBorderWidth,
canvas.getClipBounds().top + mBorderWidth,
canvas.getClipBounds().right - mBorderWidth,
canvas.getClipBounds().bottom - mBorderWidth);
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mRectF.set(mBorderWidth, mBorderWidth, w - mBorderWidth, h - mBorderWidth);
}

@Override
protected void onDraw(Canvas canvas) {
mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(mBackgroundColor);
canvas.drawRoundRect(mRectF, mBorderRadius, mBorderRadius, mPaint);
Expand All @@ -125,8 +127,10 @@ protected void onDraw(Canvas canvas) {

mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(mTextColor);
canvas.drawText(mAbstractText, getWidth() / 2 - mTextBound.width() / 2,
getHeight() / 2 + mTextBound.height() / 2, mPaint);

// Set the distance between baseline and descent as 5px
canvas.drawText(mAbstractText, getWidth() / 2 - fontW / 2,
getHeight() / 2 + fontH / 2 - 5, mPaint);
}

@Override
Expand Down
Binary file modified sample/sample-release.apk
Binary file not shown.
5 changes: 1 addition & 4 deletions sample/src/main/java/co/lujun/sample/MainActivity.java
Expand Up @@ -29,15 +29,13 @@ protected void onCreate(Bundle savedInstanceState) {

List<String> list1 = new ArrayList<String>();
list1.add("Java");
list1.add("C/C++");
list1.add("C++");
list1.add("Python");
list1.add("Swift");
list1.add("你好,这是一个TAG。你好,这是一个TAG。你好,这是一个TAG。你好,这是一个TAG。");
list1.add("PHP");
list1.add("Python");
list1.add("JavaScript");
list1.add("Html");
list1.add("Hello, this is a TAG example.");
list1.add("Welcome to use AndroidTagView!");

List<String> list2 = new ArrayList<String>();
Expand All @@ -50,7 +48,6 @@ protected void onCreate(Bundle savedInstanceState) {
list2.add("UK");
list2.add("Germany");
list2.add("Niger");
list2.add("Singapore");
list2.add("Poland");
list2.add("Norway");
list2.add("Uruguay");
Expand Down

0 comments on commit d52df4c

Please sign in to comment.