问题
I have code that adds a variable number of images into ImageViews from an ArrayList. These then get added as views to a LinearLayout, which is in a HorizontalScrollingLayout. My code is as follows:
My Activity class -
LinearLayout gallery;
...
gallery = (LinearLayout) findViewById(R.id.viewvehiclegallery);
...
gallery.removeAllViews();
for (Photo _photo : vehicle.getPhotoArrayList()) {
ImageView newImage = new ImageView(getApplicationContext());
newImage.setBackground(new BitmapDrawable(getResources(), _photo.getImage()));
newImage.setScaleType(ImageView.ScaleType.FIT_XY);
newImage.setAdjustViewBounds(true);
//newImage.setMaxWidth(40);
gallery.addView(newImage);
}
My XML -
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tool_bar"
android:id="@+id/viewvehiclehorizontal"
android:layout_marginTop="5dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp">
<LinearLayout
android:id="@+id/viewvehiclegallery"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:orientation="horizontal"
/>
</HorizontalScrollView>
My default photo is a drawable PNG, but that gets replaced as soon as the user starts taking photos with their camera. The default PNG gets stretched very very wide, and then the added photos are small and do not fill the entire space allocated to the LinearLayout/HorizontalScrollingView (sorry, not sure which I need to resize).
How do I have the photos keep their ratio as well as a reasonable size?
EDIT:
My code is now the following. Don't think it is fixed.
gallery.removeAllViews();
for (Photo _photo : vehicle.getPhotoArrayList()) {
ImageView newImage = new ImageView(getApplicationContext());
newImage.setImageBitmap(_photo.getImage());
newImage.setAdjustViewBounds(true);
newImage.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
//newImage.setBackground(new BitmapDrawable(getResources(), _photo.getImage()));
gallery.addView(newImage);
}
来源:https://stackoverflow.com/questions/33986903/keeping-the-aspect-ratio-of-imageviews-in-a-linearlayout-in-a-horizontalscrollin