My purpose is to make an Bitmap image which shows user supplied text, and the image can later be saved into cache.
After calling TextView.setText() and TextView.invalidate(), the TextView is not being updated as I expected. It still draws previous texts on Canvas.
@Override
public void onClick(View v) {
// tv is a TextView and et is an EditText.
tv.setText(et.getText().toString());
tv.invalidate();
// if measure() isn't called, getMeasuredWidth() returns previous value.tv.measure(0, 0);
int screenWidth = (int) tv.getMeasuredWidth();
int screenHeight = (int) tv.getMeasuredHeight();
if (screenHeight * screenWidth != 0) {
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
screenWidth, screenHeight);
Bitmap testB;
testB = Bitmap.createBitmap(screenWidth, screenHeight, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(testB);
tv.draw(c);
// tv draws previous String on Canvas c.
iv = (ImageView) findViewById(R.id.image);
iv.setLayoutParams(layoutParams);
iv.setBackgroundColor(Color.RED);
iv.setImageBitmap(testB);
}
}
The document says
public void invalidate ()Invalidate the whole view. If the view is visible,
onDraw(android.graphics.Canvas)will be called at some point in the
future. This must be called from a UI thread. To call from a non-UI
thread, callpostInvalidate().
Does it mean I should override the onDraw() method?
I found the solution… (may be a solution)
It is needed to add this line before calling draw():
I think the width and height aren’t updated yet, because getWidth() returns previous value.