Skip to content

Commit ff3d9c1

Browse files
committed
v1.1优化过度绘制
1 parent 9ea4e15 commit ff3d9c1

1 file changed

Lines changed: 31 additions & 5 deletions

File tree

app/src/main/java/com/example/liangmutian/randomtextview/view/RandomTextView.java

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
import android.graphics.Paint;
66
import android.os.Handler;
77
import android.util.AttributeSet;
8+
import android.util.Log;
89
import android.widget.TextView;
10+
import android.widget.Toast;
911

1012
import java.util.ArrayList;
1113

@@ -55,6 +57,10 @@ public class RandomTextView extends TextView {
5557
//基准线
5658
private int baseline;
5759

60+
private int measuredHeight;
61+
62+
63+
5864

5965
public RandomTextView(Context context) {
6066
super(context);
@@ -104,6 +110,8 @@ public void setPianyilian(int[] list) {
104110
pianyiliangSum = new int[list.length];
105111
overLine = new int[list.length];
106112
pianyilianglist = list;
113+
114+
107115
}
108116

109117

@@ -115,7 +123,9 @@ protected void onDraw(Canvas canvas) {
115123
super.onDraw(canvas);
116124
p = getPaint();
117125
Paint.FontMetricsInt fontMetrics = p.getFontMetricsInt();
118-
baseline = (getMeasuredHeight() - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top;
126+
measuredHeight=getMeasuredHeight();
127+
Log.d("EEEEEEE", "onDraw: "+measuredHeight);
128+
baseline = (measuredHeight - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top;
119129
float[] widths = new float[4];
120130
p.getTextWidths("9999", widths);
121131
f0 = widths[0];
@@ -151,15 +161,21 @@ private void drawNumber(Canvas canvas) {
151161
}
152162
if (overLine[j] == 0)
153163

154-
canvas.drawText(setBack(arrayListText.get(j), maxLine - i - 1) + "", 0 + f0 * j,
155-
i * baseline + pianyiliangSum[j], p);
164+
drawText(canvas,setBack(arrayListText.get(j), maxLine - i - 1) + "", 0 + f0 * j,
165+
i * baseline + pianyiliangSum[j], p);
166+
167+
//canvas.drawText(setBack(arrayListText.get(j), maxLine - i - 1) + "", 0 + f0 * j,
168+
// i * baseline + pianyiliangSum[j], p);
156169

157170
else {
158171
//定位后画一次就好啦
159172
if (overLine[j] == 1) {
160173
overLine[j]++;
161-
canvas.drawText(arrayListText.get(j) + "", 0 + f0 * j,
162-
baseline, p);
174+
175+
drawText(canvas,arrayListText.get(j) + "", 0 + f0 * j,
176+
baseline, p);
177+
// canvas.drawText(arrayListText.get(j) + "", 0 + f0 * j,
178+
// baseline, p);
163179
}
164180

165181
//break;
@@ -243,4 +259,14 @@ public void run() {
243259
};
244260

245261

262+
private void drawText(Canvas mCanvas,String text,float x,float y,Paint p){
263+
264+
if (y>=-measuredHeight&&y<=2*measuredHeight)
265+
266+
mCanvas.drawText(text + "", x,
267+
y, p);
268+
else return;
269+
}
270+
271+
246272
}

0 commit comments

Comments
 (0)