Launcher modification-page tag implementation principle (Code tracking)

Source: Internet
Author: User

When reading this article, you may have seen the 2.1 screen tag (http://gqdy365.iteye.com/blog/897636) on launcher 2.2, using an imageview to display different pictures, take a look at the implementation of the source code in launcher2 in Android. In the drawable folder, you can find a home_arrows_left.xml file with the following content:

<?xml version="1.0" encoding="utf-8"?><level-list xmlns:android="http://schemas.android.com/apk/res/android">    <item android:maxLevel="0" android:drawable="@android:color/transparent" />    <item android:maxLevel="1" android:drawable="@drawable/home_arrows_left_1" />    <item android:maxLevel="2" android:drawable="@drawable/home_arrows_left_2" />    <item android:maxLevel="3" android:drawable="@drawable/home_arrows_left_3" />    <item android:maxLevel="4" android:drawable="@drawable/home_arrows_left_4" /></level-list>


In the launcher. xml file,

<ImageView android:id="@+id/previous_screen"android:layout_width="93dip"                                           android:layout_height="@dimen/button_bar_height"android:layout_gravity="bottom|left"                 android:layout_marginLeft="6dip"android:scaleType="center"                 android:src="@drawable/home_arrows_left"android:onClick="previousScreen"                  android:focusable="true"android:clickable="true" />

This file is referenced, but how is it actually implemented in the code? In the setupviews () method of launcher. Java, there is the code:

 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);        mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);        Drawable previous = mPreviousView.getDrawable();        Drawable next = mNextView.getDrawable();        mWorkspace.setIndicators(previous, next);

Get the image on both sides through code, and then let's take a look at the setindicator () method in workspace. java,

void setIndicators(Drawable previous, Drawable next) {        mPreviousIndicator = previous;        mNextIndicator = next;        previous.setLevel(mCurrentScreen);        next.setLevel(mCurrentScreen);    }

Let's find the definition of mcurrentscreen (73 rows ):

 private int mCurrentScreen;

The defined integer corresponds to maxlevel in home_arrows_left.xml. Different images are displayed on different imageview parameters to mark pages.

In the preceding launcher code, Android: onclick = "previusscreen" registers the imageview click event. In launcher. Java, the corresponding code is as follows:

@SuppressWarnings({"UnusedDeclaration"})    public void previousScreen(View v) {        if (!isAllAppsVisible()) {            mWorkspace.scrollLeft();        }    }    @SuppressWarnings({"UnusedDeclaration"})    public void nextScreen(View v) {        if (!isAllAppsVisible()) {            mWorkspace.scrollRight();        }    }

We continue to track the code of the scrollleft and scrollright () methods in the workspace. Java code:

public void scrollLeft() {        clearVacantCache();        if (mScroller.isFinished()) {            if (mCurrentScreen > 0) snapToScreen(mCurrentScreen - 1);        } else {            if (mNextScreen > 0) snapToScreen(mNextScreen - 1);                    }    }    public void scrollRight() {        clearVacantCache();        if (mScroller.isFinished()) {            if (mCurrentScreen < getChildCount() -1) snapToScreen(mCurrentScreen + 1);        } else {            if (mNextScreen < getChildCount() -1) snapToScreen(mNextScreen + 1);                    }    }

First determine whether to slide to the header, and then jump to the target screen. The snaptoscreen () method code is also in the workspace. The Code is as follows:

private void snapToScreen(int whichScreen, int velocity, boolean settle) {        //if (!mScroller.isFinished()) return;        whichScreen = Math.max(0, Math.min(whichScreen, getChildCount() - 1));                clearVacantCache();        enableChildrenCache(mCurrentScreen, whichScreen);        mNextScreen = whichScreen;        mPreviousIndicator.setLevel(mNextScreen);        mNextIndicator.setLevel(mNextScreen);enableDragLayerChildrenCache();        View focusedChild = getFocusedChild();        if (focusedChild != null && whichScreen != mCurrentScreen &&                focusedChild == getChildAt(mCurrentScreen)) {            focusedChild.clearFocus();        }                final int screenDelta = Math.max(1, Math.abs(whichScreen - mCurrentScreen));        final int newX = whichScreen * getWidth();        final int delta = newX - mScrollX;        int duration = (screenDelta + 1) * 100;        if (!mScroller.isFinished()) {            mScroller.abortAnimation();        }                if (settle) {            mScrollInterpolator.setDistance(screenDelta);        } else {            mScrollInterpolator.disableSettle();        }                velocity = Math.abs(velocity);        if (velocity > 0) {            duration += (duration / (velocity / BASELINE_FLING_VELOCITY))                    * FLING_VELOCITY_INFLUENCE;        } else {            duration += 100;        }        awakenScrollBars(duration);        mScroller.startScroll(mScrollX, 0, delta, 0, duration*2/3);        invalidate();    }

This is the basic process of its implementation.

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.