0
我想問一問如何改善這些代碼的性能。 基本上它所做的是繪製一個BitmapDrawable並將其用作ImageView的drawable,然後將其放置在TableView的TableRow上。BitmapDrawable性能
private void drawTableData() {
TableLayout table = new TableLayout(this);
BitmapDrawable bm;
TableRow row = new TableRow(this);
String rowData = "A1;A2;A3;A4;A5;A6;A7;A8;A9;A10;A11;A12;";
String[] tmpRowData = rowData.split("\\;");
for (String str : tmpRowData) {
ImageView img = new ImageView(this);
bm = writeOnDrawable(R.drawable.seat_check_icon, str);
img.setImageDrawable(bm);
img.setLayoutParams(new TableRow.LayoutParams(20, 20));
row.addView(img);
}
table.addView(row, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
public BitmapDrawable writeOnDrawable(int drawableId, String text) {
Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId).copy(Bitmap.Config.ARGB_8888, true);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setTypeface(Typeface.DEFAULT_BOLD);
paint.setStyle(Style.FILL);
paint.setColor(txtColor);
paint.setTextSize((float) 14);
Rect bounds = new Rect();
paint.getTextBounds(text, 0, text.length(), bounds);
int height = bounds.bottom + bounds.height();
int width = bounds.left + bounds.width();
float canvasWidth = bm.getWidth();
float canvasHeight = bm.getHeight();
float startPositionX = (canvasWidth - width)/2;
float startPositionY = (canvasHeight + height)/2;
Canvas canvas = new Canvas(bm);
canvas.drawText(text, startPositionX, startPositionY, paint);
return new BitmapDrawable(this.getResources(), bm);
}
任何建議將非常感激。 在此先感謝。
好的,謝謝你的建議。我會試試看。 – jmetran