Android: ViewGroup
Slide with hand
Many developers do not know much about the layout and hand sliding. Here is an example to show a slide display of RelativeLayout.
Principle
Whether it is sliding with your hands or popping up an animation, it is essentially modifying the position of the View or ViewGroup, that is, the setX () setY () method.
Slide with hand
Sliding with the hand means that when a user slides on the screen, a piece of layout slides as the finger slides. Therefore, its implementation principle is to dynamically obtain the finger sliding distance in the onTouch event, and then modify the view position.
Pop-up animation
When sliding with the hand, it is possible that the user will just slide out the part of the View and then let it go. For better results, we will display the view in an animated form according to the user's sliding direction. Therefore, it uses ValueAnimator to dynamically modify the view location.
Codexml Layout
Our goal is to make the RelativeLayout with the id rl_left slide out from the left side of the screen and hide it to the left side of the screen.
How to control the slide and animation of MianActivity: slideToShow slideToHide onTouch
Package com.example.net. mobctrl. ottotest; import java. util. arrayList; import java. util. list; import android. animation. valueAnimator; import android. animation. valueAnimator. animatorUpdateListener; import android. app. activity; import android. OS. bundle; import android. view. motionEvent; import android. view. view; import android. view. view. onClickListener; import android. view. animation. decelerateInterpolator; import android. widget. adapterView; import android. widget. adapterView. onItemClickListener; import android. widget. arrayAdapter; import android. widget. listView; import android. widget. relativeLayout; import android. widget. textView;/***** @ author Zheng Haibo **/public class MainActivity extends Activity {private RelativeLayout rl_left; private ListView listView; private TextView tvShow; private int rlWidth; // The layout width is private static final int MAX_OFFSET = 5; // five pixel errors. If the slide is smaller than five pixels, no animation private float downX is displayed; // point private float viewXdown at press time; // The position private boolean lastSlidePull = false when the View is pressed; // The direction private float maxOffset = 0 for the last sliding; // maximum sliding distance @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); System. out. println (debug: onCreate); setContentView (R. layout. activity_main); findViewById (R. id. btn_1 ). setOnClickListener (new OnClickListener () {@ Override public void onClick (View arg0) {slideToShow () ;}}); findViewById (R. id. btn_2 ). setOnClickListener (new OnClickListener () {@ Override public void onClick (View arg0) {slideToHide () ;}}); tvShow = (TextView) findViewById (R. id. TV _show); initLeftView ();} private void initLeftView () {rl_left = (RelativeLayout) findViewById (R. id. rl_left); rlWidth = getResources (). getDimensionPixelSize (R. dimen. rl_left_w); rl_left.setX (-rlWidth); // move the location of rl_left to the left of the mobile phone screen. // initialize the View of RelativeLayout. In this example, ListView = (listView) rl_left.findViewById. id. lv_test); listView. setAdapter (new ArrayAdapter
(This, android. R. layout. simple_expandable_list_item_1, getItemData (); listView. setOnItemClickListener (itemListener);}/*** fill false data ** @ return */private List
GetItemData () {List
List = new ArrayList
(); For (int I = 0; I <16; I ++) {list. add (item + I);} return list;} private OnItemClickListener itemListener = new OnItemClickListener () {@ Override public void onItemClick (AdapterView
Arg0, View arg1, int arg2, long arg3) {slideToHide (); tvShow. setText (String. format (you click item % s !, Arg2) ;}};/*** use ValueAnimator to insert rl_left into the interface as an animation */private void slideToShow () {float startX = rl_left.getX (); valueAnimator valueAnimator = ValueAnimator. ofInt (int) startX, 0); valueAnimator. addUpdateListener (new AnimatorUpdateListener () {@ Override public void onAnimationUpdate (ValueAnimator animation) {int offset = (Integer) animation. getAnimatedValue (); rl_left.setX (offset) ;}}); valu EAnimator. setInterpolator (new DecelerateInterpolator (); float fraction = Math. abs (startX/rlWidth); valueAnimator. setDuration (long) (600 * fraction); valueAnimator. start ();}/*** use ValueAnimator to bring up rl_left as an animation */private void slideToHide () {float startX = rl_left.getX (); ValueAnimator valueAnimator = ValueAnimator. ofInt (int) startX,-rlWidth); valueAnimator. addUpdateListener (new Animator UpdateListener () {@ Override public void onAnimationUpdate (ValueAnimator animation) {int offset = (Integer) animation. getAnimatedValue (); rl_left.setX (offset) ;}}); valueAnimator. setInterpolator (new DecelerateInterpolator (); float fraction = Math. abs (rlWidth + startX)/rlWidth); valueAnimator. setDuration (long) (400 * fraction); valueAnimator. start () ;}@ Override public boolean onTouchEvent (MotionEvent) {float x = event. getX (); switch (event. getAction () {case MotionEvent. ACTION_DOWN: this. downX = x; this. viewXdown = rl_left.getX (); break; case MotionEvent. ACTION_MOVE: float offset = (event. getX ()-downX); // The sliding distance from float posX = viewXdown + offset; // calculate the possible position maxOffset = maxOffset> Math. abs (offset )? MaxOffset: Math. abs (offset); if (offset> 0) {// pull to show rl_left.setX (posX <0? PosX: 0); if (posX> = 0) {// update the value of downX instead of hand. downX + = posX;} lastSlidePull = true;} else {// push to hide rl_left.setX (posX>-rlWidth? PosX:-rlWidth); if (posX <=-rlWidth) {// this. downX + = (posX + rlWidth);} lastSlidePull = false;} break; case MotionEvent. ACTION_UP: if (maxOffset <MAX_OFFSET) {// prevents jitter return super. onTouchEvent (event) ;}// use an animation to slide to the specified position if (lastSlidePull) {slideToShow () ;}else {slideToHide () ;} break; default: break ;} return super. onTouchEvent (event) ;}@ Override protected void onDestroy () {super. onDestroy ();}}