Skip to content

Commit

Permalink
优化宽度显示
Browse files Browse the repository at this point in the history
  • Loading branch information
maning0303 committed Jul 6, 2020
1 parent 5af988a commit bfabf52
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 21 deletions.
8 changes: 8 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 22 additions & 4 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#dadada"
android:gravity="center_horizontal"
android:orientation="vertical">

<TextView
Expand All @@ -16,11 +17,9 @@

<com.maning.pswedittextlibrary.MNPasswordEditText
android:id="@+id/mPswEditText"
android:layout_width="match_parent"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="10dp"
android:layout_marginRight="16dp"
android:focusableInTouchMode="true"
android:inputType="number"
android:maxLength="6"
Expand All @@ -41,7 +40,7 @@
app:mnPsw_border_selected_color="#FF0000"
app:mnPsw_border_width="2dp"
app:mnPsw_cover_text=""
app:mnPsw_item_margin="10dp"
app:mnPsw_item_margin="20dp"
app:mnPsw_mode="Text"
app:mnPsw_style="StyleUnderLine"
app:mnPsw_text_color="#393939" />
Expand Down Expand Up @@ -101,5 +100,24 @@
app:mnPsw_style="StyleOneself"
app:mnPsw_text_color="#393939" />

<com.maning.pswedittextlibrary.MNPasswordEditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:focusableInTouchMode="true"
android:inputType="number"
android:maxLength="9"
android:textSize="20sp"
app:mnPsw_border_color="#FF9800"
app:mnPsw_border_radius="4dp"
app:mnPsw_cover_circle_radius="10dp"
app:mnPsw_cover_circle_color="#8BC34A"
app:mnPsw_border_selected_color="#8BC34A"
app:mnPsw_border_width="2dp"
app:mnPsw_item_margin="10dp"
app:mnPsw_mode="Circle"
app:mnPsw_style="StyleOneself"
app:mnPsw_text_color="#393939" />


</LinearLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ protected void onDraw(Canvas canvas) {
//获取宽高
int measuredWidth = getMeasuredWidth();
float itemH = getMeasuredHeight();
//方形框
float margin = itemMargin;
float itemW = (measuredWidth - margin * (maxLength - 1)) / maxLength;

//判断类型
if (editTextStyle == 1) {
Expand All @@ -226,7 +229,8 @@ protected void onDraw(Canvas canvas) {
//Android系统小于API16,使用setBackgroundDrawable
setBackgroundDrawable(gradientDrawable);
}
float itemW = measuredWidth / maxLength;
margin = 0;
itemW = measuredWidth / maxLength;
//画线
for (int i = 1; i < maxLength; i++) {
float startX = itemW * i;
Expand All @@ -236,9 +240,6 @@ protected void onDraw(Canvas canvas) {
canvas.drawLine(startX, startY, stopX, stopY, mPaintLine);
}
} else if (editTextStyle == 2) {
//方形框
float margin = itemMargin;
float itemW = measuredWidth / maxLength - margin;
gradientDrawable.setStroke((int) borderWidth, borderColor);
gradientDrawable.setCornerRadius(borderRadius);
gradientDrawable.setColor(backgroundColor);
Expand All @@ -250,7 +251,7 @@ protected void onDraw(Canvas canvas) {
}
//画每个Item背景
for (int i = 0; i < maxLength; i++) {
float left = itemW * i + margin / 2 + margin * i;
float left = itemW * i + margin * i;
float top = 0;
if (bitmapSelected == null) {
canvas.drawBitmap(bitmap, left, top, mPaintLine);
Expand All @@ -264,7 +265,6 @@ protected void onDraw(Canvas canvas) {
}
}
} else if (editTextStyle == 3) {
float itemW = (measuredWidth - itemMargin * (maxLength - 1) - itemMargin) / maxLength;
//下划线格式
for (int i = 0; i < maxLength; i++) {
if (borderSelectedColor != 0) {
Expand All @@ -277,7 +277,7 @@ protected void onDraw(Canvas canvas) {
} else {
mPaintLine.setColor(borderColor);
}
float startX = itemW * i + itemMargin * i + itemMargin / 2;
float startX = itemW * i + itemMargin * i;
float startY = itemH - borderWidth;
float stopX = startX + itemW;
float stopY = startY;
Expand All @@ -289,39 +289,42 @@ protected void onDraw(Canvas canvas) {
String currentText = getText().toString();
for (int i = 0; i < maxLength; i++) {
if (!TextUtils.isEmpty(currentText) && i < currentText.length()) {
//<!--密码框输入的模式:4.明文,3.文字,2.图片,1.圆形-->
//<!--密码框输入的模式:1.圆形,2.图片,3.文字,4.明文-->
if (inputMode == 1) {
//圆点半径
float circleRadius = measuredWidth / maxLength * 0.5f * 0.3f;
float circleRadius = itemW * 0.5f * 0.5f;
if (circleRadius > itemH / 2f) {
circleRadius = itemH * 0.5f * 0.5f;
}
if (coverCirclrRadius > 0) {
circleRadius = coverCirclrRadius;
}
float startX = (measuredWidth / maxLength) / 2.0f + measuredWidth / maxLength * i;
float startX = (itemW / 2f) + itemW * i + margin * i;
float startY = (itemH) / 2.0f;
mPaintText.setColor(coverCirclrColor);
canvas.drawCircle(startX, startY, circleRadius, mPaintText);
} else if (inputMode == 2) {
float picW = measuredWidth / maxLength * 0.5f;
float picW = itemW * 0.5f;
if (coverBitmapWidth > 0) {
picW = coverBitmapWidth;
}
float startX = (measuredWidth / maxLength - picW) / 2.0f + measuredWidth / maxLength * i;
float startY = (itemH - picW) / 2.0f;
float startX = (itemW - picW) / 2f + itemW * i + margin * i;
float startY = (itemH - picW) / 2f;
Bitmap bitmap = Bitmap.createScaledBitmap(coverBitmap, (int) picW, (int) picW, true);
canvas.drawBitmap(bitmap, startX, startY, mPaintText);
} else if (inputMode == 3) {
float fontWidth = getFontWidth(mPaintText, coverText);
float fontHeight = getFontHeight(mPaintText, coverText);
float startX = (measuredWidth / maxLength - fontWidth) / 2.0f + measuredWidth / maxLength * i;
float startY = (itemH + fontHeight) / 2.0f - 6;
float startX = (itemW - fontWidth) / 2f + itemW * i + margin * i;
float startY = (itemH + fontHeight) / 2f - 6;
mPaintText.setColor(textColor);
canvas.drawText(coverText, startX, startY, mPaintText);
} else {
String StrPosition = String.valueOf(currentText.charAt(i));
float fontWidth = getFontWidth(mPaintText, StrPosition);
float fontHeight = getFontHeight(mPaintText, StrPosition);
float startX = (measuredWidth / maxLength - fontWidth) / 2.0f + measuredWidth / maxLength * i;
float startY = (itemH + fontHeight) / 2.0f;
float startX = (itemW - fontWidth) / 2f + itemW * i + margin * i;
float startY = (itemH + fontHeight) / 2f;
mPaintText.setColor(textColor);
canvas.drawText(StrPosition, startX, startY, mPaintText);
}
Expand Down

0 comments on commit bfabf52

Please sign in to comment.