很长时间的读者,第一次海报。我对Android开发非常陌生,在使用AsyncTask将ImageViews (包含位图)插入到LinearLayout中时,很难获得要显示的图像。这都是在我所拥有的活动的onCreate()方法中触发的。
ImageViews (+位图)肯定是通过AsyncTask添加到我的LinearLayout父级的。然而,当我开始我的活动时,图像并没有正确地显示出来。有时会显示一两个图像(在3+之外),有时则没有显示。在我摆弄UI之后,所有的图像都会正常显示,比如打开和隐藏键盘。我怀疑LinearLayout和/或ImageViews可能没有调整大小以包含和显示所有新的子元素,但是我尝试了在标记为"LOCATION1“和"LOCATION2”的位置上使用多个无效()和requestLayout()的组合,以尝试触发重绘。
有谁能帮助确保在onCreate()之后和每个AsyncTask完成之后正确地显示所有图像吗?谢谢一堆。我会尽量精简我的代码片段..。
这是我的布局XML。我将我的ImageViews添加到id为“水平”的LinearLayout中:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
... />
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
这是我的一些onCreate()代码。在这里,我为我想要显示的每一个图像创建一个AsyncTask。
protected void onCreate(Bundle savedInstanceState) {
...
LinearLayout horizontal = (LinearLayout) findViewById(R.id.horizontal);
...
//an array of absolute file paths to JPGs in storage
ArrayList<String> images = report.getImageMain();
PhotoBitmapTask task = null; //extension of AsyncTask
for (int i = 0; i < images.size(); i++) {
task = new PhotoBitmapTask(getApplicationContext(), horizontal, images);
task.execute(i);
//LOCATION1
}
...
}这是我对AsyncTask的扩展。
//a bunch of imports
public class PhotoBitmapTask extends AsyncTask<Integer, Void, Bitmap> {
private Context context;
private WeakReference<ViewGroup> parent;
private ArrayList<String> images;
private int data;
public PhotoBitmapTask(Context context, ViewGroup parent, ArrayList<String> images) {
super();
this.context = context;
this.parent = new WeakReference<ViewGroup>(parent);
this.images = images;
this.data = 0;
}
@Override
protected Bitmap doInBackground(Integer... params) {
data = params[0];
return getBitmapFromFile(images.get(params[0]), 600, 600);
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
if (context != null && parent != null && result != null) {
ViewGroup viewGroup = parent.get();
if (viewGroup != null) {
ImageView imageView = PhotoBitmapTask.getImageView(context);
imageView.setImageBitmap(result);
viewGroup.addView(imageView);
//LOCATION2
}
}
}
public static Bitmap getBitmapFromFile(String filePath, int maxHeight,
int maxWidth) {
// check dimensions for sample size
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
// calculate sample size
options.inSampleSize = getSampleSize(options, maxHeight, maxWidth);
// decode Bitmap with sample size
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filePath, options);
}
public static int getSampleSize(BitmapFactory.Options options,
int maxHeight, int maxWidth) {
final int height = options.outHeight;
final int width = options.outWidth;
int sampleSize = 1;
if (height > maxHeight || width > maxWidth) {
// calculate ratios of given height/width to max height/width
final int heightRatio = Math.round((float) height / (float) maxHeight);
final int widthRatio = Math.round((float) width / (float) maxWidth);
// select smallest ratio as the sample size
if (heightRatio > widthRatio)
return heightRatio;
else
return widthRatio;
} else
return sampleSize;
}
public int getData() {
return this.data;
}
public static ImageView getImageView(Context context) {
// width and height
final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
// margins
params.setMargins(20, 20, 20, 20);
final ImageView view = new ImageView(context);
view.setLayoutParams(params);
// scale type
view.setScaleType(ScaleType.CENTER);
return view;
}
}发布于 2013-10-25 01:49:45
在头痛和心痛之后,我找到了答案。我之前没有提到它,也没有专门搜索它,因为我不认为它是相关的,但我正在使用SDK的360 SpeechAnywhere开发者SDK在我的应用程序中包括语音识别。希望我没有违反SDK许可证的条款,因为我说:
每个“识别启用”活动都应该有一个自定义视图作为根,以便嵌入语音识别控件和功能。事实证明,这个自定义视图并不总是刷新它的子视图,除非您通过定制视图的synchronize()函数指示它。长话短说,当我的AsyncTask完成后,我调用了View的synchronize()方法,运行了onPostExecute(),并将位图添加到活动中。
发布于 2013-10-18 06:49:39
在AsynTask中,您的UI changes.you无法在后台进行UI更改,work.Use runOnUIThread总是在UI thread.It上进行计算,更好地依赖于您在这里上的use.look易用性。
https://stackoverflow.com/questions/19442809
复制相似问题