android webview图文混排,Android 图文混排 通过webview实现并实现点击图片

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();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值