I need to be able to display an image with zoom and scroll capabilities. Since this seems a bit too tedious with ImageView, I’m doing it with a WebView which loads the local image and then displays it. This is my current code:
WebView image = (WebView)findViewById(R.id.image);
image.getSettings().setBuiltInZoomControls(true);
image.getSettings().setLoadWithOverviewMode(true);
image.getSettings().setUseWideViewPort(true);
image.setInitialScale(40);
image.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
image.loadUrl("file:///android_asset/image.png");
While this loads the image, I can’t get it to display only the image. There is always a decent amount of white space below it since setUseWideViewPort(true) makes it go all the way out to scale the width.
Any suggestions on allowing it to only scroll over the image that is loaded? Thanks. I’ve also tried setDefaultZoom and can’t get it to go out far enough.
I think using a static percentage 40 within your setInitialScale method is decent, however, it does not take into consideration screen density or size per say. Currently your 40 means that you are scaled to 40% of the actual size.
Think about when you open up an image only in your web browser on your PC, this is going to essentially respond the same way, put it in the upper left corner. Yes you can scale it with a WebView, but in this case to make it work for all platforms you are going to have to calculated your scaling factor based upon the screen resolution minus any bounding rectangle (top and bottom in the Android’ case) related to the actual width and height of the image.
I suggest you go back to the ImageView!