【发布时间】:2014-01-23 18:36:13
【问题描述】:
我再次需要你的帮助。
在我的活动中,我有一个 TextView,我在其中加载 html 内容:
textview.setText(Html.fromHtml(body, p, null));
由于此内容中可能有一些图像,我正在使用
URLImageParser p = new URLImageParser(textview, this);
获取它们并动态添加。问题是,如果图像小于屏幕宽度,它会自动向左对齐。所以我想将图像居中我将创建更宽的位图并根据显示器和图像的宽度将我的图像附加到特定位置:
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;
}
}
并且在活动开始时它完美运行 - 这是在纵向开始时:
这是在横向开始时:
问题始于设备的旋转。我想,因为我没有使用类似的东西
android:configChanges="keyboardHidden|orientation|screenSize"
在这个活动中,我没有覆盖 onConfigurationChanged(Configuration newConfig),应该重新加载整个活动,应该再次调用 onCreate,并且应该根据新的屏幕宽度重新计算我的可绘制对象的大小。但是,当从纵向切换到横向时,我得到的不是第二张图片上的内容,而是这样的:
当我检查可绘制对象的大小时,它是正确的(假设纵向为 295x80 像素,横向为 455x80 像素,其中屏幕为 480x800 像素(或横向时为 800x480 像素,显然)和图像 120x80 像素)。我错过了什么?
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);
}
}
}
【问题讨论】:
-
比较
widthBM和heightBM(如果我理解正确的话,它们是告诉你要放置图像的“坐标”),当你从风景开始时,你得到的方向变化。如果这些不相等,您可能必须在另一个事件中获取坐标。告诉我,我曾经做过类似的事情。但是,我认为为每个方向提供不同的布局会容易得多。 -
感谢您的回答。 WidthBM 和 heightBM 是位图的宽度和高度。该位图的左上角坐标为(width - widthBM)/2, 0(“内部”位图b),它们在设备旋转后重新计算并且是正确的。你说的布局是什么意思?在这种情况下,在这个活动使用的布局中只有一个 TextView。
-
也许你想以编程方式来做,所以我说的不适合你。否则,您可以为纵向模式指定一种布局,为横向模式指定另一种布局:developer.android.com/training/basics/supporting-devices/…。那么,您说这些变量在两种情况下都相同吗?如果是这样,我会尝试实现它。
-
好吧,如果我能处理这个单一的场景,我应该没问题,所以显示其他分辨率等,目前不是问题。我有一个 120x80 的位图。我将它放置在 x 坐标 (480-120)/2 = 180px 处。旋转后重新计算 x 坐标,等于 (800-120)/2 = 340px。这是在 comboCanvas.drawBitmap(...) 中使用的值,但由于某种原因,应用程序仍然使用前一个值(从肖像模式,而不是风景)。