«

webView 手势缩放以及自适应屏幕

时间:2024-3-2 18:44     作者:韩俊     分类: Android


方法一

// 是否支持脚本

webView.getSettings().setJavaScriptEnabled(true);

//支持缩放

webView.getSettings().setSupportZoom(true);

//自动适应屏幕

webView.getSettings().setLoadWithOverviewMode(true);

webView.getSettings().setUseWideViewPort(true);


float OldX1,OldY1,OldX2,OldY2,NewX1,NewY1,NewX2,NewY2;


public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_POINTER_2_DOWN:
if (event.getPointerCount() == 2) {
for (int i = 0; i < event.getPointerCount(); i++) {
if (i == 0) {
OldX1 = event.getX(i);
OldY1 = event.getY(i);
} else if (i == 1) {
OldX2 = event.getX(i);
OldY2 = event.getY(i);
}
}
}
break;
case MotionEvent.ACTION_MOVE:
if (event.getPointerCount() == 2) {
for (int i = 0; i < event.getPointerCount(); i++) {
if (i == 0) {
NewX1 = event.getX(i);
NewY1 = event.getY(i);
} else if (i == 1) {
NewX2 = event.getX(i);
NewY2 = event.getY(i);
}
}
float disOld = (float) Math.sqrt((Math.pow(OldX2 - OldX1, 2) + Math.pow(
OldY2 - OldY1, 2)));
float disNew = (float) Math.sqrt((Math.pow(NewX2 - NewX1, 2) + Math.pow(
NewY2 - NewY1, 2)));
Log.d("onTouch","disOld="+disOld+"|disNew="+disNew);
if (disOld - disNew >= 25) {
// 缩小
webView.zoomOut();

} else if(disNew - disOld >= 25){
// 放大
webView.zoomIn();
}
OldX1 = NewX1;
OldX2 = NewX2;
OldY1 = NewY1;
OldY2 = NewY2;
}
}


return false;
}


方法二

只需设置2个属性就可以了

webView.getSettings().setSupportZoom(true);

webView.getSettings().setBuiltInZoomControls(true);

标签: android

热门推荐