android - ScrollView 内的 ImageView 不会调整大小以填满屏幕(保持纵横比)

标签 android layout resize imageview scrollview

我遇到了一个我似乎无法解决的非常奇怪的问题。

我似乎无法为 ScrollView 内的 ImageView 调整位图的大小。 我测试了相同的布局(但没有滚动),图像按应有的大小调整了大小。

我发现其他人也有这种问题,对于大多数人来说,使用 android:fillViewport="true" 就足够了,但对我来说不起作用。

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"      
      android:fillViewport="true"
      android:background="@drawable/bg_app" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="@drawable/custom_white_background"
            android:padding="5dp">

            <ImageView
                android:id="@+id/full_image_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:adjustViewBounds="true"
                android:scaleType="fitCenter"
                android:src="@drawable/bg_app"
                android:contentDescription="@string/otl_img_description"
                android:layout_margin="5dp"
                android:padding="5dp" />            

            <View
                android:id="@+id/divider2"
                android:layout_width="match_parent"
                android:layout_height="3dp"
                android:layout_below="@+id/full_image_view"
                android:layout_centerHorizontal="true"
                android:layout_marginLeft="5dp"
                android:paddingRight="5dp"
                android:layout_marginRight="5dp"
                android:paddingLeft="5dp"
                android:alpha="0.3"
                android:background="@color/black_50"
                android:fadingEdge="horizontal" />

            <TextView
                android:id="@+id/onthelist_animal_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                android:layout_below="@+id/full_image_view"
                android:layout_margin="5dp"
                android:layout_toLeftOf="@+id/onthelist_animal_date"
                android:gravity="left|center"
                android:padding="5dp"
                android:text="@string/otl_animal_name"
                android:textColor="@color/blue"
                android:textStyle="bold"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/onthelist_animal_date"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/full_image_view"
                android:layout_centerHorizontal="true"
                android:layout_margin="5dp"
                android:gravity="center"
                android:padding="5dp"
                android:text="@string/otl_animal_date"
                android:textColor="#a00000"
                android:textStyle="bold"
                android:textSize="18sp" />

            <ImageButton
                android:id="@+id/onthelist_share"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/onthelist_animal_date"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"
                android:layout_alignTop="@+id/onthelist_animal_date"
                android:layout_toRightOf="@+id/onthelist_animal_date"
                android:adjustViewBounds="true"
                android:paddingRight="5dp"
                android:contentDescription="@string/otl_animal_share"
                android:scaleType="fitEnd"
                android:src="@android:drawable/ic_menu_share"
                android:background="@android:color/transparent" />          

        </RelativeLayout>

        <Button
            android:id="@+id/onthelist_save"
            style="@style/ButtonText"
            android:layout_height="wrap_content"
            android:background="@layout/custom_red_button"
            android:text="@string/otl_save"
            android:layout_margin="5dp"
            android:padding="5dp" /> 

        <TextView
            android:id="@+id/onthelist_animal_description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="@drawable/custom_white_background"
            android:padding="5dp"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/black"
            android:text="@string/otl_animal_description"
            android:linksClickable="true" />

    </LinearLayout>
</ScrollView>

我正在为 ImageView full_image_view 设置图像,如下所示:

        imageView = (ImageView) findViewById(R.id.full_image_view);
    Log.i("OnTheList", "itemID[FI] = " + position);
    try {
        imageView.setImageBitmap(new ImageLoadTask().execute(
                JSONParserOnTheList.ANIMALS.get(position).photo).get());            
    } catch (InterruptedException e) {
        imageView.setImageResource(R.drawable.default_photo);
        e.printStackTrace();
    } catch (ExecutionException e) {
        imageView.setImageResource(R.drawable.default_photo);
        e.printStackTrace();
    }

AsyncTask 返回一个位图。

谢谢。

编辑(解决方案 -> 修复)

在应用了许多尝试解决问题的方法和方法之后,我找到了解决方案。 想法是布局工作正常,问题是在某些设备上,由于像素密度,图像被调整为较小的版本。

我在解码位图时直接在 asynctask 中应用了修复。

所以布局和其他一切都保持不变,asyntask 是这样的:

    private class ImageLoadTask extends AsyncTask<String, Void, Bitmap> {
    private Bitmap bitmap;
    @Override
    protected void onPreExecute() {
        loading.setTitle("Loading image ...");
        loading.show();
        super.onPreExecute();
    }

    @Override
    protected Bitmap doInBackground(String... params) {
        try {
            URL url;
            url = new URL(params[0]);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            BitmapFactory.Options options = new BitmapFactory.Options();

            // Bitmap scale depending on density
            // For example: 
            // 1600*1200 image to be resized to 640*480
            // (1600*2 equal to 640*5)
            options.inDensity = 5;
            options.inTargetDensity = 2;
            options.inScaled = false;
            // Decode the bitmap
            bitmap = BitmapFactory.decodeStream(input, null, options);
            input.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return bitmap;
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        loading.dismiss();
        loading.cancel();
        super.onPostExecute(result);
    }
}

注意 BitmapFactory 的选项,这是解决问题的重要部分。

此外,如果没有非常大的图像(又名全尺寸图像),您可以根据屏幕大小指定尺寸,如下所示:

ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
imageView.setMaxHeight(screenSize()[0]-150);
imageView.setMaxWidth(screenSize()[1]-150);


private int[] screenSize() {
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);

    int height = metrics.heightPixels;
    int width = metrics.widthPixels;

    return new int[] {height, width};

希望这对将来遇到此问题的任何人有所帮助。

最佳答案

请在scrollview中添加fadingEdge和fillViewport

<ScrollView 
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"      
      android:fillViewport="true"
      android:fadingEdge="none"
      android:background="@drawable/bg_app" >

关于android - ScrollView 内的 ImageView 不会调整大小以填满屏幕(保持纵横比),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17426637/

相关文章:

android - LazyColumn - Jetpack Compose 中的隐藏底部项目

python - 使用布局的绘图问题 - python

android - 在 'layout' res 文件夹中创建子文件夹

javascript - 如何优化窗口调整大小和初始化时的图像自动调整

java - 如何退出包含Java的Callable ExecutiorService MultiThreading的循环(Android)

android - 单一上下文。多个 GLSurfaceView 和一个纹理

android - 从收件箱中检索 SMS 消息

java - 如何获取 View 的父 fragment : 的实例

Qtreeview,选中时调整线的大小

android - 如何在不更改按钮大小的情况下更改按钮字体大小(在 android 中)?