goodier
goodier

Reputation: 395

Android WebView zoom image not working for width="100%"

When I use the following html:

img src="file:///sdcard/image1.jpg" width="100%" heighth="100%"

and use WebView.setBuiltInZoomControls(true), my webview is zoomable, but after I release my finger, the webview is zoomed back.

I guess it is because of width="100%" heighth="100%".

But image1.jpg is big, and at the beginning I want to show the whole image on screen. Later the user can zoom in/out.

Any solution or code sample?

Upvotes: 4

Views: 2908

Answers (3)

HaMMeReD
HaMMeReD

Reputation: 2462

If you are only showing a image off the Sd card are you sure you want to use a webview, or would it make more sense to use a ImageView. There are lots of examples/free code that Extend ImageView and provide this functionality.

A Webview may be the most bloated way of handling this functionality if all you want to do is display an image.

Upvotes: 0

fjs
fjs

Reputation: 131

I believe you're writing it wrong!

img src="file:///sdcard/image1.jpg" width="100%" heighth="100%"
img src="file:///sdcard/image1.jpg" width="100%" height="100%"

It is height and not what you've got there.

Upvotes: 0

Shankar Agarwal
Shankar Agarwal

Reputation: 34765

You have to calculate the scale that you need to use manually. PIC_WIDTH is the width of the webview.

private int getScale(){
    Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 
    int width = display.getWidth(); 
    Double val = new Double(width)/new Double(PIC_WIDTH);
    val = val * 100d;
    return val.intValue();
}

Then use

WebView web = new WebView(this);
web.setPadding(0, 0, 0, 0);
web.setInitialScale(getScale());

Refer this LINK LINK

Upvotes: 1

Related Questions