gpt4 book ai didi

android - webView中如何控制缩放

转载 作者:行者123 更新时间:2023-11-29 14:25:31 27 4
gpt4 key购买 nike

我有这个代码:

[...]
{
webView = (WebView) findViewById(R.id.webView1);
webView.setInitialScale(getScale(Double.parseDouble(pubPages)));
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setPadding(0, 0, 0, 0);

webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
activity.setTitle("Loading...");
activity.setProgress(progress * 100);

if(progress == 100)
activity.setTitle(R.string.app_name);
}
});

webView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
// Handle the error
}

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});

webView.loadUrl("http://urltoshow");
}

private int getScale(double numPages){
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenHeight = dm.heightPixels;

Double val = (new Double(screenHeight-40)/(new Double(1024)));
val = val * 100d;
return val.intValue();
}

它工作正常,但我想禁止将侧面缩放到其自然高度以下。

可以放大,可以缩小,但要达到一定程度。

如何做到这一点?

最佳答案

为了子孙后代:

public class CustomWebView extends WebView {
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;

PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;
float scale = 0f;
float oldscale = 0f;
int displayHeight;

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

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

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

@Override
public boolean onTouchEvent(MotionEvent ev) {
boolean consumed = super.onTouchEvent(ev);

if (isClickable())
switch (ev.getAction() & MotionEvent.ACTION_MASK) {

case MotionEvent.ACTION_DOWN:
start.set(ev.getX(), ev.getY());
mode = DRAG;
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(ev);
if (oldDist > 5f) {
midPoint(mid, ev);
mode = ZOOM;
}
break;

case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
}
else if (mode == ZOOM) {
float newDist = spacing(ev);
if (newDist > 5f) {
scale = newDist / oldDist;
if(scale>1){
if(Math.abs(oldscale-scale)>0.3){
zoomIn();
oldscale = scale;
}
}
System.out.println(scale);
if(scale<1){
if((getContentHeight()*getScale()>displayHeight)){
zoomOut();
System.out.println(getScale());
}
}
}
}
break;
}
return consumed;
}

private float spacing(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}

private void midPoint(PointF point, MotionEvent event) {
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
point.set(x / 2, y / 2);
}
}

这个工作正常。

关于android - webView中如何控制缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8049343/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com