2

I'm trying to scale the text continuously by changing text size. And I implement my own view to drawText on canvas every time the text size changed. But the procedure of invalidate and redraw canvas became very slow which lead to some animation became slow too.

The specific os version is 4.0.3(The application works well on the same kind device with os version 4.1.1)

The hardware acceleration is on.(works well when it's off)

I wrote a simple project to reproduce this issue. The custom view:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class MyTextView extends View {

private Paint mPaint;
private float mTextSize = 80f;

public MyTextView(Context context) {
    this(context, null);
}

public MyTextView(Context context, AttributeSet attr) {
    super(context, attr);

    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setTextSize(mTextSize);
}

@Override
public void onDraw(Canvas canvas) {
    canvas.save();
    mPaint.setTextSize(mTextSize);
    canvas.drawText("Test test test", 200, 200, mPaint);
    canvas.restore();
    mTextSize += 1f;
}
}

The Activity:

import android.os.Bundle;
import android.view.MotionEvent;
import android.app.Activity;

public class MainActivity extends Activity {
private MyTextView mTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mTextView = (MyTextView)findViewById(R.id.text);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    mTextView.invalidate();
    return true;
}
}
Michelle
  • 1,097
  • 8
  • 18
  • I report this to Google and got their response. They just said they have made a lot of text rendering performance improvements in 4.1 and 4.2. But I want to know how can we deal with os version below 4.1 with hardware acceleration. It's very important for a product to provide good user experience for every devices we support. – Michelle Jan 30 '13 at 01:48
  • http://code.google.com/p/android/issues/detail?id=43457&can=1&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars#makechanges – Michelle Jan 30 '13 at 07:22
  • Did you find a solution? I have the same case, and it's still slow on 4.2. – WonderCsabo Jan 06 '14 at 20:46

1 Answers1

0

Finally I used StaticLayout.draw(Canvas) instead call Canvas.drawText

Michelle
  • 1,097
  • 8
  • 18