Welcome to the page + go to the classic cases page.

Source: Internet
Author: User

Welcome to the page + go to the classic cases page.

View the boot Interface


First look at the layout of the welcome page (animation effect)


Activity_welcome.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:id="@+id/rl_root"    android:background="@drawable/splash_bg_newyear" >    <ImageView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:src="@drawable/splash_horse_newyear" /></RelativeLayout>


Let's look at the activity on the welcome page.

WelcomeActivity. java

Package com. example. bjnews. activity; import com. example. bjnews. r; import com. example. bjnews. util. SPUtil; import android. OS. bundle; import android. util. log; import android. view. animation. alphaAnimation; import android. view. animation. animation; import android. view. animation. animation. animationListener; import android. view. animation. animationSet; import android. view. animation. rotateAnimation; import android. view. animation. scaleAnimation; import android. widget. relativeLayout; import android. widget. toast; import android. app. activity; import android. content. intent; public class WelcomeActivity extends Activity {private RelativeLayout rl_root; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_welcome); init ();} private void init () {rl_root = (RelativeLayout) findViewById (R. id. rl_root); // rotate RotateAnimation rotateAnimation = new RotateAnimation (0,360, Animation. RELATIVE_TO_SELF, 0.5f, Animation. RELATIVE_TO_SELF, 0.5f); rotateAnimation. setDuration (2000); rotateAnimation. setFillAfter (true); // scale ScaleAnimation scaleAnimation = new ScaleAnimation (0, 1, 0, 1, Animation. RELATIVE_TO_SELF, 0.5f, Animation. RELATIVE_TO_SELF, 0.5f); scaleAnimation. setDuration (2000); scaleAnimation. setFillAfter (true); // transparency AlphaAnimation alphaAnimation = new AlphaAnimation (0.0f, 1.0f); alphaAnimation. setDuration (2000); alphaAnimation. setFillAfter (true); // Add the animation AnimationSet animationSet = new AnimationSet (false); animationSet. addAnimation (rotateAnimation); animationSet. addAnimation (scaleAnimation); animationSet. addAnimation (alphaAnimation); rl_root.startAnimation (animationSet); // animation listening event animationSet. setAnimationListener (new MyAnimation ();} class MyAnimation implements AnimationListener {@ Overridepublic void onAnimationEnd (Animation animation) {// whether the main interface has been entered (not entered by default) boolean isEnterMain = SPUtil. getInstance (WelcomeActivity. this ). getboolean ("isEnterMain", false); Log. e ("TAG", "isEnterMain" + isEnterMain); if (isEnterMain) {// enter the main interface startActivity (new Intent (WelcomeActivity. this, MainActivity. class); Log. e ("TAG", "Main");} else {// not entered, included in the Wizard interface Log. e ("TAG", "guide"); startActivity (new Intent (WelcomeActivity. this, GuideActivity. class);} finish () ;}@ Overridepublic void onAnimationRepeat (Animation animation) {}@ Overridepublic void onAnimationStart (Animation animation ){}}}
In addition, a tool class is provided for the object (used to save whether it is the first time you enter the application)

SPUtil. java

Package com. example. bjnews. util; import android. content. context; import android. content. sharedPreferences; import android. content. sharedPreferences. editor; public class SPUtil {// constructor private SPUtil () {}// private, static instance private static SPUtil instance = new SPUtil (); // obtain the instance public static SPUtil getInstance (Context context) {if (sp = null) {sp = context. getSharedPreferences ("atguigu", Context. MODE_PRIVATE);} return instance;} private static SharedPreferences sp; // Method for saving data public void put (String key, Object defValue) {Editor edit = sp. edit (); if (defValue instanceof Boolean) {edit. putBoolean (key, (Boolean) defValue);} if (defValue instanceof String) {edit. putString (key, (String) defValue);} if (defValue instanceof Integer) {edit. putInt (key, (Integer) defValue);} edit. commit () ;}// obtain the public String getString (String key, String defValue) {return sp. getString (key, defValue);} public int getInt (String key, int defValue) {return sp. getInt (key, defValue);} public boolean getboolean (String key, boolean defValue) {return sp. getBoolean (key, defValue );}}

Next, start the operations on the boot interface.

First view Layout

Activity_guide.xml

<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent"> <android. support. v4.view. viewPager android: id = "@ + id/viewpager" android: layout_width = "fill_parent" android: layout_height = "fill_parent"> </android. support. v4.view. viewPager> <Button android: id = "@ + id/btn_enterMain" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignParentBottom = "true" android: layout_centerHorizontal = "true" android: layout_marginBottom = "100dp" android: background = "@ drawable/enter_main_button_selector_bg" android: text = "start to experience" android: textColor = "@ drawable/enter_main_button_text_selector" android: visibility = "gone"/> <RelativeLayout android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignParentBottom = "true" android: layout_centerHorizontal = "true" android: layout_marginBottom = "80dp"> <LinearLayout android: id = "@ + id/ll_point_group" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: orientation = "horizontal"> </LinearLayout> <View android: id = "@ + id/red_point" android: layout_width = "10dp" android: layout_height = "10dp" android: background = "@ drawable/point_red"/> </RelativeLayout>
Four xml files are also required for the boot interface (switch to experience the effect of button pressing, and three status switching dots)
Enter_main_button_selector_bg.xml

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" >    <item android:state_checked="true" android:drawable="@drawable/button_red_pressed"></item>    <item android:state_checked="false" android:drawable="@drawable/button_red_normal"></item></selector>

Enter_main_button_text_selector.xml

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" >    <item android:state_checked="true" android:color="@android:color/black"></item>    <item android:state_checked="false" android:color="@android:color/white"></item></selector>

Point_normal.xml

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">    <solid android:color="#55000000"/></shape>
Point_red.xml

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">    <solid android:color="#ff0000"/></shape>

The wizard code is worth noting.

GuideActivity. java

Package com. example. bjnews. activity; import java. util. arrayList; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. support. v4.view. pagerAdapter; import android. support. v4.view. viewPager; import android. support. v4.view. viewPager. onPageChangeListener; import android. view. view; import android. view. viewGroup; import android. view. viewTreeObserver. onGlobalLayoutLi Stener; import android. widget. button; import android. widget. imageView; import android. widget. linearLayout; import android. widget. linearLayout. layoutParams; import android. widget. relativeLayout; import com. example. bjnews. r; import com. example. bjnews. util. SPUtil; public class GuideActivity extends Activity {private ViewPager viewpager; // three sliding pages, private ArrayList <ImageView> imageViews; // The image set corresponding to the three sliding pages, private Button btn_enterMain; // The start experience Button private LinearLayout ll_point_group; // The three dots shown below are private View red_point; // The highlighted dot private int leftMax on the current page; // The distance before the two origins @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_guide); // obtain the vertex set ll_point_group = (LinearLayout) findViewById (R. id. ll_point_group); // obtain the highlighted red_point = findViewById (R. id. Red_point); // obtain viewpagerviewpager = (ViewPager) findViewById (R. id. viewpager); // get the button (start experience) btn_enterMain = (Button) findViewById (R. id. btn_enterMain); // create the collection imageViews = new ArrayList <ImageView> (); // prepare the data int [] ids = {R. drawable. guide_1, R. drawable. guide_2, R. drawable. guide_3}; for (int I = 0; I <ids. length; I ++) {ImageView imageView = new ImageView (this); // create an image object imageView. setBackgroundResourc E (ids [I]); // sets the background imageViews for the image. add (imageView); // add an image View point = new View (this); // add the following Punctuation point-create click LayoutParams params = new LayoutParams (10, 10 ); // create layout parameters // except for 0th vertices, The rest must be 10 pixels away from the left if (I! = 0) {params. leftMargin = 10;} point. setBackgroundResource (R. drawable. point_normal); // Add the background image point. setLayoutParams (params); // set the layout parameter ll_point_group.addView (point); // Add the indicator click} // set the adapter viewpager. setAdapter (new viewpagerAdapter (); // listen to red_point.getViewTreeObserver () when the onLayout method is executed (). addOnGlobalLayoutListener (new OnGlobalLayoutListener () {@ Overridepublic void onGlobalLayout () {// cancel registration listening-because the child will also call red_point.getViewTreeObserver (). removeGlobalOnLayoutListener (this); // leftMax = ll_point_group.getChildAt (1 ). getLeft ()-ll_point_group.getChildAt (0 ). getLeft () ;}});/*** set page change listener */viewpager. setOnPageChangeListener (new OnPageChangeListener () {@ Overridepublic void onPageSelected (int position) {// only the third page shows the enter button if (position = 2) {btn_enterMain.setVisibility (View. VISIBLE);} else {btn_enterMain.setVisibility (View. GONE) ;}}/** display of sliding percentage on the screen */@ Overridepublic void onPageScrolled (int position, float positionOffset, int positionOffsetPixels) {// calculate the distance to slide = spacing * Percentage of sliding on the screen int distance = (int) (leftMax * (positionOffset + position); RelativeLayout. layoutParams params = new RelativeLayout. layoutParams (10, 10); params. leftMargin = distance; red_point.setLayoutParams (params) ;}@ Overridepublic void onPageScrollStateChanged (int arg0 ){}}); /*** Add a click event for the start experience button */btn_enterMain.setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {// mark it as trueSPUtil. getInstance (GuideActivity. this ). put ("isEnterMain", true); // enter startActivity (new Intent (GuideActivity. this, MainActivity. class); // kill yourself finish () ;}});} class viewpagerAdapter extends PagerAdapter {@ Overridepublic int getCount () {return imageViews. size () ;}@ Overridepublic boolean isViewFromObject (View arg0, Object arg1) {return (arg0 = arg1) ;}@ Overridepublic void destroyItem (ViewGroup container, int position, Object object) {container. removeView (View) object);} @ Overridepublic Object instantiateItem (ViewGroup container, int position) {ImageView imageView = imageViews. get (position); container. addView (imageView); return imageView ;}}}


In addition, there is also a main interface for testing (there is nothing in the layout file)

Activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" ></RelativeLayout>

MainActivity. java

package com.example.bjnews.activity;import com.example.bjnews.R;import android.app.Activity;import android.os.Bundle;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}}



Related Article

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.