长期读者,第一次海报。我对Android开发很陌生,在使用AsyncTask将ImageViews(包含Bitmaps)插入LinearLayout时,无法显示图像。这都是在我拥有的Activity的onCreate()方法中触发的。
ImageViews(+ Bitmaps)肯定会通过AsyncTask添加到我的LinearLayout父级。但是,当我启动Activity时,图像无法正常显示。有时会显示一两个图像(3个以上),有时不会显示。在摆弄UI之后,所有图像都可以正常显示,例如通过调出和隐藏键盘。我怀疑LinearLayout和/或ImageViews可能没有调整大小来包含和显示所有新的子节点,但我在尝试尝试“LOCATION1”和“LOCATION2”的地方尝试了很多invalidate()和requestLayout()的组合触发重绘。
在onCreate()之后和每个AsyncTask完成之后,是否有人帮助确保所有图像都正确显示?谢谢一堆。我会尝试简洁地使用我的代码片段......
这是我的布局XML。我将我的ImageViews添加到ID为“horizontal”的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;
}
}
答案 0 :(得分:0)
每个“启用识别”活动都应该以自定义视图作为根,以嵌入语音识别控件和功能。事实证明,除非您通过自定义View的synchronize()函数指示它,否则此自定义View不会始终刷新其子项。简而言之,我在AsyncTask完成后调用了View的synchronize()方法,运行了onPostExecute(),并将Bitmap添加到了活动中。
答案 1 :(得分:-1)
在AsynTask中,您处理UI更改。您无法在后台工作中对UI进行更改。使用runOnUIThread总是在UI线程上进行计算。更好地取决于您的易用性。查看here