Skip to content

Commit 28eaa61

Browse files
committed
Bug fixed
1 parent e7bfed2 commit 28eaa61

File tree

4 files changed

+37
-38
lines changed

4 files changed

+37
-38
lines changed

app/src/main/java/uz/sadirbaev/waterBack/MainActivity.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
import android.support.v7.app.AppCompatActivity;
44
import android.os.Bundle;
55

6-
import uz.sadirbaev.waterbackground.CustomTextView;
6+
import uz.sadirbaev.waterbackground.WaterTextView;
77

88

99
public class MainActivity extends AppCompatActivity {
10-
CustomTextView textView30;
11-
CustomTextView textView16;
10+
WaterTextView textView30;
11+
WaterTextView textView16;
1212

1313

1414
@Override
@@ -19,7 +19,7 @@ protected void onCreate(Bundle savedInstanceState) {
1919
textView30 = findViewById(R.id.tv30);
2020
textView16 = findViewById(R.id.tv16);
2121

22-
String s = "Welcome to Water Background.\nThis view is like unknown view, which is used in Telegram messenger.";
22+
String s = "Welcome to Water Background.";
2323

2424
textView30.setText(s);
2525
textView16.setText(s);

app/src/main/res/layout/activity_main.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
android:layout_width="match_parent"
1414
android:layout_height="wrap_content"
1515
android:orientation="vertical">
16-
<uz.sadirbaev.waterbackground.CustomTextView
16+
<uz.sadirbaev.waterbackground.WaterTextView
1717
android:layout_width="match_parent"
1818
android:layout_height="wrap_content"
1919
android:textSize="30sp"
2020
android:id="@+id/tv30"
2121
android:layout_marginBottom="20dp"/>
22-
<uz.sadirbaev.waterbackground.CustomTextView
22+
<uz.sadirbaev.waterbackground.WaterTextView
2323
android:layout_width="match_parent"
2424
android:layout_height="wrap_content"
2525
android:textSize="16sp"

waterbackground/src/main/java/uz/sadirbaev/waterbackground/BackgroundSpannable.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,19 @@ public void updateDrawState(Canvas canvas, int PADDING){
4747

4848
int saveBound = canvas.save();
4949

50-
//计算剪裁矩形,保证绘制内容不会超出文本layout
5150
int clipLeft = tv.getPaddingLeft();
5251
int clipTop = tv.getTotalPaddingTop() + tv.getScrollY();
5352
int clipRight = canvas.getWidth() - tv.getPaddingRight();
5453
int clipBottom = clipTop + (tv.getHeight() - tv.getTotalPaddingTop() - tv.getTotalPaddingBottom());
5554

5655
canvas.clipRect(clipLeft, clipTop, clipRight, clipBottom);
5756

58-
//根据start end 获得起始行数和结束行数
5957
lineStart = layout.getLineForOffset(start);
6058
lineEnd = layout.getLineForOffset(end);
6159

6260
if(lineStart != lineEnd){
6361
lines = new Rect[(lineEnd + 1) - lineStart];
6462

65-
//计算每一行中包含当前span的矩形大小
6663
for(int i = lineStart; i <= lineEnd; i++){
6764
Rect rect = new Rect();
6865
layout.getLineBounds(i, rect);
@@ -105,7 +102,6 @@ public void updateDrawState(Canvas canvas, int PADDING){
105102

106103
int length = lineEnd - lineStart;
107104

108-
//绘制
109105
for(int i = 0; i <= lineEnd - lineStart; i++){
110106
Rect rect = lines[i];
111107

waterbackground/src/main/java/uz/sadirbaev/waterbackground/CustomTextView.java renamed to waterbackground/src/main/java/uz/sadirbaev/waterbackground/WaterTextView.java

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* Created by QarakenBacho on 13.05.2018.
1717
*/
1818

19-
public class CustomTextView extends android.support.v7.widget.AppCompatTextView {
19+
public class WaterTextView extends android.support.v7.widget.AppCompatTextView {
2020

2121
private DrawableBackgroundSpan spanBoth;
2222
private DrawableBackgroundSpan spanTop;
@@ -25,17 +25,17 @@ public class CustomTextView extends android.support.v7.widget.AppCompatTextView
2525
private static int DIFFERENCE = 120;
2626

2727

28-
public CustomTextView(Context context) {
28+
public WaterTextView(Context context) {
2929
super(context);
3030
init();
3131
}
3232

33-
public CustomTextView(Context context, @Nullable AttributeSet attrs) {
33+
public WaterTextView(Context context, @Nullable AttributeSet attrs) {
3434
super(context, attrs);
3535
init();
3636
}
3737

38-
public CustomTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
38+
public WaterTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
3939
super(context, attrs, defStyleAttr);
4040
init();
4141
}
@@ -67,42 +67,45 @@ public void onDraw(Canvas canvas){
6767

6868
int start = 0;
6969
for (int i=0; i<lines.size(); i++){
70-
BackgroundSpannable span = this.spanBoth;
70+
BackgroundSpannable span = spanBoth;
7171
int PADDING = 20;
72-
if (i==0){
73-
if (widths.get(i+1) - widths.get(i)> DIFFERENCE){
74-
if (this.spanBottom != null) {
75-
span = spanBottom;
76-
PADDING = 60;
77-
}
78-
}
79-
} else {
80-
if (i==lines.size()-1){
81-
if (widths.get(i-1)-widths.get(i)> DIFFERENCE){
82-
if (this.spanTop != null) {
83-
span = spanTop;
72+
73+
if (lines.size()>1) {
74+
if (i == 0) {
75+
if (widths.get(i + 1) - widths.get(i) > DIFFERENCE) {
76+
if (this.spanBottom != null) {
77+
span = spanBottom;
8478
PADDING = 60;
8579
}
8680
}
8781
} else {
88-
if (widths.get(i+1) - widths.get(i) > DIFFERENCE &&
89-
widths.get(i-1) - widths.get(i) > DIFFERENCE){
90-
if (this.spanNone != null) {
91-
span = this.spanNone;
92-
PADDING = 60;
93-
}
94-
} else {
95-
if (widths.get(i-1)-widths.get(i)> DIFFERENCE && widths.get(i+1) - widths.get(i)< DIFFERENCE){
82+
if (i == lines.size() - 1) {
83+
if (widths.get(i - 1) - widths.get(i) > DIFFERENCE) {
9684
if (this.spanTop != null) {
9785
span = spanTop;
9886
PADDING = 60;
9987
}
88+
}
89+
} else {
90+
if (widths.get(i + 1) - widths.get(i) > DIFFERENCE &&
91+
widths.get(i - 1) - widths.get(i) > DIFFERENCE) {
92+
if (this.spanNone != null) {
93+
span = this.spanNone;
94+
PADDING = 60;
95+
}
10096
} else {
101-
if (widths.get(i+1) - widths.get(i)> DIFFERENCE && widths.get(i-1)-widths.get(i)< DIFFERENCE){
102-
if (this.spanBottom != null) {
103-
span = spanBottom;
97+
if (widths.get(i - 1) - widths.get(i) > DIFFERENCE && widths.get(i + 1) - widths.get(i) < DIFFERENCE) {
98+
if (this.spanTop != null) {
99+
span = spanTop;
104100
PADDING = 60;
105101
}
102+
} else {
103+
if (widths.get(i + 1) - widths.get(i) > DIFFERENCE && widths.get(i - 1) - widths.get(i) < DIFFERENCE) {
104+
if (this.spanBottom != null) {
105+
span = spanBottom;
106+
PADDING = 60;
107+
}
108+
}
106109
}
107110
}
108111
}

0 commit comments

Comments
 (0)