Chapter Three classic scene design

Source: Internet
Author: User
Tags switches

Picture cache:

Imageloader knowledge point is older, temporarily do not summarize (digression: Recommended glide or fresco, but must be carried out two times package, otherwise who know which day whim, think of a change frame to try it)

Image Loading tool fresco:configuration file configuration:
< Uses-permissionandroid:name = "Android.permission.INTERNET" />
to initialize in application:
Fresco.initialize (context);
Replace the ImageView of the displayed picture in the program with Simpledraweeview and add the fresco namespace:
<!--other elements--><linearlayoutxmlns:android= "Http://schemas.android.com/apk/res/android"xmlns: Fresco= "Http://schemas.android.com/apk/res-auto" > join Simpledraweeview:<  Com.facebook.drawee.view.SimpleDraweeViewandroid:id= "@+id/my_image_view"android:layout_width= " 20DP "android:layout_height=" 20DP "fresco:placeholderimage=" @drawable/my_drawable "/>
load network images in activity or fragment:
URI Uri =uri.parse ("https://raw.githubusercontent.com/facebook/fresco/gh-pages/static/fresco-logo.png"= (Simpledraweeview) Findviewbyid (R.id.my_image_view);d Raweeview.setimageuri (URI);
Image Pipeline Workflow:Freso Three-tier caching concept: Bitmap cache: 5.0,bitmap is cached in the heap heap. 4.0 and below: Bitmap is located in Ashmem, not in Java's heap, does not cause excessive GC because of picture creation, and when the app switches to the background, it empties the bitmap cache.  Memory Cache: Store image raw compressed format, remove from memory and decode, switch backstage, clean up memory cache.  disk cache: stored locally and not lost. online fresco Chinese documents can be seen below: Http://fresco-cn.org/docs/index.html#_Network traffic optimization:
  1. Data is compressed and processed for transmission, using gzip
  2. Transfer format using JSON or Protobuffer
  3. Combine multiple data requests for a page whenever possible
  4. Use long connections instead of HTTP requests
  5. leave the page, cancel the request ( In this respect, for example, I used the volley, now use the okhttp have to provide a way to cancel, generally the cancellation of operations uniformly encapsulated in the base class Ondestory () inside ).
  6. Establish a retry mechanism. The general setting retries the operation three times, only for get,post to prevent retries.
 Picture Traffic optimization:
    1. The client can pass the width to the server side to crop the picture, and to get the picture that conforms to the current display control, the relative pressure on the server side is larger.
    2. Reduces the quality of images for 2G and 3G users, thus reducing the amount of traffic the picture occupies.
    3. In fact, if the application to the picture requirements are not very high, you can control the application in the 2G,3G network environment does not load the picture, and in the case of WiFi load, of course, it is best to set an optional inside the application for the user to choose.
 City list data:City list information, which generally includes the following:Cityid City IDCityName City NamePinyin City Pinyinjianpin City Jane Pinyin(Off-topic, there are two ways to get city list information in a general project:1, direct db file or JSON file,2, each time fetched from the server side, but serialized locally, read from the cache file to read)In the case of 2, there is still a problem, that is, every time you go to serialize the content, or every time from the server side to get the cache database or file inside it, it is obviously unwise.  in the book for the above question, in fact, in the JSON structure design time to do a more appropriate way to solve:
{   "Citys" : {},   false,   "version": 1}
IsMatch: Indicates whether the version of the local version and server-side city list information is consistent. Version: The release information for the city list data. citys: City list data. the JSON data for the city list is added to the Version field, and if the version field does not change, the content is not updated the second time, and the data is incremented when the version field changes. policy on this side of the app:
    1. Local Store The latest city listing information, including version information, before the outgoing version;
    2. To get the city list, the local city information list inside the version of the information into the interface, according to return, IsMatch field to determine whether and server-side consistent, if consistent, read the client cache data, inconsistent resolution and cache the latest city list data.
    3. Interface call failed, read local cache city list data.
    4. Open gzip compression to ensure the least amount of data is transmitted.
    5. Whether parsing or reading the local cached city information, be sure to work asynchronously, otherwise it is easy to stutter.
City List Information Incremental update:It is divided into 2 situations:
    1. The original table data is to be modified.
    2. There are new city list data.
The following scenario is also addressed for both scenarios:There are three kinds of operations that exist for city information:
    1. Add new city information;
    2. Delete invalid city information;
    3. Updating existing city data;
Workaround:Add type to city information:
{    "IsMatch":false; "Version": 1,    "Citys":{                {                    "Cityid": 1,                    "CityName": "Xian",                    "Pinyin": "Xian",                    "Jianpin": "XA",                    ' Type ': ' Delete '                },                {                    "Cityid": 2,                    "CityName": "Beijing",                    "Pinyin": "Beijing",                    "Jianpin": "BJ",                    ' Type ': ' Update '                },                {                    "Cityid": 3,                    "CityName": "Nanjing",                    "Pinyin": "Nanjing",                    "Jianpin": "NJ",                    ' Type ': ' Add '                }            }}                

Delete deletes the city information for the specified CityidUpdate Update updates city information for a CityidAdd adds new city information the app interacts with HTML5: Code Address:Https://github.com/icodeu/AppProgrammingSource/tree/master/3.4%20App%E4%B8%8EHtml5%E4%BA %a4%e4%ba%92 HTML5 and native direct and flexible switching:two sets of interface native HTML5 each set, through the interface inside the field control is to show native or HTML5 interface. Policy:
    1. The background depends on the version configuration to determine whether the page is displayed and interacted with native or HTM5 pages.
    2. When the app launches, gets whether the page is HTML5 or native page.
    3. Page jump to achieve loose coupling, design a Navigatormanager class, to control all pages in the project jump operation.
eliminate global variables:when the app switches to the background, the previously stored global variables are easily recycled, and if you switch back to the foreground, you might be using a global variable, but the variable is null and crashes. Workaround:Serialization. use intent for data transfer (the disadvantage is that you cannot carry data with a large amount of data). serializes the data locally, if the in-memory variable is reclaimed, from the local fetch. the problem with the above scenario:
    1. Enforces serialization operations and is prone to ANR.
    2. The serialized generated file is lost due to insufficient memory.
    3. Jsonobject, Jsonarray does not support serialization, hashmap<string, object> does not necessarily support serialization, arraylist
2 Interim Solution:use up, empty, and then force serialization to local to ensure that the local file size is reduced. 3 Solution: Serialize string to local;hashmap<string, Object>,arraylist object be converted to string, otherwise it will be traversed to convert object to string. What if the activity is destroyed? onsaveinstancestate ()onrestoreinstancestate () User is the only global variable (that is, users information, which is obtained after logging in, and the client should be cached for a long time, unless logged off).

Chapter Three classic scene design

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.