0

我正在使用Android-Query将图像加载到 web 视图中,因此我可以使用该特定视图附带的缩放功能。

现在,我正在加载的图像比它们的宽度高,因此,在我的布局中,正在被裁剪(看不到相关图像的底部)。

无论如何我可以更改我的代码以强制加载的图像缩放以适合视图吗?文档说...

除了 ImageView,WebView 还可用于显示图像以及 Android 内置对 WebView 的缩放支持。图像将居中并根据其方向填充 webview 的宽度或高度。

所以我想我可能不走运?这是加载图像的相关代码行......

aq.id(R.id.webview).progress(R.id.progressbar).webImage(imageUrl);

这是layoutyt..

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <WebView
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <ProgressBar
        android:id="@+id/progressbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />
  </RelativeLayout>
4

1 回答 1

0

好的。有一个适用于我的特定场景的解决方案,所以......

由于我知道加载到 webview 中的图像比例,我想我可以将 webview 调整为正确的比例,以确保它正确地适合可用空间。我将 XML 更新为此...

 <RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:id="@+id/wrapper"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:orientation="vertical" >

        <WebView
            android:id="@+id/webview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

    </LinearLayout>

    <ProgressBar
        android:id="@+id/progressbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

</RelativeLayout>

...然后将此添加到我的活动中(从此处获取的想法)...

@Override
public void onWindowFocusChanged(boolean hasFocus) {

    // need to set correct proportions of webview to match image
    super.onWindowFocusChanged(hasFocus);
    WebView mWrapper = (WebView) findViewById(R.id.webview);
    int w = mWrapper.getWidth();
    int h = mWrapper.getHeight();
    while ( w * 1.28 > h ) {
        w--;
    }
    LayoutParams params = new LinearLayout.LayoutParams( (int) w, LinearLayout.LayoutParams.FILL_PARENT );
    mWrapper.setLayoutParams(params);
}

...其中“1.28”值是我的图像的宽度和高度之间的比率(高度除以宽度)。

因此,图像被添加到 webview,视图被布局,然后这段代码开始并缩小宽度,直到它小到可以使用适当的比例适应可用高度。新的 LinearLayout 使 webview 居中以保持整洁。

于 2013-11-29T04:07:00.310 回答