加载图像时 TableRow 与其他 TableRow 重叠 - Android
TableRow Overlaps other TableRows when Images are Loaded - Android
我正在 android 应用程序上将大量书面内容转换为易于阅读的页面。此内容主要由文字和图片组成,还包括 tables.
内容下载为 html,所以我使用 Html.fromHtml() 方法和 URLImageParser class 来显示内容。
我在我的应用程序中使用了 TableLayout。我的代码是这样工作的:
- 如果内容中没有 tables,只需将其全部放在一行中并将这一行添加到 tablelayout
- 如果有一个table,获取table之前的所有内容,将其放在一行中并将该行添加到table布局中。现在,对于 table 中的每一行,获取其内容并将其添加为 table 布局中的一行。然后将table之后的所有内容添加到一行中,并添加到table布局中。
- (多个 table 以此类推)
这段代码工作正常,我 运行 遇到的问题是 table 之前的内容是否包含图像。在这种情况下,初始内容在图像加载完成之前作为一行放置在 TableLayout 中。图像加载完成后,此 table 行会调整大小以包含图像。但是 table 布局中的下一行不会相应地向下移动。这意味着下面的行与第一行 table 重叠。
理想情况下,我希望下面的行在上面的行更改大小时自动调整,这可能吗?我一直在尝试在线查找,但找不到类似的东西。
我一直在尝试不同的方法 - 我想在将行添加到 table 布局之前检测 URLImageParser 何时完成加载图像(我接受这将意味着应用程序将暂停一段时间在打开页面时),但我也无法让它工作。出于某种原因,我的 URLImageParser AsyncTask 的 onPostExecute() 方法从未被调用。
有谁知道是否 possible/how 使 table 行在上面的行更改其大小时重新调整自己?
或者,任何人都可以看到我尝试检测加载完成时出错的地方。我只是使用一个布尔标志 finishedExecuting 但它从未设置为 true。我在下面包含了 class。
感谢您的宝贵时间,如有任何回复,我们将不胜感激。
URLImageParser.java
public class URLImageParser implements ImageGetter {
Context c;
TextView container;
Activity a;
int intrinsicHeight;
int intrinsicWidth;
boolean finishedExecuting;
public URLImageParser(TextView t, Context c, Activity a) {
this.c = c;
this.container = t;
this.a = a;
intrinsicHeight =0;
intrinsicWidth = 0;
finishedExecuting = false;
}
public Drawable getDrawable(String source) {
System.out.println("getDrawable() was called");
URLDrawable urlDrawable = new URLDrawable();
ImageGetterAsyncTask asyncTask =
new ImageGetterAsyncTask( urlDrawable);
asyncTask.execute(source);
return urlDrawable;
}
public class ImageGetterAsyncTask extends AsyncTask<String, Void, Drawable> {
URLDrawable urlDrawable;
boolean usesImageNotFoundDrawable = false;
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) {
System.out.println("ENTERED ON POST EXECUTE");
//check to see if an image was found (if not could
//be due to no internet)
if(result ==null){
usesImageNotFoundDrawable = true;
//the drawable wasn't found so use the image not found
//png
Drawable imageNotFound = a.getResources().getDrawable(R.drawable.image_not_found);
result = imageNotFound;
} else {
usesImageNotFoundDrawable = false;
}
intrinsicHeight = result.getIntrinsicHeight();
intrinsicWidth = result.getIntrinsicWidth();
DisplayMetrics dm = new DisplayMetrics();
((WindowManager) c.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels -50;
int height = width * intrinsicHeight / intrinsicWidth;
result.setBounds(0, 0, 0 + width, 0
+ height);
urlDrawable.setBounds(0, 0, 0+width, 0+height);
urlDrawable.drawable = result;
URLImageParser.this.container.invalidate();
if(usesImageNotFoundDrawable == true){
URLImageParser.this.container.setHeight((URLImageParser.this.container.getHeight()
+ height*4));
} else {
URLImageParser.this.container.setHeight((URLImageParser.this.container.getHeight()
+ height));
}
// Pre ICS
URLImageParser.this.container.setEllipsize(null);
setFinishedExecuting(true);
System.out.println("END OF ON POST EXECUTE");
}
public Drawable fetchDrawable(String urlString) {
try {
InputStream is = fetch(urlString);
Drawable drawable = Drawable.createFromStream(is, "src");
return drawable;
} catch (Exception e) {
return null;
}
}
private InputStream fetch(String urlString) throws MalformedURLException, IOException {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(urlString);
HttpResponse response = httpClient.execute(request);
return response.getEntity().getContent();
}
}
public boolean getFinishedExecuting(){
return finishedExecuting;
}
public void setFinishedExecuting(boolean bool){
finishedExecuting = bool;
}
}
现在想通了。这不是 TableRow 的问题。这是我的 URLImageParser class 的问题。
我删除了
if(usesImageNotFoundDrawable == true){
URLImageParser.this.container.setHeight((URLImageParser.this.container.getHeight()
+ height*4));
} else {
URLImageParser.this.container.setHeight((URLImageParser.this.container.getHeight()
+ height));
}
并将其替换为
container.setText(container.getText());
现在可以正确检测图像的高度,并且所有内容都可以正确显示。
我正在 android 应用程序上将大量书面内容转换为易于阅读的页面。此内容主要由文字和图片组成,还包括 tables.
内容下载为 html,所以我使用 Html.fromHtml() 方法和 URLImageParser class 来显示内容。
我在我的应用程序中使用了 TableLayout。我的代码是这样工作的:
- 如果内容中没有 tables,只需将其全部放在一行中并将这一行添加到 tablelayout
- 如果有一个table,获取table之前的所有内容,将其放在一行中并将该行添加到table布局中。现在,对于 table 中的每一行,获取其内容并将其添加为 table 布局中的一行。然后将table之后的所有内容添加到一行中,并添加到table布局中。
- (多个 table 以此类推)
这段代码工作正常,我 运行 遇到的问题是 table 之前的内容是否包含图像。在这种情况下,初始内容在图像加载完成之前作为一行放置在 TableLayout 中。图像加载完成后,此 table 行会调整大小以包含图像。但是 table 布局中的下一行不会相应地向下移动。这意味着下面的行与第一行 table 重叠。
理想情况下,我希望下面的行在上面的行更改大小时自动调整,这可能吗?我一直在尝试在线查找,但找不到类似的东西。
我一直在尝试不同的方法 - 我想在将行添加到 table 布局之前检测 URLImageParser 何时完成加载图像(我接受这将意味着应用程序将暂停一段时间在打开页面时),但我也无法让它工作。出于某种原因,我的 URLImageParser AsyncTask 的 onPostExecute() 方法从未被调用。
有谁知道是否 possible/how 使 table 行在上面的行更改其大小时重新调整自己? 或者,任何人都可以看到我尝试检测加载完成时出错的地方。我只是使用一个布尔标志 finishedExecuting 但它从未设置为 true。我在下面包含了 class。
感谢您的宝贵时间,如有任何回复,我们将不胜感激。
URLImageParser.java
public class URLImageParser implements ImageGetter {
Context c;
TextView container;
Activity a;
int intrinsicHeight;
int intrinsicWidth;
boolean finishedExecuting;
public URLImageParser(TextView t, Context c, Activity a) {
this.c = c;
this.container = t;
this.a = a;
intrinsicHeight =0;
intrinsicWidth = 0;
finishedExecuting = false;
}
public Drawable getDrawable(String source) {
System.out.println("getDrawable() was called");
URLDrawable urlDrawable = new URLDrawable();
ImageGetterAsyncTask asyncTask =
new ImageGetterAsyncTask( urlDrawable);
asyncTask.execute(source);
return urlDrawable;
}
public class ImageGetterAsyncTask extends AsyncTask<String, Void, Drawable> {
URLDrawable urlDrawable;
boolean usesImageNotFoundDrawable = false;
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) {
System.out.println("ENTERED ON POST EXECUTE");
//check to see if an image was found (if not could
//be due to no internet)
if(result ==null){
usesImageNotFoundDrawable = true;
//the drawable wasn't found so use the image not found
//png
Drawable imageNotFound = a.getResources().getDrawable(R.drawable.image_not_found);
result = imageNotFound;
} else {
usesImageNotFoundDrawable = false;
}
intrinsicHeight = result.getIntrinsicHeight();
intrinsicWidth = result.getIntrinsicWidth();
DisplayMetrics dm = new DisplayMetrics();
((WindowManager) c.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels -50;
int height = width * intrinsicHeight / intrinsicWidth;
result.setBounds(0, 0, 0 + width, 0
+ height);
urlDrawable.setBounds(0, 0, 0+width, 0+height);
urlDrawable.drawable = result;
URLImageParser.this.container.invalidate();
if(usesImageNotFoundDrawable == true){
URLImageParser.this.container.setHeight((URLImageParser.this.container.getHeight()
+ height*4));
} else {
URLImageParser.this.container.setHeight((URLImageParser.this.container.getHeight()
+ height));
}
// Pre ICS
URLImageParser.this.container.setEllipsize(null);
setFinishedExecuting(true);
System.out.println("END OF ON POST EXECUTE");
}
public Drawable fetchDrawable(String urlString) {
try {
InputStream is = fetch(urlString);
Drawable drawable = Drawable.createFromStream(is, "src");
return drawable;
} catch (Exception e) {
return null;
}
}
private InputStream fetch(String urlString) throws MalformedURLException, IOException {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(urlString);
HttpResponse response = httpClient.execute(request);
return response.getEntity().getContent();
}
}
public boolean getFinishedExecuting(){
return finishedExecuting;
}
public void setFinishedExecuting(boolean bool){
finishedExecuting = bool;
}
}
现在想通了。这不是 TableRow 的问题。这是我的 URLImageParser class 的问题。
我删除了
if(usesImageNotFoundDrawable == true){
URLImageParser.this.container.setHeight((URLImageParser.this.container.getHeight()
+ height*4));
} else {
URLImageParser.this.container.setHeight((URLImageParser.this.container.getHeight()
+ height));
}
并将其替换为
container.setText(container.getText());
现在可以正确检测图像的高度,并且所有内容都可以正确显示。