摘要:一個非常小型的項目,但是該有的都有??焖偬D至頂部,底部,指定位置。讀寫緩存的效率遠低于,所以采用實現的緩存機制,速度快了大概,倍。序列化性能性能測試,,為了避免干擾,我們使用進行測試。
一個非常小型的Kotlin項目,但是該有的都有。如果您覺得有意思就幫我star下,人多了我就會放更多的福利哦。github
先上福利。Release1.0
特點列表顯示圖片,點擊查看更多。
快速跳轉至頂部,底部,指定位置。
收藏,查看歷史記錄。
設置壁紙。
離線緩存。
組成語言:Kotlin,Java
網絡請求:HttpUrlConnection
數據庫:Sqlite
數據源:Jsoup
第三方庫:Glide
概述1) 網絡請求
網絡框架并沒有使用RxRetrofit等,為了保證精簡高效直接使用的HttpUrlConnection
get
val request = AsyncNetwork() request.request(Constants.HOST_MOBILE_URL, null) request.setRequestCallback(object : IRequestCallback { override fun onSuccess(httpURLConnection: HttpURLConnection?, response: String) { //todo } })
post
val request = AsyncNetwork() request.request(Constants.HOST_MOBILE_URL, mutableMapOf()) request.setRequestCallback(object : IRequestCallback { override fun onSuccess(httpURLConnection: HttpURLConnection?, response: String) { //todo } })
2) 數據庫
數據庫沒有使用第三方框架,直接使用的sql語句。
CREATE TABLE tb_class_page_list ( _id INTEGER PRIMARY KEY ASC AUTOINCREMENT, href STRING UNIQUE, description STRING, image_url STRING, id_class_page INTEGER REFERENCES tb_class_page (_id) ON DELETE CASCADE ON UPDATE CASCADE, [index] INTEGER);
3) 讀寫緩存
Serializable的效率遠低于Parcelable,所以采用Parcelable實現的緩存機制,速度快了大概7,8倍。
讀取緩存
val helper = PageListCacheHelper(container?.context?.filesDir?.absolutePath) val pageModel: Any? = helper.get(key)
寫入緩存
val helper = PageListCacheHelper(context.filesDir.absolutePath) helper.put(key, object)
刪除緩存
val helper = PageListCacheHelper(context.filesDir.absolutePath) helper.remove(key)
4) jsoup獲取數據
由于數據是用從html頁面中提取的,所以速度偏慢,為了不影響體驗做了一套緩存機制,來做到流暢體驗。
Document doc = Jsoup.parse(html); Elements elements = body.getElementsByTag("a"); String text = elements.get(0).text(); String imageUrl = elements.get(0).attr("src"); ...
5) 組件
Activity fragment替代activity來顯示新界面
因為activity需要在Manifiest中注冊,所以當有多個activity的時候,就需要編寫很長的Manifiest文件,嚴重影響了Manifiest的可讀性,界面的風格也比較笨重。所以一個新頁面就注冊一個activity不太合適,我們通過用activity做為容器添加不同的Fragment來達到注冊一個activity啟動多個不同頁面的效果。生命周期由activity管理,更方便簡潔。
open class BaseFragmentActivity : BaseActionActivity() { companion object { private const val EXTRA_FRAGMENT_NAME = "extra_fragment_name" private const val EXTRA_FRAGMENT_ARG = "extra_fragment_arguments" @JvmOverloads @JvmStatic fun newInstance(context: Context, fragment: Class<*>, bundle: Bundle? = null, clazz: Class= getActivityClazz()): Intent { val intent = Intent(context, clazz) intent.putExtra(EXTRA_FRAGMENT_NAME, fragment.name) intent.putExtra(EXTRA_FRAGMENT_ARG, bundle) return intent } protected open fun getActivityClazz(): Class { return BaseFragmentActivity::class.java } } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.cc_activity_base_fragment) val fragmentName = intent.getStringExtra(EXTRA_FRAGMENT_NAME) var fragment: Fragment? = null if (TextUtils.isEmpty(fragmentName)) { //set default fragment //fragment = makeFragment(MainFragment::class.java!!.getName()) } else { val args = intent.getBundleExtra(EXTRA_FRAGMENT_ARG) try { fragment = makeFragment(fragmentName) if (args != null) fragment?.arguments = args } catch (e: Exception) { e.printStackTrace() } } if (fragment == null) return supportFragmentManager .beginTransaction() .replace(R.id.fragment_container, fragment) .commit() } fun makeFragment(name: String): Fragment? { try { val fragmentClazz = Class.forName(name) return fragmentClazz.newInstance() as Fragment } catch (e: Exception) { e.printStackTrace() } return null } }
6) 序列化性能
性能測試,Serializable VS Externalizable,為了避免干擾,我們使用AndroidTest進行測試。
模型
class Model1 implements Serializable { String text; int code; boolean bool; Model1 child; } class Model2 extends Model1 implements Externalizable { public Model2() { } @Override public void readExternal(ObjectInput input) throws IOException, ClassNotFoundException { text = input.readUTF(); code = input.readInt(); bool = input.readBoolean(); child = new Model2(); child.text = input.readUTF(); child.code = input.readInt(); child.bool = input.readBoolean(); } @Override public void writeExternal(ObjectOutput output) throws IOException { output.writeUTF(text); output.writeInt(code); output.writeBoolean(bool); if (child != null) { output.writeUTF(child.text); output.writeInt(child.code); output.writeBoolean(child.bool); } } }
測試
@Test public void serializableVSExternalizable() throws Exception { ListtestModel1 = new ArrayList<>(); for (int i = 0; i < 50000; i++) { Model1 model1 = new Model1(); model1.text = "Hello World " + i; model1.code = i; model1.bool = false; Model1 child = new Model1(); child.text = "Hello World Child" + i; child.code = i; child.bool = false; model1.child = child; testModel1.add(model1); } long startTime = System.currentTimeMillis(); File file = new File("/sdcard/serializable"); ObjectOutputStream oStream = new ObjectOutputStream(new FileOutputStream(file)); oStream.writeObject(testModel1); oStream.close(); Log.e("serializable", "write time " + (System.currentTimeMillis() - startTime)); startTime = System.currentTimeMillis(); ObjectInputStream iStream = new ObjectInputStream(new FileInputStream(file)); testModel1 = (List ) iStream.readObject(); iStream.close(); Log.e("serializable", "read time " + (System.currentTimeMillis() - startTime)); testModel1 = null; List testModel2 = new ArrayList<>(); for (int i = 0; i < 50000; i++) { Model2 model2 = new Model2(); model2.text = "Hello World " + i; model2.code = i; model2.bool = false; Model2 child = new Model2(); child.text = "Hello World Child" + i; child.code = i; child.bool = false; model2.child = child; testModel2.add(model2); } startTime = System.currentTimeMillis(); file = new File("/sdcard/externalizable"); oStream = new ObjectOutputStream(new FileOutputStream(file)); oStream.writeObject(testModel2); oStream.close(); Log.e("externalizable", "write time " + (System.currentTimeMillis() - startTime)); startTime = System.currentTimeMillis(); iStream = new ObjectInputStream(new FileInputStream(file)); testModel2 = (List ) iStream.readObject(); iStream.close(); Log.e("externalizable", "read time " + (System.currentTimeMillis() - startTime)); }
結果
序列化5000個對象 Serializable:寫入耗時4026 ms,讀取耗時177 ms Externalizable:寫入耗時2680 ms,讀取耗時79 ms 序列化50000個對象 Serializable:寫入耗時46872 ms,讀取耗時1807 ms Externalizable:寫入耗時41334 ms,讀取耗時792 ms
從結果上可以看到Externalizalbe相比于Serializable是稍微快一些點不管是寫入還是讀取速度。對象存儲還可以使用一些對象關系映射(ORM)型的數據庫。如GreenDao等等。
7) Java中的深拷貝
由于System.arrayCopy()該方法拷貝數組的時候,如果是基本數據類型則是深拷貝,如果是對象類型則會是淺拷貝,無法做到深拷貝,所以想深拷貝一個數組就得循環創建對象并賦值,這顯得很麻煩。所以項目中使用序列化的方法進行深拷貝。PS:Serializable序列化方式讀取的時候并不會調用對象構造方法,而Externalizable序列化方式讀取時會調用對象的無參構造方法。
@SuppressWarnings("unchecked") public staticT deepCopyOrThrow(T src) throws IOException, ClassNotFoundException { ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(byteOut); out.writeObject(src); ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray()); ObjectInputStream in = new ObjectInputStream(byteIn); return (T) in.readObject(); } public static T deepCopy(T src) { try { return deepCopyOrThrow(src); } catch (Exception ignore) { ignore.printStackTrace(); return null; } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://m.specialneedsforspecialkids.com/yun/68026.html
閱讀 3225·2021-11-08 13:21
閱讀 1202·2021-08-12 13:28
閱讀 1413·2019-08-30 14:23
閱讀 1935·2019-08-30 11:09
閱讀 850·2019-08-29 13:22
閱讀 2694·2019-08-29 13:12
閱讀 2557·2019-08-26 17:04
閱讀 2265·2019-08-26 13:22