Question

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;
}
}
Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top