java應用數組和鏈表完成隊列示例。本站提示廣大學習愛好者:(java應用數組和鏈表完成隊列示例)文章只能為提供參考,不一定能成為您想要的結果。以下是java應用數組和鏈表完成隊列示例正文
蘋果的Touch Icon絕對我們都比擬熟習,是蘋果為了支撐收集運用(或許說網頁)添加到桌面須要的圖標,有了這些Touch Icon的網頁鏈接加倍和Native運用更相像了。因為蘋果裝備IPod,IPhone,IPad等裝備普遍,許多網頁都供給了touch icon這類圖標資本。因為Android中並沒有盡早的有一份如許的尺度,當我們想把網頁添加到桌面時,依然須要應用蘋果的Touch Icon。
Touch Icon
當我們想讓一個網頁比擬完善地添加到桌面,平日情形下我們須要設置一個png圖片文件作為apple-touch-icon。好比:
<link rel="apple-touch-icon" href="/custom_icon.png">
假如想支撐IPhone和IPad,我們須要應用sizes屬性來制訂多個圖片,默許sizes的值為60 x 60。
<link rel="apple-touch-icon" href="touch-icon-iphone.png">
<link rel="apple-touch-icon" sizes="76x76" href="touch-icon-ipad.png">
<link rel="apple-touch-icon" sizes="120x120" href="touch-icon-iphone-retina.png">
<link rel="apple-touch-icon" sizes="152x152" href="touch-icon-ipad-retina.png">
在IOS7之前,蘋果體系會對添加到桌面的圖標停止圓角化等視覺上的處置,為了不讓其處置,我們可使用apple-touch-icon-precomposed來作為rel的值完成。
Android中出缺陷的完成
在Android WebView供給了處置Touch Icon的回調,onReceivedTouchIconUrl(WebView view, String url,boolean precomposed)該辦法前往了對我們有效的touch icon的url,和能否為預組合(在IOS中不須要停止視覺處置)。固然有這些數據,我們可以停止處置,然則這個中是有成績的,就是我們欠好肯定文件的年夜小,來選擇合適的圖片。
舉個例子,以下一個網頁的源碼,個中sizes的次序不紀律
<link rel="apple-touch-icon-precomposed" sizes="72x72" href="http://www.qiyipic.com/20130423143600/fix/H5-72x72.png">
<link rel="apple-touch-icon-precomposed" sizes="114x114" href="http://www.qiyipic.com/20130423143600/fix/H5-114x114.png">
<link rel="apple-touch-icon-precomposed" sizes="57x57" href="http://www.qiyipic.com/20130423143600/fix/H5-57x57.png">
<link rel="apple-touch-icon-precomposed" href="http://www.qiyipic.com/20130423143600/fix/H5-0x0.png">
加載網頁,onReceivedTouchIconUrl輸入的日記
I/MainActivity( 6995): onReceivedTouchIconUrl url=http://www.qiyipic.com/20130423143600/fix/H5-0x0.png;precomposed=true
I/MainActivity( 6995): onReceivedTouchIconUrl url=http://www.qiyipic.com/20130423143600/fix/H5-57x57.png;precomposed=true
I/MainActivity( 6995): onReceivedTouchIconUrl url=http://www.qiyipic.com/20130423143600/fix/H5-114x114.png;precomposed=true
I/MainActivity( 6995): onReceivedTouchIconUrl url=http://www.qiyipic.com/20130423143600/fix/H5-72x72.png;precomposed=true
從下面的輸入來看,根本上是前面(書寫)的元素先打印出來,所以這個回調的缺點以下
1.因為Touch Icon url地址沒有硬性劃定,不克不及依據url包括某些尺寸來斷定應用哪一個icon
2.因為網頁編寫touch icon元素絕對隨便,不克不及依據onReceivedTouchIconUrl挪用前後來決議應用哪一個icon
3.回調中沒有sizes屬性值,欠好肯定應用哪一個icon
4.假如我們拔取質量最高的圖片,然落後行恰當緊縮處置也許可以處理成績,然則將全體icon下載上去或許依據Head頭信息總感到不怎樣好。
改良辦法
既然WebView沒有現成的辦法知足我們的需求,只好本身來完成。其實完成辦法照樣比擬簡略地就是js劇本注入檢測網頁元素中得touch icon,前往json數據。
JavaScript辦法
上面的JS代碼所做的功效為查找一切為touch icon的link元素,包括正常的還標志為precomposed。然後將這些link元素的屬性存入json數據,最初前往給Java代碼中對應的回調。
var touchIcons = [];
function gatherTouchIcons(elements) {
var normalTouchIconLength = elements.length;
var currentElement;
for (var i =0; i < normalTouchIconLength;i++) {
currentElement = elements[i];
var size;
if (currentElement.hasAttribute('sizes')) {
size = currentElement.sizes[0];
} else {
size = '';
}
var info = {'sizes':size, 'rel': currentElement.rel, 'href': currentElement.href};
touchIcons.push(info);
}
}
function obtainTouchIcons() {
normalElements = document.querySelectorAll("link[rel='apple-touch-icon']");
precomposedElements = document.querySelectorAll("link[rel='apple-touch-icon-precomposed']");
gatherTouchIcons(normalElements);
gatherTouchIcons(precomposedElements);
var info = JSON.stringify(touchIcons);
window.app_native.onReceivedTouchIcons(document.URL, info);
}
obtainTouchIcons();
Java代碼
這裡為了便於懂得照樣全體貼出了demo的源碼,demo中當網頁加載完成以後注入下面的js代碼獲得touch icon信息,然後前往給java的回調辦法中。假如不清晰Java和JavaScript交互,可以拜訪Android中Java和JavaScript交互懂得更多。
package com.example.obtaintouchicon;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
protected String LOGTAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webView = new WebView(this);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
final String touchIconJsCode = getTouchIconJsCode();
Log.i(LOGTAG , "onPageFinished url = " + url + ";touchIconJsCode=" + touchIconJsCode);
view.loadUrl("javascript:" + touchIconJsCode);
}
});
webView.addJavascriptInterface(new JsObject(), "app_native");
webView.loadUrl("http://192.168.1.5:8000/html/touchicon.html");
}
private class JsObject {
@JavascriptInterface
public void onReceivedTouchIcons(String url, String json) {
Log.i(LOGTAG, "onReceivedTouchIcons url=" + url + ";json=" + json);
}
}
private String getTouchIconJsCode() {
StringBuilder total = new StringBuilder();
InputStream inputStream = null;
BufferedReader bufferReader = null;
try {
inputStream = getAssets().open("touchicon.js");
bufferReader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = bufferReader.readLine()) != null) {
total.append(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != inputStream) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return total.toString();
}
}
前往的JSON數據
[
{
"sizes":"72x72",
"rel":"apple-touch-icon-precomposed",
"href":"http://www.qiyipic.com/20130423143600/fix/H5-72x72.png"
},
{
"sizes":"114x114",
"rel":"apple-touch-icon-precomposed",
"href":"http://www.qiyipic.com/20130423143600/fix/H5-114x114.png"
},
{
"sizes":"57x57",
"rel":"apple-touch-icon-precomposed",
"href":"http://www.qiyipic.com/20130423143600/fix/H5-57x57.png"
},
{
"sizes":"",
"rel":"apple-touch-icon-precomposed",
"href":"http://www.qiyipic.com/20130423143600/fix/H5-0x0.png"
}
]
我們可以對獲得的JSON數據依照須要處置。
Google會改良麼
謎底是會,並且曾經改良,但Google修正的不是onReceivedTouchIconUrl這個辦法,而是Google正在履行本身的一套規矩。
在Chrome上,Google增長了如許一個元素,這是Google供給的為網頁法式界說元數據的辦法。
<link rel="manifest" href="manifest.json">
在元數據json中,你可以自界說title,肇端頁,法式是橫屏照樣豎屏展現。一個簡略地json實例以下,這裡我們可以看到個中icons中存在多個相似touch icon的圖標,src代表圖標途徑,sizes代表年夜小,type就是mimetype,density指的是Android中的屏幕密度(如許加倍Android化了)。
{
"name": "Web Application Manifest Sample",
"icons": [
{
"src": "launcher-icon-0-75x.png",
"sizes": "36x36",
"type": "image/png",
"density": "0.75"
},
{
"src": "launcher-icon-1x.png",
"sizes": "48x48",
"type": "image/png",
"density": "1.0"
},
{
"src": "launcher-icon-1-5x.png",
"sizes": "72x72",
"type": "image/png",
"density": "1.5"
},
{
"src": "launcher-icon-2x.png",
"sizes": "96x96",
"type": "image/png",
"density": "2.0"
},
{
"src": "launcher-icon-3x.png",
"sizes": "144x144",
"type": "image/png",
"density": "3.0"
},
{
"src": "launcher-icon-4x.png",
"sizes": "192x192",
"type": "image/png",
"density": "4.0"
}
],
"start_url": "index.html",
"display": "standalone",
"orientation": "landscape"
}
關於Google這套新的尺度,可以參考Add to Homescreen
然則因為今朝,這類尺度實行率絕對比擬低,所以我們照樣須要應用蘋果的touch icon。