ページに次々と表示する必要がある画像のリストと、その下にあるすべてのテキストがあります。そのテキストに関連する部分(一部のTextView)を追加したファイルのxmlにLinearLayoutがあります。画像の URL を取得するためにサービスを呼び出す必要があるため、コードでその LinearLayout に画像を追加します。このコードを使用して、その URL を使用して画像を取得し、レイアウトに追加します (AsyncTask 実装の onPostExecute 部分)。
/**
* Download image using image url and show it in ImageView
*/
private class DownloadImageTask extends AsyncTask<Void, Void, Bitmap> {
public DownloadImageTask() {
super();
}
protected Bitmap doInBackground(Void... arg0) {
String urlDisplay = mDocumentUrls.get(mDocumentIdx);
mDocumentIdx = mDocumentIdx + 1;
Bitmap bmp = null;
try {
InputStream in = new java.net.URL(urlDisplay).openStream();
bmp = BitmapFactory.decodeStream(in);
} catch (Exception e) {
e.printStackTrace();
}
return bmp;
}
protected void onPostExecute(Bitmap result) {
// We add a new ImageView to the LinearLayout of the page and set it
// source to the downloaded image
ImageView newImageView = new ImageView(getActivity());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(convertDpToPx(20), convertDpToPx(5), convertDpToPx(20), 0);
newImageView.setImageBitmap(result);
mDocumentDetailsLayout.addView(newImageView, params);
//Next image
if(mDocumentIdx < mDocumentUrls.size())
new DownloadImageTask().execute();
}
}
そして、これがページのxml(LinearLayoutに関係する部分)
<ScrollView
android:id="@+id/details_form"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/splashScreenGray
android:visibility="gone" >
<LinearLayout
android:id="@+id/details_form_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/splashScreenGray"
android:clickable="false"
android:orientation="vertical" ...
ページに画像が表示されますが、2 つの問題があります。携帯電話が縦向きモードのときに持っていない画像 (画像は必要ありません) - prtsc1.png と prtsc2.png を比較してください。
私はすべてを試しましたが、何が間違っているのかわかりません。