When we use ViewFlipper, we lament that ViewFlipper is easy to use. On the other hand, we often lament that ViewFlipper provides too few interface methods and that many common effects are not good, it is difficult to show your skills. This article will introduce the architecture of ViewFlipper and its working principles in detail. I believe that after reading this article, you will find that ViewFlipper can display a lot of results, you can also customize your own ViewFlipper as needed.
Class inheritance relationship diagram:The class diagram shows the original
Actually
ViewFlipper
public void setFlipInterval(int milliseconds) { mFlipInterval = milliseconds; }
public void stopFlipping() { mStarted = false; updateRunning(); }
private void updateRunning() { boolean running = mVisible && mStarted && mUserPresent; if (running != mRunning) { if (running) { showOnly(mWhichChild); Message msg = mHandler.obtainMessage(FLIP_MSG); mHandler.sendMessageDelayed(msg, mFlipInterval); } else { mHandler.removeMessages(FLIP_MSG); } mRunning = running; } if (LOGD) { Log.d(TAG, "updateRunning() mVisible=" + mVisible + ", mStarted=" + mStarted + ", mUserPresent=" + mUserPresent + ", mRunning=" + mRunning); } }
By looking at these three methods, we can know whether
private final Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == FLIP_MSG) { if (mRunning) { showNext(); msg = obtainMessage(FLIP_MSG); sendMessageDelayed(msg, mFlipInterval); } } } };
If yes
The main methods in ViewAnimator are as follows:
public void showNext() { setDisplayedChild(mWhichChild + 1); }
public void setDisplayedChild(int whichChild) { mWhichChild = whichChild; if (whichChild >= getChildCount()) { mWhichChild = 0; } else if (whichChild < 0) { mWhichChild = getChildCount() - 1; } boolean hasFocus = getFocusedChild() != null; // This will clear old focus if we had it showOnly(mWhichChild); if (hasFocus) { // Try to retake focus if we had it requestFocus(FOCUS_FORWARD); } }
These two methods calculate the next
void showOnly(int childIndex) { final int count = getChildCount(); for (int i = 0; i < count; i++) { final View child = getChildAt(i); final boolean checkForFirst = (!mFirstTime || mAnimateFirstTime); if (i == childIndex) { if (checkForFirst && mInAnimation != null) { child.startAnimation(mInAnimation); } child.setVisibility(View.VISIBLE); mFirstTime = false; } else { if (checkForFirst && mOutAnimation != null && child.getVisibility() == View.VISIBLE) { child.startAnimation(mOutAnimation); } else if (child.getAnimation() == mInAnimation) child.clearAnimation(); child.setVisibility(View.GONE); } } }
The main task of this method is to display
In fact
Have you found this?
Today, the next article will inherit ViewFlipper to rewrite an Image Browsing component. The main functions of the component are: 1, which can automatically play images; 2, you can slide your fingers to browse images. You can search for similar examples on the Internet. However, the slide action on your fingers does not change, but the animation starts after the slide on your fingers. (Poor experience) the components have been written, but today there is no time to explain the implementation principle. If you need a friend, please leave a mailbox and I will send it to you.
Reprint please explain the source: http://blog.csdn.net/ff20081528