Android boot page: red dot, sliding, android sliding

Source: Internet
Author: User

Android boot page: red dot, sliding, android sliding

The focus of a simple boot page is that the points under the boot page slide with the page,
See http://blog.csdn.net/wanghao200906/article/details/45536761

It means that three points are fixed and one point slides to calculate the distance between each point, and then the formula is used to calculate how much the sliding little dot moves. Actually, it's easy. The following is the core code.
SmartActivity is a custom baseactivity.
1. Listen for layout loading. After loading, obtain and calculate the distance between the two static points.
2. In the viewpager sliding listening event, the page number and sliding quantity do not know the dynamic point position.

''
Package com. example. wang;

Import java. util. ArrayList;
Import java. util. List;

Import com. example. wang. utils. CacheUtils;

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. View. OnClickListener;
Import android. view. ViewGroup;
Import android. view. ViewTreeObserver;
Import android. view. ViewTreeObserver. OnGlobalLayoutListener;
Import android. widget. Button;
Import android. widget. ImageView;
Import android. widget. LinearLayout;
Import android. widget. RelativeLayout;
Import android. widget. LinearLayout. LayoutParams;

/**
* @ Author wang
* @ Version Creation Time: 11:28:46, January 1, May 6, 2015 Class description
*/
Public class GuideUI extends SmartActivity implements OnPageChangeListener,
OnClickListener {

// Start to experience the Button private Button btnStartExperience; private List <ImageView> imageViewList; // viewpager data private LinearLayout llPointGroup; // The point group private View mSelectPointView; // The selected vertex view object private int basicWidth; // The width between vertices @ Overrideprotected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stub super. onCreate (savedInstanceState); setContentView (R. layout. guide); initView ();} private v Oid initView () {ViewPager mViewPager = getView (R. id. vp_guide); btnStartExperience = getView (R. id. btn_guide_start_experience); llPointGroup = getView (R. id. ll_guide_point_group); mSelectPointView = getView (R. id. select_point); initData (); GuideAdapter mAdapter = new GuideAdapter (); mViewPager. setAdapter (mAdapter); // sliding listener mViewPager. setOnPageChangeListener (this); // The button listens to btnStartExperience. setOnClic KListener (this); // view rendering process: measure-> layout-> draw // listen to the mSelectPointView control layout ViewTreeObserver viewTreeObserver = mSelectPointView. getViewTreeObserver (); // obtains the observer of the view tree and listens to the callback viewTreeObserver for all la S. addOnGlobalLayoutListener (new OnGlobalLayoutListener () {@ Override public void onGlobalLayout () {// only run once to remove the current event from the observer in the view tree mSelectPointView. getViewTreeObserver (). removeGlobalOnLayoutListener (This); // retrieve the width basicWidth = llPointGroup between two points. getChildAt (1 ). getLeft ()-llPointGroup. getChildAt (0 ). getLeft (); System. out. println ("width between points:" + basicWidth) ;}}) ;}private void initData () {int [] imageResIDs = {R. drawable. a, R. drawable. b, R. drawable. c}; ImageView iv; View view; LayoutParams params; // ctrl + 2 loose pause L imageViewList = new ArrayList <ImageView> (); for (int I = 0; I <imageResIDs. leng Th; I ++) {iv = new ImageView (this); iv. setBackgroundResource (imageResIDs [I]); // Add an animated imageViewList. add (iv); // add a vertex view = new View (this) to LinearLayout every cycle based on the number of images; view. setBackgroundResource (R. drawable. point_normal); // the width and height of the view are 10 params = new LayoutParams (20, 20); // gap between each point if (I! = 0) {params. leftMargin = 20;} view. setLayoutParams (params); llPointGroup. addView (view) ;}} class GuideAdapter extends PagerAdapter {@ Override public int getCount () {return imageViewList. size () ;}@ Override public boolean isViewFromObject (View arg0, Object arg1) {return arg0 = arg1 ;}@ Override public void destroyItem (ViewGroup container, int position, Object object) {container. removeView (View) object);} @ Override public Object instantiateItem (ViewGroup container, int position) {ImageView iv = imageViewList. get (position); // 1. add a view object container to ViewPager. addView (iv); // 2. return the currently added view object return iv; }}@ Overridepublic void onClick (View v) {// Save the IS_OPEN_MAIN_PAGER key, and then store a true CacheUtils in the cache. putBoolean (this, WelcomeUI. is_open_main_pager, true); // open the gotoActivity (MainUI. class, true);}/*** page status change */@ Overridepublic void onPageScrollStateChanged (int arg0) {// TODO Auto-generated method stub}/*** page rolling position currently selected is the page positionOffset ratio positionOffsetPixels mobile offset pixel */@ Overridepublic void onPageScrolled (int position, float positionOffset, int positionOffsetPixels) {System. out. println ("position:" + position + ", positionOffset:" + positionOffset); // 20*(1 + 0.5) = 30 moving distance from int leftMargin = (int) (basicWidth * (position + positionOffset); RelativeLayout. layoutParams params = (android. widget. relativeLayout. layoutParams) mSelectPointView. getLayoutParams (); params. leftMargin = leftMargin; mSelectPointView. setLayoutParams (params);}/*** page selected */@ Overridepublic void onPageSelected (int arg0) {if (arg0 = imageViewList. size ()-1) {btnStartExperience. setVisibility (View. VISIBLE);} else {btnStartExperience. setVisibility (View. INVISIBLE );}}

}

Download Code

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.