Reputation: 125
I need your help once again.
In my activity I have a TextView where I'm loading html content:
textview.setText(Html.fromHtml(body, p, null));
Since there might be some images in this content, I'm using
URLImageParser p = new URLImageParser(textview, this);
to fetch them and add dynamically. The thing is that if image is smaller than width of the screen it automatically align left. So I figured to center the image I'll create wider bitmap and attach my image in a specific position, based on width of the display and the image:
public Drawable fetchDrawable(String urlString)
{
try
{
InputStream is = fetch(urlString);
Drawable drawable;
Bitmap bitmap = BitmapFactory.decodeStream(is);
widthBM=bitmap.getWidth();
heightBM=bitmap.getHeight();
display.getSize(size);
int width = size.x;
int w = (width - widthBM)/2+widthBM;
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap b = Bitmap.createBitmap(w, heightBM, conf);
Canvas comboCanvas = new Canvas(b);
comboCanvas.drawBitmap(bitmap, (width - widthBM)/2, 0, null);
drawable = new BitmapDrawable(getResources(), b);
drawable.setBounds(0, 0, 0+w, 0+heightBM);
return drawable;
}
catch (Exception e)
{
return null;
}
}
And on start of the activity it works perfect - this is when starting in portrait:
and this is when starting in landscape:
The problem starts on the rotation of the device. I thought since I'm not using anything like
android:configChanges="keyboardHidden|orientation|screenSize"
in this activity and I'm not overriding onConfigurationChanged(Configuration newConfig), the whole activity should be reloaded, onCreate should be called again and size of my drawables should be recalculated based on new screen width. But, when switching from portrait to landscape, instead of getting what's on second picture, I'm getting something like this:
When I'm checking size of the drawable it is correct (let's say 295x80px in portrait and 455x80px in landscape, where screen is 480x800px (or 800x480px when in landscape, obviously) and image 120x80px). What am I missing?
public class URLImageParser implements ImageGetter
{
Context c;
TextView container;
public URLImageParser(TextView t, Context c)
{
this.c = c;
this.container = t;
}
public Drawable getDrawable(String source)
{
URLDrawable urlDrawable = new URLDrawable();
ImageGetterAsyncTask asyncTask = new ImageGetterAsyncTask(urlDrawable);
asyncTask.execute(source);
return urlDrawable;
}
public class ImageGetterAsyncTask extends AsyncTask<String, Void, Drawable>
{
URLDrawable urlDrawable;
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
public ImageGetterAsyncTask(URLDrawable d)
{
this.urlDrawable = d;
}
@Override
protected Drawable doInBackground(String... params)
{
String source = params[0];
return fetchDrawable(source);
}
@Override
protected void onPostExecute(Drawable result)
{
urlDrawable.setBounds(0, 0, 0+(width - widthBM)/2 + widthBM, 0+heightBM);
urlDrawable.drawable = result;
URLImageParser.this.container.invalidate();
URLImageParser.this.container.setHeight((URLImageParser.this.container.getHeight() + heightBM));
URLImageParser.this.container.setEllipsize(null);
}
}
}
Upvotes: 0
Views: 958
Reputation: 125
Fixed it. Turns out it has nothing to do with drawables. The correct answer is: don't be an idiot. And if for some reason you decide to be one, like me, don't put
android:freezesText="true"
in your textview properties.
Upvotes: 1