2012-03-20 2 views
1

Привет Я использую следующий макет для отображения Webview содержание ...центр изображения в 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"); 

То, что я хочу, чтобы центр это изображение в веб-просмотре. Может кто-нибудь, пожалуйста, взглянуть и что-нибудь предложить?

ответ

0

Я столкнулся с этой же проблемой, когда я использовал WebView для отображения статической карты google. Я хотел бы центрировать WebView в центре карты google. Вот мое решение:

Я расширил WebView, чтобы у меня был доступ к его методам computeHorizontalScrollRange() и 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 в установлены). И я называю centerMap() в случае onNewPicture так, что он уважает метод 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(); 
     } 
    });     
}  

отлично работает для меня на всех размеров экрана. Надеюсь, поможет!

Смежные вопросы