Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 位图可绘制性能_Android - Fatal编程技术网

Android 位图可绘制性能

Android 位图可绘制性能,android,Android,我想问一下,如何提高这些代码的性能。 基本上,它所做的是绘制一个BitmapDrawable,并将其用作ImageView的drawable,然后将其放置在TableView的TableRow上 private void drawTableData() { TableLayout table = new TableLayout(this); BitmapDrawable bm; TableRow row = new TableRow(this); String

我想问一下,如何提高这些代码的性能。 基本上,它所做的是绘制一个BitmapDrawable,并将其用作ImageView的drawable,然后将其放置在TableView的TableRow上

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);
}
如有任何建议,将不胜感激。 提前感谢。

这里有三个(加一个)建议:

  • 仅创建和初始化一次
    Paint
    对象,而不是每次绘制
  • 预加载并保留绘制的位图(如果始终是相同的)
  • 您可以直接创建自定义的
    Drawable
    类,并在它的
    draw()
    方法中进行绘制

  • 激进(可能不适用于您的需要):在您的布局中,使用位图制作一个
    ImageView
    ,并在其上方放置一个
    TextView
    ,以显示文本