私のアプリの画面の1つには、6列のリストビューが表示されます。6番目の画面は、ユーザーが作成した画像またはスケッチを含むimageViewです。listView行のコードは次のとおりです。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="2dp"
android:paddingBottom="2dp" >
<TextView android:id="@+id/surveyColumn1"
android:textSize="13sp"
android:layout_width="0dip"
android:layout_weight="0.105"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/surveyColumn2"
android:textSize="13sp"
android:layout_width="0dip"
android:layout_weight="0.137"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/surveyColumn3"
android:textSize="12sp"
android:layout_width="0dip"
android:layout_weight="0.25"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/surveyColumn4"
android:textSize="13sp"
android:layout_width="0dip"
android:layout_weight="0.153"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/surveyColumn5"
android:textSize="13sp"
android:layout_width="0dip"
android:layout_weight="0.153"
android:layout_height="wrap_content"/>
<ImageView android:id="@+id/surveyColumn6"
android:layout_width="0dip"
android:layout_weight="0.202"
android:layout_height="wrap_content"
android:paddingTop="0dp"
android:paddingBottom="0dp"
android:scaleType="centerInside"
android:layout_gravity="center"
android:contentDescription="@string/pictureHeader"/>
</LinearLayout>
この設定は、表示されている画像がカメラによって撮影された垂直方向の画像である場合を除いて、うまく機能します。その場合、リストビューは行内の画像の上下に大量の空白スペースをスローします。アクティビティでは、custumViewBinderを使用して、ここから取得したコードを使用して画像をビットマップとして表示しています:http: //developer.android.com/training/displaying-bitmaps/load-bitmap.html
スペースを取り除くために私が見つけた唯一の方法は、静的な高さをimageViewに設定することです。これは、一部の行に画像がないため、私はむしろ行いません。何が起こっているのかを理解できる人は誰でも私のヒーローです。
編集:画像をビットマップとして表示するために呼び出すクラスは次のとおりです。
public class customBitmap {
public customBitmap(String pathName, int width, int height, ImageView view) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(pathName, options);
options.inSampleSize = calculateInSampleSize(options, width, height);
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(pathName, options);
//Check if bitmap was created; if so, display it in the imageView
if(bitmap == null)
Log.w("UI Thread", "Null bitmap at moto.sitesurvey.customBitmap:35");
else
view.setImageBitmap(bitmap);
}
public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight){
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if(height > reqHeight || width > reqWidth) {
if(width > height)
inSampleSize = Math.round((float)height / (float)reqHeight);
else
inSampleSize = Math.round((float)width / (float)reqWidth);
}
return inSampleSize;
}
}
アクティビティでは、幅と高さの値として200と150を渡します。私の問題は、数か月前にこのコードを最初に書いたとき、横向きの写真でのみ機能するように設計し、今ではポートレートでも機能する必要があることです。