Android custom View-Sliding Control

Source: Internet
Author: User

MainActivity is as follows: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHByZSBjbGFzcz0 = "brush: java;"> package cc. testview4; import cc. testview4.SlideView. switchChangedListener; import android. app. activity; import android. OS. bundle;/*** Demo Description: * Custom slide controls ** reference: * http://blog.csdn.net/lfdfhl/article/details/8195441 ** Note: * Another method is used in CopyOfSlideView: * mainly involves the processing of Touch and GestureDetector of custom controls * For detailed code, please refer to SlideView as a good way and train of thought !!! * This method has a slight BUG. For details, see the code in line 71st */public class MainActivity extends Activity {private SlideView mSlideView; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); init ();} private void init () {mSlideView = (SlideView) findViewById (R. id. slideView); mSlideView. setSwitchChangedListener (new SwitchChangedListenerImpl ();} private class SwitchChangedListenerImpl implements SwitchChangedListener {@ Overridepublic void OnChanged (String info) {System. out. println ("info =" + info );}}}


The SlideView is as follows:

Package cc. testview4; import android. content. context; import android. util. attributeSet; import android. view. gestureDetector; import android. view. layoutInflater; import android. view. motionEvent; import android. view. view; import android. widget. imageView; import android. widget. relativeLayout; public class SlideView extends RelativeLayout {private include; private Context mContext; private View mSlideView; private int dotRawLeft; private int dotRawRight; private int dotRawTop; private int dotRawBottom; private int downX; private int downY; private int dotCurrentLeft; private int dotCurrentRight; private int dotCurrentTop; private int dotCurrentBottom; // The middle ball private ImageView mDotImageView; // The previous private ImageView mPreStepImageView; // next, private ImageView mNextStepImageView; // private ImageView mPreArrowImageView; // The next arrow line private ImageView mNextArrowImageView; // The position on the left of the previous arrow is private int preArrowLeft relative to the left of the parent; private int preArrowWidth; // The position on the right of the previous arrow relative to the left of the parent is private int preArrowRight; // The position on the left of the next arrow relative to the left of the parent is private int nextArrowLeft; // private int nextArrowWidth; private int nextArrowRight; public SlideView (Context context) {super (context); init (context );} public SlideView (Context context, AttributeSet attrs) {super (context, attrs); init (context);} private void init (Context context) {mContext = context; layoutInflater inflater = (LayoutInflater) context. getSystemService (Context. LAYOUT_INFLATER_SERVICE); mSlideView = inflater. inflate (R. layout. slideview, this); mDotImageView = (ImageView) mSlideView. findViewById (R. id. dotImageView); mPreStepImageView = (ImageView) mSlideView. findViewById (R. id. preStepImageView); mNextStepImageView = (ImageView) mSlideView. findViewById (R. id. nextStepImageView); mNextArrowImageView = (ImageView) mSlideView. findViewById (R. id. nextArrowImageView); mPreArrowImageView = (ImageView) mSlideView. findViewById (R. id. preArrowImageView); this. getRawLocation (); // custom component Add the mDotImageView event of the touch listener. setOnTouchListener (new TouchListenerImpl ();} // use the Post method to obtain the private void getRawLocation () {mNextArrowImageView. post (new Runnable () {@ Overridepublic void run () {dotRawLeft = mDotImageView. getLeft (); dotRawRight = mDotImageView. getRight (); dotRawTop = mDotImageView. getTop (); dotRawBottom = mDotImageView. getBottom (); preArrowWidth = mPreArrowImageView. getWidth (); nextArrowWidth = mNextArrowImageView. getWidth () ;}});} private class TouchListenerImpl implements OnTouchListener {@ Overridepublic boolean onTouch (View view, MotionEvent event) {switch (event. getAction () {case MotionEvent. ACTION_DOWN: downX = (int) event. getRawX (); downY = (int) event. getRawY (); break; case MotionEvent. ACTION_MOVE: // slide the finger to the left, dx <0 // slide the finger to the right, dx> 0int distanceX = (int) event. getRawX ()-downX; // The distance from the movement to int distanceY = (int) event. getRawY ()-downY; // The moving distance // prevent cross-border if (distanceX <0) {if (Math. abs (distanceX) >=prearrowwidth) {distanceX =-increment ;}} else {if (distanceX >=nextarrowwidth) {distanceX = nextArrowWidth ;}} dotCurrentLeft = dotRawLeft + distanceX; dotCurrentRight = dotRawRight + distanceX; System. out. println ("dotCurrentLeft =" + dotCurrentLeft + ", dotCurrentRight =" + dotCurrentRight); if (distanceX <0 & Math. abs (distanceX) = preArrowWidth) {mSwitchChangedListener. onChanged ("previous");} if (distanceX> 0 & distanceX = nextArrowWidth) {mSwitchChangedListener. onChanged ("Next");} mDotImageView. layout (dotCurrentLeft, dotRawTop, dotCurrentRight, dotRawBottom); break; case MotionEvent. ACTION_UP: mDotImageView. layout (dotRawLeft, dotRawTop, dotRawRight, dotRawBottom); break; default: break;} return true ;}// callback interface public interface SwitchChangedListener {public void OnChanged (String info );} public void setSwitchChangedListener (SwitchChangedListener switchChangedListener) {this. mSwitchChangedListener = switchChangedListener ;}}

CopyOfSlideView is as follows:

Package cc. testview4; import android. content. context; import android. util. attributeSet; import android. view. gestureDetector; import android. view. layoutInflater; import android. view. motionEvent; import android. view. view; import android. widget. imageView; import android. widget. relativeLayout; import android. widget. toast; public class CopyOfSlideView extends RelativeLayout {private SwitchChangedListener listener; private Context mContext; private View mSlideView; private int dotRawLeft; private int dotRawRight; private int dotRawTop; private int dotRawBottom; // private ImageView mDotImageView; // private ImageView mPreStepImageView in the previous step; // private ImageView mNextStepImageView in the next step; // private ImageView mPreArrowImageView In the arrows in the previous step; // private ImageView mNextArrowImageView; // private int preArrowLeft on the left of the previous arrow relative to the left of the parent; // private int preArrowRight on the right of the previous arrow relative to the left of the parent; // The position on the left of the next arrow relative to the left of the parent is private int nextArrowLeft; // The position on the right of the next arrow relative to the left of the parent is private int nextArrowRight; // The distance from the private int sliddingSumX = 0; private RelativeLayout extends; private GestureDetector mGestureDetector; private SlideViewGestureListener extends; public CopyOfSlideView (Context context) {super (context ); init (context);} public CopyOfSlideView (Context context, AttributeSet attrs) {super (context, attrs); init (context);} private void init (Context context) {mContext = context; LayoutInflater inflater = (LayoutInflater) context. getSystemService (Context. LAYOUT_INFLATER_SERVICE); mSlideView = inflater. inflate (R. layout. slideview, this); mDotImageView = (ImageView) mSlideView. findViewById (R. id. dotImageView); mPreStepImageView = (ImageView) mSlideView. findViewById (R. id. preStepImageView); mNextStepImageView = (ImageView) mSlideView. findViewById (R. id. nextStepImageView); mNextArrowImageView = (ImageView) mSlideView. findViewById (R. id. nextArrowImageView); mPreArrowImageView = (ImageView) mSlideView. findViewById (R. id. preArrowImageView); mSlideViewRelativeLayout = (RelativeLayout) findViewById (R. id. slideView_RelativeLayout); this. getRawLocation (); mSlideViewGestureListener = new SlideViewGestureListener (); mGestureDetector = new GestureDetector (mContext, mSlideViewGestureListener ); // Add a Touch listening event to the custom widget // use the following sentence to implement Touch listening for the entire custom control. Normally, the mSlideView is displayed when the slider slides. setOnTouchListener (new TouchListenerImpl (); // use the following sentence to implement Touch listening for the slider. The slider slides jitters due to unknown causes. // mDotImageView. setOnTouchListener (new TouchListenerImpl ();} // use the Post method to obtain the private void getRawLocation () {mNextArrowImageView. post (new Runnable () {@ Overridepublic void run () {dotRawLeft = mDotImageView. getLeft (); dotRawRight = mDotImageView. getRight (); dotRawTop = mDotImageView. getTop (); dotRawBottom = mDotImageView. getBottom (); preArrowLeft = mPreArrowImageView. getLeft (); preArrowRight = mPreArrowImageView. getRight (); nextArrowLeft = mNextArrowImageView. getLeft (); nextArrowRight = mNextArrowImageView. getRight () ;}});} private class TouchListenerImpl implements OnTouchListener {@ Overridepublic boolean onTouch (View view, MotionEvent event) {switch (event. getAction () {case MotionEvent. ACTION_UP: // if (Math. abs (sliddingSumX) <(preArrowRight-preArrowLeft) * 4/5) {mDotImageView. layout (dotRawLeft, dotRawTop, dotRawRight, dotRawBottom); sliddingSumX = 0;} else {if (mDotImageView. getLeft () <dotRawLeft) {Toast. makeText (mContext, "previous", 0 ). show (); mSwitchChangedListener. onChanged ("reached the previous step"); mDotImageView. layout (dotRawLeft, dotRawTop, dotRawRight, dotRawBottom); sliddingSumX = 0;} else {Toast. makeText (mContext, "Next", 0 ). show (); mSwitchChangedListener. onChanged ("to the next step"); mDotImageView. layout (dotRawLeft, dotRawTop, dotRawRight, dotRawBottom); sliddingSumX = 0 ;}} break; default: break;}/*** note: * The OnTouchListener transfers the move and down events to mGestureDetector. * Only the up event is handled in OnTouchListener-The dotImageView is displayed after the finger is lifted. */return mGestureDetector. onTouchEvent (event) ;}/// gesture of the custom control // Note: Return true in the corresponding method, indicating that the private class SlideViewGestureListener implements GestureDetector has been consumed. onGestureListener {@ Overridepublic boolean onDown (MotionEvent e) {return true;} // execute this method when the custom control slides @ Overridepublic boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {sliddingSumX = sliddingSumX + (int) distanceX; // when displaying dotImageView again, you only need to change the value in the X axis direction. // pull to the left and increase X; pull to the right, x decrease // Note: pull to the right, distanceX is negative, pull to the left, distanceX is positive. // so it is not: rawLeft + sumX, rawTop, rawRight + sumX, rawBottom // but it should be: rawLeft-sumX, rawTop, rawRight-sumX, rawBottomif (dotRawLeft-sliddingSumX> = (preArrowLeft-(mDotImageView. getWidth () * 0.5) & dotRawRight-sliddingSumX <= nextArrowRight + (mDotImageView. getWidth () * 0.5) {System. out. println ("=> distanceX =" + distanceX + ", sliddingSumX =" + sliddingSumX); mDotImageView. layout (dotRawLeft-sliddingSumX, dotRawTop, dotRawRight-sliddingSumX, dotRawBottom);} else {System. out. println ("the two ends are exceeded");} return true ;}@ Overridepublic boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {return false ;} @ Overridepublic void onLongPress (MotionEvent e) {}@ Overridepublic void onShowPress (MotionEvent e) {}@ Overridepublic boolean onSingleTapUp (MotionEvent e) {return false ;}} // callback interface public interface SwitchChangedListener {public void OnChanged (String info);} public void setSwitchChangedListener (SwitchChangedListener switchChangedListener) {this. mSwitchChangedListener = switchChangedListener ;}}


Main. xml is as follows:

     
  
 


Slideview. xml is as follows:

 
     
      
      
      
      
  
 


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.