Skip to content

Commit

Permalink
added android:gravity attribute support for floating label.
Browse files Browse the repository at this point in the history
  • Loading branch information
rengwuxian committed Dec 12, 2014
1 parent ea0f255 commit d41b92d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.text.method.TransformationMethod;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AutoCompleteTextView;
Expand Down Expand Up @@ -865,17 +866,28 @@ protected void onDraw(@NonNull Canvas canvas) {
// calculate the text color
textPaint.setColor((Integer) focusEvaluator.evaluate(focusFraction, getCurrentHintTextColor(), primaryColor));

// calculate the horizontal position
float floatingLabelWidth = textPaint.measureText(floatingLabelText.toString());
int floatingLabelStartX = getScrollX();
if ((getGravity() & Gravity.LEFT) == Gravity.LEFT) {
floatingLabelStartX += getPaddingLeft();
} else if ((getGravity() & Gravity.RIGHT) == Gravity.RIGHT) {
floatingLabelStartX += getWidth() - getPaddingRight() - floatingLabelWidth;
} else {
floatingLabelStartX += (int) (getPaddingLeft() + (getWidth() - getPaddingLeft() - getPaddingRight() - floatingLabelWidth) / 2);
}

// calculate the vertical position
int start = innerPaddingTop + floatingLabelTextSize + floatingLabelSpacing;
int floatingLabelStartY = innerPaddingTop + floatingLabelTextSize + floatingLabelSpacing;
int distance = floatingLabelSpacing;
int position = (int) (start - distance * floatingLabelFraction);
int position = (int) (floatingLabelStartY - distance * floatingLabelFraction);

// calculate the alpha
int alpha = (int) (floatingLabelFraction * 0xff * (0.74f * focusFraction + 0.26f));
textPaint.setAlpha(alpha);

// draw the floating label
canvas.drawText(floatingLabelText.toString(), getPaddingLeft() + getScrollX(), position, textPaint);
canvas.drawText(floatingLabelText.toString(), floatingLabelStartX, position, textPaint);
}

// draw the bottom ellipsis
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import android.text.method.TransformationMethod;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
Expand Down Expand Up @@ -865,17 +866,28 @@ protected void onDraw(@NonNull Canvas canvas) {
// calculate the text color
textPaint.setColor((Integer) focusEvaluator.evaluate(focusFraction, getCurrentHintTextColor(), primaryColor));

// calculate the horizontal position
float floatingLabelWidth = textPaint.measureText(floatingLabelText.toString());
int floatingLabelStartX = getScrollX();
if ((getGravity() & Gravity.LEFT) == Gravity.LEFT) {
floatingLabelStartX += getPaddingLeft();
} else if ((getGravity() & Gravity.RIGHT) == Gravity.RIGHT) {
floatingLabelStartX += getWidth() - getPaddingRight() - floatingLabelWidth;
} else {
floatingLabelStartX += (int) (getPaddingLeft() + (getWidth() - getPaddingLeft() - getPaddingRight() - floatingLabelWidth) / 2);
}

// calculate the vertical position
int start = innerPaddingTop + floatingLabelTextSize + floatingLabelSpacing;
int floatingLabelStartY = innerPaddingTop + floatingLabelTextSize + floatingLabelSpacing;
int distance = floatingLabelSpacing;
int position = (int) (start - distance * floatingLabelFraction);
int position = (int) (floatingLabelStartY - distance * floatingLabelFraction);

// calculate the alpha
int alpha = (int) (floatingLabelFraction * 0xff * (0.74f * focusFraction + 0.26f));
textPaint.setAlpha(alpha);

// draw the floating label
canvas.drawText(floatingLabelText.toString(), getPaddingLeft() + getScrollX(), position, textPaint);
canvas.drawText(floatingLabelText.toString(), floatingLabelStartX, position, textPaint);
}

// draw the bottom ellipsis
Expand Down
2 changes: 1 addition & 1 deletion sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Max Characters"
app:minCharacters="8"/>
app:maxCharacters="10"/>

<TextView
android:layout_width="match_parent"
Expand Down

0 comments on commit d41b92d

Please sign in to comment.