我需要在每個頁面上使用webview查看傳呼機。應用程序將下載從服務器到設備緩存的遠程(大型)圖像,當下載完成時,它應顯示在當前的webview中。這是我的看法尋呼機項目佈局:查看傳呼機與網絡瀏覽器快速滾動
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:gravity="center"
android:padding="10dp" >
<WebView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00000000"
android:padding="1dp" />
</RelativeLayout>
,這是考慮到尋呼機適配器我instantiateItem方法:
public Object instantiateItem(ViewGroup container, int position) {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.viewpager_item, container,
false);
imgflag = (WebView) itemView.findViewById(R.id.image);
imgflag.setBackgroundColor(0x000000);
imgflag.getSettings().setBuiltInZoomControls(true);
imgflag.getSettings().setUseWideViewPort(true);
imgflag.getSettings().setLoadWithOverviewMode(true);
DownloadImageTask task = new DownloadImageTask(urls[position]);
task.execute();
((ViewPager) container).addView(itemView);
return itemView;
}
這是DownloadImageTask類:
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
String imageUrl;
String filename;
public DownloadImageTask(String url)
{
super();
this.imageUrl=url;
}
protected Bitmap doInBackground(String... urls) {
Bitmap image = null;
Log.d("Start", "Start downloading");
filename=imageUrl.replace('/', '-');
filename=filename.replace('.', '-');
filename=filename.replace(':', '-');
File file=new File(context.getCacheDir(), filename);
if(file.exists())
{
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(context.getCacheDir() + "/" + filename);
return bitmap;
}
URL url;
try {
url = new URL(imageUrl);
HttpURLConnection ucon = (HttpURLConnection) url.openConnection();
image = BitmapFactory.decodeStream(ucon.getInputStream());
File cacheDir = context.getCacheDir();
File imageFile = new File(cacheDir, filename);
FileOutputStream fos;
fos = new FileOutputStream(imageFile);
image.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close();
fos.flush();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return image;
}
protected void onPostExecute(Bitmap result) {
if(result!=null)
{
String style="width='100%'";
if(result.getWidth()>result.getHeight())
style="height='100%'";
String base = context.getCacheDir().getAbsolutePath().toString();
String imagePath = "file://"+ base + "/" + filename;
String html = "<html><head></head><body><img " + style + " src=\""+ imagePath + "\"></body></html>";
imgflag.loadDataWithBaseURL("", html, "text/html","utf-8", "");
}
}
}
而且一切都運行完美當我慢慢滾動,一頁頁......但如果我快速滾動約10-20頁(無需等待每一個加載),它沒有工作的權利。它不會崩潰或類似的東西,但視圖只是黑色。當我離開開始加載的頁面時,開始下載不會停止,它仍然會在後臺加載圖像,所以也許會導致問題?當它不加載圖像時,控制檯說:nativeOnDraw失敗;清除背景顏色。
有沒有簡單的解決這個問題?我不應該使用任何外部代碼,庫或類似的東西 - 我應該自己編寫一切代碼。