Sunday, March 10, 2019

Android开发笔记-ch3.2.15 -3.2.19


3.2.15 Custom View

Refer to custom component, custom view and custom drawing. The most important step in drawing a custom view is to override the onDraw() method. The parameter to onDraw() is a Canvas object that the view can use to draw itself. The Canvas class defines methods for drawing text, lines, bitmaps, and many other graphics primitives. You can use these methods in onDraw() to create your custom user interface (UI). Before you can call any drawing methods, though, it's necessary to create a Paint object.The android.graphics framework divides drawing into two areas:
  • What to draw, handled by Canvas
  • How to draw, handled by Paint.
An example of Joystick view is zerokol.

3.2.16 Check Network connection

Add permission: <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWifi.isConnected()) {
    // Do whatever
}
getNetworkInfo” is deprecated. Below is my code which takes user's setting of using WIFI-only as mWifiOnly.
public Boolean networkAvailable(Context context) {
    ConnectivityManager cm=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
    if(isConnected) {
        if(mWifiOnly) return activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
        else return true;
    }
    return false;
}

3.2.17 Notification

Refer to this. Note, with Lollipop(API23) the icon turns to white, refer to this SO and Android 5.0 changes.

3.2.18 Rich Text in TextView

Refer to so and this. So either use Html.fromHtml() or use SpannableString, ClickableSpan, and ImageSpan.

3.2.19 Find the window contains a view

Refer to this SO and getWindowToken. When using getWindowToken for hiden view or other purpose, refer to this SO and this SO.

0 Comments:

Post a Comment