Handheld Express Boot Start

Source: Internet
Author: User

Start an article

Mainly divided into the boot and functional navigation to summarize the two parts

The first part of the image is intuitive to feel



1. Boot part

The boot image is relatively simple, there is a boot image, The picture is placed in the R.layout.start layout file, just beginning with the adaptation problem, then simply put into the linearlayout background attribute, and then use Hnadler to create a sub-thread delay line loads load subsequent jump activity (first boot jump function navigation , non-first jump main interface) boot layout file R.layout.start (good ratio)

<span style= "Font-family:microsoft yahei;font-size:14px;" ><?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/ Res/android "    xmlns:baiduadsdk=" Http://schemas.android.com/apk/res/com.weimeijing.feigeshudi "    android: Layout_width= "Fill_parent"    android:layout_height= "fill_parent"    android:background= "@drawable/startlogo "></LinearLayout></span>


Boot activity file Startactivity.java

Which guide the animation logic section, where the Sharedpreferences method to record whether the first start, to determine whether to enter the boot screen boot

<span style= "Font-family:microsoft yahei;font-size:14px;" >package com;import android.app.activity;import Android.content.intent;import android.content.SharedPreferences ; Import Android.os.bundle;import Android.os.handler;import Com.kaiji.viewpage_activity;import Com.weimeijing.feigeshudi.r;public class StartActivity extends Activity {private final int splash_display_lenght = 2000; Delay two sec @overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.start)///In this display boot picture//Handler thread delay 2 seconds to execute the Run method, display the boot picture Effect new Handler (). postdelayed (New Runnable () {@ overridepublic void Run () {///Run method sharedpreference is an XML file that stores the corresponding user's settings and their state through key-value pairs. Sharedpreferences setting = Getsharedpreferences ("Citigame.ini", 0); Boolean User_first = Setting.getboolean ("First", true); if (User_first) {////load jump to function navigation interface <span style= "COLOR: #009900 ;" >setting.edit (). Putboolean ("First", false). Commit () </span>;intent Intent = new Intent (Startactivity.this, ViewPage_activity.class); StartActivity.this.startActivity (Intent); StartActivity.this.finish ();} else {//non-first load direct jump main interface Intent mainintent = new Intent (startactivity.this,maintabactivity.class); StartActivity.this.startActivity (mainintent); StartActivity.this.finish ();}}}, Splash_display_lenght);}} </span>

1.1, Knowledge points

The code comments are clear, here is the main point to mention

"1" sharedpreferences, its essence is actually XML, but it can be used to store the user's corresponding settings by the key value pair, is a lightweight storage class, sharedpreferences similar to the previous INI configuration file on the Windows system, However, it is divided into multiple permissions and can be shared globally.

Getsharedpreferences (String name, int mode)

Name is the profile name of this component (its own definition, that is, a file name), and when the file does not exist, it is created directly, if it already exists, it is used directly,
Mode is operational, the default mode is 0 or mode_private, and you can use Mode_world_readable and mode_world_writeable
mode is specified as Mode_private, the profile can only be accessed by its own application.
mode is specified as mode_world_readable, the configuration file can be read by other programs, in addition to its own access.
mode is specified as mode_world_writeable, the configuration file can be read and written by other programs, in addition to its own access

"2" sharedpreferences use

Its essence is to store Key-value key-value pairs of data based on XML files, usually to store some simple configuration information. Its storage location is under the/data/data/< package name >/shared_prefs directory.
The Sharedpreferences object itself can only fetch data without supporting storage and modification, and storage modifications are implemented through the editor object.
The steps to implement Sharedpreferences storage are as follows:
First, according to the context to obtain Sharedpreferences object
Second, use the edit () method to get the editor object.
Third, through the editor object store Key-value key value pair data.
Iv. submit data through the commit () method.

"3" Code Analysis

The first run record in the created Sharedpreferences is true thereby entering the first if condition into the function navigation:

Boolean User_first = Setting.getboolean ("First", true);

Then go to the first if judgment and immediately change it to false:

Setting.edit (). Putboolean ("First", false). commit ();

So the next time you start, go straight to the second if and jump directly to the main interface

"4" Handler the effect of enabling sub-threading to achieve delayed loading

The code has been very detailed.


2. Function Navigation section

The main thing is to use Viewpager+layoutinflater (dynamically loaded pages) to achieve

Mainly divided into the following 2 parts:

2.1. Initialization

Mainly through the layoutinflater Dynamic loading navigation page, in order to display the effect here also added some mark Dot, in can see, code comments as follows

Used to get the entire View and return layoutinflater Inflater = Getlayoutinflater ();p ageviews = new arraylist<view> ();p Ageviews.add ( Inflater.inflate (R.layout.activity_viewpage1, null));p Ageviews.add (Inflater.inflate (r.layout.activity_viewpage2 , null));p Ageviews.add (inflater.inflate (r.layout.activity_viewpage3, null));p Ageviews.add (Inflater.inflate ( R.layout.activity_viewpage4, NULL));p Ageviews.add (inflater.inflate (r.layout.activity_viewpage5, null)); Pageviews.add (inflater.inflate (R.layout.activity_viewpage6, NULL));//Small Circle point size is the number of pictures imageviews = new imageview[ Pageviews.size ()];//loads the view from the specified XML file Viewpictures = (viewgroup) inflater.inflate (r.layout.activity_viewpagers, NULL); Viewpager = (Viewpager) Viewpictures.findviewbyid (r.id.guidepagers); viewpoints = (ViewGroup) Viewpictures.findviewbyid (r.id.viewpoints);//Add small dots navigation picture for (int i = 0; i < pageviews.size (); i++) {ImageView = new Imag Eview (Viewpage_activity.this); Imageview.setlayoutparams (new Layoutparams); imageview.setpadding (5, 0, 5, 0) ;//Put small dots into numbersGroup Imageviews[i] = imageview;//The first picture is selected by default, when the first small dot is selected, the other is not if (i = = 0) imageviews[i].setimagedrawable (getresources () . Getdrawable (r.drawable.page_indicator_focused)); Elseimageviews[i].setimagedrawable (GetResources (). getDrawable (r.drawable.page_indicator_unfocused)); /Add imageviews to the small Dot view group Viewpoints.addview (Imageviews[i]);}


2.2, the Pageadapter--pageview adapter

Like a ListView, this thing needs an adaptor.

PageAdapter four functions that must be rewritten:
* Boolean isviewfromobject (View arg0, Object arg1)
* int GetCount ()
* void Destroyitem (ViewGroup container, int position,object Object)
* Object Instantiateitem (viewgroup container, int position)
The specific function is as follows code comment:

Navigate view Adapter Class Navigationadapter extends Pageradapter {//Get required to slide view number @overridepublic int GetCount () {return Pageviews.size ();} @Overridepublic boolean isviewfromobject (View arg0, Object arg1) {return arg0 = = arg1;} 2 things, First: Add the current view to container, second: Returns the current View@overridepublic Object instantiateitem (view container, int position) {( Viewpager) container). AddView (Pageviews.get (position)); return pageviews.get (position);} Destroy each item@overridepublic void Destroyitem (View container, int position, object object) {((Viewpager) container). Removeview (Pageviews.get (position));}}

2.3, Knowledge points

This section requires attention to the following points of knowledge

"1"Viewpager

The Viewpager class provides a new effect for multi-interface switching, with the following characteristics:
<1> currently displays one of the interfaces in a set of interfaces;
<2> when the user slides the interface through the left and right, the current screen displays the current interface and part of the next interface;
<3> after the swipe is finished, the interface automatically jumps to the currently selected interface

"2" Layoutinflater

Dynamically loading pages, note the difference from Findviewbyid ()

Layoutinflater actually finds the XML layout file under res/layout/and instantiates it, which is somewhat similar to Findviewbyid (), which is to find the specific widget controls under the XML layout file (such as button, TextView, etc.)
Role:
1, for a non-loaded or want to dynamically load the interface, you need to use Layoutinflater.inflate () to load;
2, for an already loaded interface, you can use the Activiyt.findviewbyid () method to obtain the interface elements.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Handheld Express Boot Start

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.