Up knowledge! 90% Android developers do not know the cold knowledge of Android development

1. YYYY and yyyy is not the same

At December 31, 2019, for example:

yyyy-MM-dd :2019-12-31

YYYY-MM-dd :2020-12-31

I believe that you already know, a lot of heavyweights are written in detail to explain the article, but also directly see the official explanation SimpleDateFormat.

In short: Y refers Week year, represents the year of the week belongs to; we use every day of the year is represented by y.

2. getReadableDatabase not open read-only database

Android in getWritableDatabase () and getReadableDatabase () method can be acquired SQLiteDatabase instance.

But getReadableDatabase () is not opened read-only database, but the first execution getWritableDatabase (), only to open read-only database in case of failure ..

Source as follows:

public synchronized SQLiteDatabase getReadableDatabase() {
    // ...
    try {
        // 执行 getWritableDatabase() , 若出现异常,以只读方式打开数据库
        return getWritableDatabase();
    } catch (SQLiteException e) {
        if (mName == null) throw e;  
    }
    SQLiteDatabase db = null;
    try {
        // ... 
        // 以只读方式打开数据库
        db = SQLiteDatabase.openDatabase(path, mFactory, SQLiteDatabase.OPEN_READONLY);
        // ... 
        mDatabase = db;
        return mDatabase;
    } finally {
        // ... 
    }
}

3. The child can not necessarily update the UI thread

Visit the Android UI is not locked, when multiple threads access is not secure. Therefore, the provisions can only access the UI in the UI thread.

Responsible for checking the thread is ViewRootImpl of checkThread () method:

void checkThread() {
    if (mThread != Thread.currentThread()) {
        throw new CalledFromWrongThreadException(
                "Only the original thread that created a view hierarchy can touch its views.");
    }
}

However, after ViewRootImpl created in onResume () callback. So before onResume (), it is also a sub-thread can update the UI.

Even after ViewRootImpl created, they do not call checkThread (), sub-thread update also does not complain.

However, we do not develop or update the UI when the child thread.

4. View the code is not new id

Android layout file by way of the + @ id, R may be generated in a file corresponding to the value of Int, to ensure the uniqueness of resources at runtime, but the new dynamic code View no id.

If you need to use its id, you can call a View generateViewId () method to generate id (API17 +), instead of generating a specific value or handwriting with a random number.

5. View the return is not necessarily getContext Activity

Activity Activity is in a certain setContentView;

By adding new View, View.inflate, LayoutInflater.inflate this in several ways View, when we preach the mass participation is what context, View of What is Context.

In the mobile phone system version 5.0 or less, and Activity inherited from AppCompatActivity, then getConext method View, the return would not Activity but TintContextWrapper.

6. RemoteViews and have nothing View

RemoteViews provides a basis for the operation of cross-process update, mainly for the development notification bar and desktop widgets.

From the name, I feel should be a remote View. In actual fact, the source code is as follows:

public class RemoteViews implements Parcelable, Filter {
    // ...
}

All in all, RemoteViews is to operate cross-process controls and provide a range of methods of the class.

7. boolean type occupy a few bytes

The actual information in Java boolean indicates one: 1 for true, 0 represents false. However, Java specification data type document does not precisely define the actual memory size Boolean variables.

Its size associated with a virtual machine, you can be sure that would not be one bit.

Java Virtual Machine recommendations are as follows:

  1. boolean types are compiled to use an int, is four byte.
  2. boolean arrays are compiled into byte array type, each member of boolean arrays accounted for a byte

8. RecyclerView layout file can be specified with spanCount layoutManager

RecyclerView layout file can be specified with spanCount layoutManager

<declare-styleable name="RecyclerView">        
    <attr name="layoutManager" format="string" />
    <attr name="android:orientation" />
    <attr name="spanCount" format="integer"/>
    <attr name="reverseLayout" format="boolean" />
    <attr name="stackFromEnd" format="boolean" />
</declare-styleable>

Attr its properties which can be specified layoutManager, spanCount, orientation of. We do not have to set up in the code.

9. 9-patch images are padding of

NinePatchDrawable pattern is a stretchable bitmap, it can be used as the background view. Android will automatically adjust the size of the graphic to fit the content view. Contains an additional 1 pixel border, you must use 9.png extension will save it in the res / drawable / directory of the project.

Line of action:

Left on: the definition of who is allowed to copy the pixels of the picture to fit image.

Right, bottom: place the view area relative to define the content of the picture allowed.

Thus, 9-patch images may include padding, if control is not explicitly set, the picture will be used as padding control padding.

So, sometimes, android: padding = "0dp" The writing was also written.

10. The hardware acceleration switch can not where

Hardware acceleration that is directly dependent on the GPU to achieve accelerated graphics rendering. Since the introduction of GPU rendering not only improve efficiency, but also due to changes in the drawing mechanism, greatly enhancing the efficiency of the interface refresh content changes.

In the beginning Android4.0 hardware acceleration enabled by default, you can manually control the opening and closing:

have to be aware of is:

Window hardware acceleration level can only be opened not close; View class not only closed to open.

Application and Activity Control

Application or Activity node is added in AndroidManifest file

android:hardwareAccelerated="true"

Window control

getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED)

View Control

view.setLayerType(View.LAYER_TYPE_SOFTWARE,null);

Query whether to open hardware acceleration

View.isHardwareAccelerated()
Canvas.isHardwareAccelerated()

11. Use getVisibility () determines whether the user can see not good

getVisibility () determines whether itself is the only display state. But if it's not visible to the parent do?

With isShown () method is more appropriate, first determines the current View In Flag, and then loops to get the parent View, judgment is not visible. As long as one is not visible, it returns false.

Source as follows:

public boolean isShown() {
    View current = this;
    //noinspection ConstantConditions
    do {
        if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
            return false;
        }
        ViewParent parent = current.mParent;
        if (parent == null) {
            return false; // We are not attached to the view root
        }
        if (!(parent instanceof View)) {
            return true;
        }
        current = (View) parent;
    } while (current != null);
    return false;
}

A lot of knowledge comes from various online bigwigs blog, some have forgotten where to see, and this also pay tribute to the bigwigs again!

If this article helpful to you, but also hope you can point a praise Ha ~ ~

Thank you very much Tell me what you can see here, here I have a big brother to share your own collection of finishing Android studying architecture PDF + Video + Interview + document source notes , as well as advanced technical architecture Advanced Brain Mapping, Android interview thematic development information senior advanced schema information these are my leisure will repeatedly read the fine material. In the mind map, each point is equipped with a knowledge of the topic corresponding to the actual project, can effectively help you grasp the knowledge points.

In short we are also here to help enhance learning advanced, but also saves you the time to learn online search data can also be shared with close friends studying together

If you have a need, you can comment on , follow me , click here or add Vx: 15388039515 (Note CSDN, need information)

image

image

image

image

Published 200 original articles · won praise 83 · views 70000 +

Guess you like

Origin blog.csdn.net/weixin_45258969/article/details/104783125