In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces Android how to achieve the effect of right sliding switch Activity, the article introduces in great detail, has a certain reference value, interested friends must read it!
It brings an effect of sliding to the right to switch Activity. Activtiy moves with the movement of fingers. This effect is relatively rare in Android applications, but it is more common in IOS. For example, applications such as "NetEase News", "Food Jie", "Taobao" and so on use this effect, while "Zhihu" in Android application also uses this effect of sliding switching Activity. However, I found that "Taobao" did not move with the gesture, but only captured the sliding gesture, and then produced a smooth switching interface animation effect, which is very easy to achieve in Android. Many sliding switching Activity Demo on the Internet seem to have this effect. If you want to achieve a slide similar to "NetEase News" sliding with the gesture, it seems to be a little more complicated. I was very interested when I saw this effect of "NetEase News" in IOS, and then some friends in the group asked me how to achieve the effect of sliding switching similar to "Zhihu". I also went to a "Zhihu". In the previous implementation, I encountered some bottlenecks, so I shelved it there. Today, I accidentally saw a transparent background for Activity, so it dawned on me. Really inspiration from the moment, can not be forced ah, and then I will achieve this effect, to share with you, I hope to give you a little help with this need.
I wonder if you are familiar with the class Scroller and the use of scrollBy () and scrollTo () of View. I introduced the sliding implementation principle of the Scroller class before. Android takes you to analyze the scrolling implementation principle of Scroller from the point of view of source code. There are also scrollBy () and scrollTo () methods. Students who do not understand can take a look. This is of great help to achieve this effect. Friends who understand scrollBy () and scrollTo () should know. If you want to scroll a View (for example, Button), we directly call the scrollBy () method of the View (Button), not the View (Button), but the contents of the View (the text above the Button). So if we want to make the View scroll as a whole, we need to call the scrollBy () method on the parent layout of its View. Back to this article, if we want to scroll an Activity, We need to scroll the parent layout of the top-level layout of the Activity layout file
For example, the following XML layout file
If we scroll the LinearLayout, we can not achieve the desired effect, but can only scroll the contents of the LinearLayout or the child View, so we need to get the parent layout using the getParent () method of LinearLayout. In fact, the Android system will nest a FrameLayout on the outermost layer of our layout file, so we just scroll the FrameLayout.
Now that we understand the principle of the implementation, let's write the code. First, create a new android project and name it SildingFinish.
Since our requirements may not provide the effect of this sliding switch in an interface, we should extract this part of the sliding logic. Here I write a custom layout SildingFinishLayout that extends RelativeLayout. First, let's look at its code.
Package com.example.view;import android.content.Context;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;import android.view.ViewConfiguration;import android.view.ViewGroup;import android.widget.AbsListView;import android.widget.RelativeLayout;import android.widget.ScrollView;import android.widget.Scroller / * customize the sliding RelativeLayout, which is similar to the sliding deletion effect of IOS. When we want to use * this feature, we need to set the top-level layout of the Activity to SildingFinishLayout. * then you need to call the setTouchView () method to set the parent layout of the View * * @ author xiaanming * * @ blog http://blog.csdn.net/xiaanming * * / public class SildingFinishLayout extends RelativeLayout implements OnTouchListener {/ * SildingFinishLayout layout * / private ViewGroup mParentView / * View * / private View touchView; / * minimum sliding distance for handling sliding logic * / private int mTouchSlop; / * Press the X coordinate of the point * / private int downX; / * press the Y coordinate of the point * / private int downY / * * temporary storage X coordinates * / private int tempX; / * sliding class * / private Scroller mScroller; / * width of SildingFinishLayout * / private int viewWidth; / * whether the record is sliding * / private boolean isSilding Private OnSildingFinishListener onSildingFinishListener; private boolean isFinish Public SildingFinishLayout (Context context, AttributeSet attrs) {this (context, attrs, 0);} public SildingFinishLayout (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); mTouchSlop = ViewConfiguration.get (context). GetScaledTouchSlop () MScroller = new Scroller (context);} @ Override protected void onLayout (boolean changed, int l, int t, int r, int b) {super.onLayout (changed, l, t, r, b); if (changed) {/ / get the parent layout of SildingFinishLayout's layout mParentView = (ViewGroup) this.getParent (); viewWidth = this.getWidth () }} / * set OnSildingFinishListener, and in the onSildingFinish () method, finish Activity * * @ param onSildingFinishListener * / public void setOnSildingFinishListener (OnSildingFinishListener onSildingFinishListener) {this.onSildingFinishListener = onSildingFinishListener;} / * set Touch's View * * @ param touchView * / public void setTouchView (View touchView) {this.touchView = touchView TouchView.setOnTouchListener (this);} public View getTouchView () {return touchView;} / * scroll out of the interface * / private void scrollRight () {final int delta = (viewWidth + mParentView.getScrollX ()) / call the startScroll method to set some scrolling parameters. We call scrollTo in the computeScroll () method to scroll item mScroller.startScroll (mParentView.getScrollX (), 0,-delta + 1, 0, Math.abs (delta)); postInvalidate () } / * scroll to start position * / private void scrollOrigin () {int delta = mParentView.getScrollX (); mScroller.startScroll (mParentView.getScrollX (), 0,-delta, 0, Math.abs (delta)); postInvalidate () Is the View of} / * touch AbsListView, such as ListView, GridView and its subclasses * * @ return * / private boolean isTouchOnAbsListView () {return touchView instanceof AbsListView? Is the view of true: false;} / * touch ScrollView or its subclass * * @ return * / private boolean isTouchOnScrollView () {return touchView instanceof ScrollView? True: false;} @ Override public boolean onTouch (View v, MotionEvent event) {switch (event.getAction ()) {case MotionEvent.ACTION_DOWN: downX = tempX = (int) event.getRawX (); downY = (int) event.getRawY (); break; case MotionEvent.ACTION_MOVE: int moveX = (int) event.getRawX () Int deltaX = tempX-moveX; tempX = moveX; if (Math.abs (moveX-downX) > mTouchSlop & & Math.abs ((int) event.getRawY ()-downY) < mTouchSlop) {isSilding = true / / if touchView is AbsListView, / / cancel the click event of item when the finger is swiped, otherwise we slide with the occurrence of item click event if (isTouchOnAbsListView ()) {MotionEvent cancelEvent = MotionEvent.obtain (event) CancelEvent .setAction (MotionEvent.ACTION_CANCEL | (event.getActionIndex ()) = 0 & & isSilding) {mParentView.scrollBy (deltaX, 0) / / shielding ListView ScrollView's own sliding event if (isTouchOnScrollView () | | isTouchOnAbsListView ()) {return true;}} break; case MotionEvent.ACTION_UP: isSilding = false; if (mParentView.getScrollX ()) during sliding
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.