WebView中的中心图像

时间:2012-03-20 09:37:21

标签: android android-layout

您好我使用以下布局显示webview内容...

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/ads_screen"
android:background="@drawable/splash_gradient">
<FrameLayout 
    android:id = "@+id/ads_frame"
    android:layout_weight="8"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_gravity="center"
    android:scaleType="centerInside"
    android:background="@drawable/splash_gradient">  

<WebView
    android:id="@+id/ads_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginLeft = "10dp"
    android:layout_marginRight = "10dp"
    android:scaleType="center"
    />

</FrameLayout>

<ProgressBar
    android:id="@+id/ads_progress_bar"
    android:layout_width="18dp"
    android:layout_height="18dp"
    android:layout_gravity="center"/>

<TextView 
    android:layout_width="fill_parent"
    android:layout_weight="3"
    android:layout_height="0dp"        
    android:gravity="center"
    android:id="@+id/ads_text"
    android:text="@string/connecting"
    android:textColor="@color/white"/>

</LinearLayout>

在代码中我使用以下内容:

View adsFragment = inflater.inflate(R.layout.ads_fragment, container, false);
i_image = (WebView) adsFragment.findViewById(R.id.ads_image);
View view = adsFragment.findViewById(R.id.ads_screen);
final View f = (FrameLayout)adsFragment.findViewById(R.id.ads_frame);
i_image.getSettings().setJavaScriptEnabled(false);
i_image.getSettings().setPluginState(android.webkit.WebSettings.PluginState.OFF);
i_image.getSettings().setBuiltInZoomControls(false);
i_image.getSettings().setSupportZoom(false);
i_image.setBackgroundColor(0);

i_image.setWebViewClient(new WebViewClient() {

public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

           if (DebugConstants.ENABLE_LOGS)
            {
                 Log.info("onReceivedError() URL: " + failingUrl + " ERROR: " + description);
            }   
            f.setVisibility(View.GONE);                            
        }
    });

i_image.loadUrl("file:///android_res/drawable/splashscreen_default.png");

我想要的是将此图像置于webview中心。有人可以看看并提出建议吗?

1 个答案:

答案 0 :(得分:0)

当我使用WebView显示静态谷歌地图时,我遇到了同样的问题。我想将WebView置于谷歌地图的中心。这是我的解决方案:

我扩展了WebView,以便可以访问其computeHorizo​​ntalScrollRange()和computeVerticalScrollRange()方法。注意“centerMap()”函数:

public class WebMapView extends WebView {

    public WebMapView(Context context) {
        super(context);
    }

    public WebMapView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public WebMapView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void centerMap(){
        int scrollX = (computeHorizontalScrollRange() - getWidth()) / 2;
        int scrollY = (computeVerticalScrollRange() - getHeight()) / 2;
        scrollTo(scrollX, scrollY);
    }
}

在我的活动onCreate中,设置全局布局后设置WebMapView(因此设置了WebMapView的宽度和高度)。我在onNewPicture事件中调用centerMap(),以便它尊重scrollTo()方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.vendor_info);

    mWebMap = (WebMapView) findViewById(R.id.webMap);
    mWebMap.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            mWebMap.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            setupWebMapView();              
        }
    });             
} 


private void setupWebMapView(){

    final String latitude = String.valueOf(mData.venues.get(0).lat);
    final String longitude = String.valueOf(mData.venues.get(0).lon);

    String centerUrl = String.format(WEBMAP_URL, latitude, longitude, WEBMAP_ZOOM, sVenueImageWidth, sVenueImageHeight, latitude, longitude, lat, lon);

    mWebMap.getSettings().setJavaScriptEnabled(true);
    mWebMap.getSettings().setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);
    mWebMap.setWebViewClient(new WebViewClient());
    mWebMap.loadUrl(centerUrl);

    mWebMap.setPictureListener(new PictureListener() {

        @Override
        public void onNewPicture(WebView view, Picture picture) {
            mWebMap.centerMap();
        }
    });                 
}       

在所有屏幕尺寸上都适合我。希望它有所帮助!