package com.su.imagetextview;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebView.HitTestResult;
import android.widget.Toast;public classWebViewActitivy extends Activity {private String TAG = "WebViewActitivy";privateWebView webView;
@Overrideprotected voidonCreate(Bundle savedInstanceState) {
setContentView(R.layout.webview);
super.onCreate(savedInstanceState);
webView=(WebView) findViewById(R.id.news_body_webview_content);
webView.getSettings().setDefaultTextEncodingName("utf-8");//避免中文乱码
webView.addJavascriptInterface(this, "javatojs");
webView.setScrollBarStyle(0);
WebSettings webSetting=webView.getSettings();
webSetting.setJavaScriptEnabled(true);
webSetting.setNeedInitialFocus(false);
webSetting.setSupportZoom(true);
webSetting.setCacheMode(WebSettings.LOAD_DEFAULT|WebSettings.LOAD_CACHE_ELSE_NETWORK);try{//读取来自assets的信息 实际操作 _newsContent 来自网络
String _newsContent = getStringFromAssets("test.html");//这是本地的html模板
String htmlContent = getStringFromAssets("NewsDetail.html");//替换文本
String newsInfo = "发表时间:" + "19901195" + "查看:" + "255";
String newsTitle= "测试Html图文混排";
String LOCAL_PATH= "file:///android_asset/";//本地html//替换信息加载到html模板中
webView.loadDataWithBaseURL(
LOCAL_PATH,
htmlContent.replace("#title#", newsTitle)
.replace("#time#", newsInfo)
.replace("#content#", _newsContent), "text/html","utf-8", null);
webView.setOnTouchListener(touchlistener);/**
* 长按获取图片地址可以进行放大保存等操作*/webView.setOnLongClickListener(newOnLongClickListener() {
@Overridepublicboolean onLongClick(View v) {
HitTestResult hitTestResult=((WebView) v)
.getHitTestResult();if (hitTestResult.getType() ==HitTestResult.IMAGE_TYPE|| hitTestResult.getType() ==HitTestResult.IMAGE_ANCHOR_TYPE|| hitTestResult.getType() ==HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
Log.e(TAG,"保存这个图片!"
+hitTestResult.getExtra().toString());
}return true;
}
});
}catch(Exception e) {//TODO: handle exception
}
}/**
* 只能通过实现这个方法来模拟点击 事件 直接点击没效果*/
private OnTouchListener touchlistener = newOnTouchListener() {public boolean onTouch(View v, MotionEvent event) {float x = 0, y = 0;switch (event.getAction()) {caseMotionEvent.ACTION_DOWN:
x= (int) event.getRawX();
y= (int) event.getRawY();break;caseMotionEvent.ACTION_UP:if (x - event.getX() < 5 && y - event.getY() < 5) {
HitTestResult hitTestResult=((WebView) v)
.getHitTestResult();if (hitTestResult.getType() ==HitTestResult.IMAGE_TYPE|| hitTestResult.getType() ==HitTestResult.IMAGE_ANCHOR_TYPE|| hitTestResult.getType() ==HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
Log.e(TAG,"保存这个图片!"
+hitTestResult.getExtra().toString());
Toast.makeText(
WebViewActitivy.this,"保存这个图片!" +hitTestResult.getExtra().toString(),10).show();
}
}break;
}return false;
}
};
String getStringFromAssets(String path) throws IOException {
AssetManager assetManager=getAssets();
InputStream inputStream=assetManager.open(path);returninputStream2String(inputStream);
}public static String inputStream2String(InputStream in) throws IOException {
StringBufferout = newStringBuffer();byte[] b = new byte[4096];for (int n; (n = in.read(b)) != -1;) {out.append(new String(b, 0, n));
}return out.toString();
}
}