Android Custom ViewGroup Fast Sliding (4) _android

Source: Internet
Author: User
Tags int size

Last article Custom ViewGroup (3) Address: http://www.jb51.net/article/100618.htm

Code:

Package com.example.libingyuan.horizontallistview.ScrollViewGroup;
Import Android.content.Context;
Import Android.util.AttributeSet;
Import Android.util.DisplayMetrics;
Import android.view.MotionEvent;
Import Android.view.VelocityTracker;
Import Android.view.View;
Import android.view.ViewConfiguration;
Import Android.view.ViewGroup;
Import Android.view.WindowManager;

Import Android.widget.Scroller; /** * Custom ViewGroup * Added acceleration slide/public Class Scrollviewgroup extends ViewGroup {//scrolling compute auxiliary class private scroller Mscrol
  Ler
  The x coordinate of the finger placement is private float Mlastmotionx = 0;
  Screen width private int screenwidth;
  Finger acceleration Auxiliary class private Velocitytracker mvelocitytracker;
  The smallest DP private int mminimumvelocity moved per second;

  The largest DP private int mmaximumvelocity moved per second; /** * When creating objects using the New keyword, call * * @param context Contexts/public Scrollviewgroup {
  , null); /** * When used in XML files, call * * @param context * @param attrs properties: such as Android:layout_widTh= "Wrap_content" * * Public scrollviewgroup (context, AttributeSet attrs) {This (context, attrs, 0); /** * is invoked in an XML file and is invoked with a custom attribute (* * @param context * @param attrs properties: such as android:layout_width= "Wra P_content "* @param defstyleattr ID of the custom attribute/public Scrollviewgroup (context, AttributeSet attrs, int defst
    YLEATTR) {Super (context, attrs, defstyleattr);
  Init (context);  /** * Initialization Method * Initializes the scrolling helper class scroller and calculates the screen width * * @param context/private void Init
    {//Initialize auxiliary class Mscroller = new Scroller (context);
    Get screen width WindowManager manager = (windowmanager) context. Getsystemservice (Context.window_service);
    Displaymetrics outmetrics = new Displaymetrics ();
    Manager.getdefaultdisplay (). Getmetrics (Outmetrics);
    ScreenWidth = Outmetrics.widthpixels;
    Get the minimum and maximum moving distance final viewconfiguration configuration = viewconfiguration.get (context); Mminimumvelocity = COnfiguration.getscaledminimumflingvelocity ();
  Mmaximumvelocity = Configuration.getscaledmaximumflingvelocity (); /** * The method that needs to be overridden when rolling * is used to control scrolling/@Override public void Computescroll () {//To stop if scrolling (mscroller.compute
      Scrolloffset ()) {//scroll to the specified position Scrollto (Mscroller.getcurrx (), Mscroller.getcurry ());
    This sentence must be written, otherwise not real-time refresh postinvalidate ();  /** * Finger touchscreen event Monitor/@Override public boolean ontouchevent (Motionevent event) {//TODO auto-generated
    Method Stub int action = Event.getaction ();
    Gets the x-coordinate of the position where the finger is now in the x = Event.getx (); To determine the trigger time switch (action) {//press event case Motionevent.action_down://Initialize or take acceleration tester Initorrese
        Tvelocitytracker ();
        The tester adds the press event mvelocitytracker.addmovement (events);
        If you stop scrolling, the animation is canceled (that is, the finger presses to stop scrolling) if (!mscroller.isfinished ()) {mscroller.abortanimation ();
    //Get now X coordinate Mlastmotionx = Event.getx ();    Break Move Event Case Motionevent.action_move://Tester Add move Event if (Mvelocitytracker!= null) {mvelocity
        Tracker.addmovement (event);
        //Calculate the offset of the movement float delt = mlastmotionx-x;
        Reset Finger position Mlastmotionx = x;
        Rolling Scrollby ((int) delt, 0);
      Break
        Finger Lift Event Case MOTIONEVENT.ACTION_UP://Tester Add lift event mvelocitytracker.addmovement;
        To add the acceleration test time, here is measured within 1000 milliseconds of the acceleration mvelocitytracker.computecurrentvelocity (1000, mmaximumvelocity);
        Get x direction acceleration Float pxsec = mvelocitytracker.getxvelocity ();
        Get the last child view view LastChild = Getchildat (Getchildcount ()-1); Gets the maximum sliding distance of the slide (the coordinates of the last child's right border minus the screen width) int finalychild = (int) (LASTCHILD.GETX () + lastchild.getwidth ()-SCREENWI
        DTH); If the acceleration of x is greater than the minimum moving distance set by the system, it can be inertial sliding if (Math.Abs (pxsec) > Mminimumvelocity) mscroller.fling (GETSCROLLX (), 0, (int)-pxsec, 0, 0, finalychild, 0, 0);
        If the sliding distance is less than the leftmost (0) of the first control, the rebound to (0,0) point if (GETSCROLLX () < 0) {scrollto (0, 0);
        //If the sliding distance is greater than the maximum sliding distance, slide to the last child view if (GETSCROLLX () >= finalychild) Scrollto (finalychild, 0);
        Refresh interface invalidate ();
        Empty the tester recyclevelocitytracker ();
      Break
    Default:break;
  return true; /** * Create or reuse acceleration tester/private void Initorresetvelocitytracker () {if (Mvelocitytracker = null) {MV
    Elocitytracker = Velocitytracker.obtain ();
    else {mvelocitytracker.clear ();
      }/** * Recovery acceleration tester to prevent memory leaks/private void Recyclevelocitytracker () {if (Mvelocitytracker!= null) {
      Mvelocitytracker.recycle ();
    Mvelocitytracker = null; }} @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {//Reset width High This.setmeas Ureddimension (Measurewidth (Widthmeasurespec, HeightmeasuresPEC), Measureheight (Widthmeasurespec, Heightmeasurespec)); /** * Measuring width */private int measurewidth (int widthmeasurespec, int heightmeasurespec) {//width int size
    Width = Measurespec.getsize (Widthmeasurespec);
    int modewidth = Measurespec.getmode (Widthmeasurespec);
    The width of the parent control (wrap_content) int width = 0;

    int childcount = Getchildcount ();
      Re-measure the width of the child View and the maximum height for (int i = 0; i < ChildCount i++) {View children = getchildat (i);
      Measurechild (Child, Widthmeasurespec, Heightmeasurespec);
      Marginlayoutparams LP = (marginlayoutparams) child.getlayoutparams ();
      int childwidth = child.getmeasuredwidth () + Lp.leftmargin + lp.rightmargin;
    width + + childwidth; return modewidth = = measurespec.exactly?
  Sizewidth:width; /** * Measuring Height * * private int measureheight (int widthmeasurespec, int heightmeasurespec) {//height int Sizeh
    eight = Measurespec.getsize (HEIGHTMEASURESPEC); int modeheight = MeasureSPec.getmode (HEIGHTMEASURESPEC);
    The height (wrap_content) int height = 0 of the parent control;

    int childcount = Getchildcount ();
      Re-measure the width of the child View and the maximum height for (int i = 0; i < ChildCount i++) {View children = getchildat (i);
      Measurechild (Child, Widthmeasurespec, Heightmeasurespec);
      Marginlayoutparams LP = (marginlayoutparams) child.getlayoutparams ();
      int childheight = child.getmeasuredheight () + Lp.topmargin + lp.bottommargin;
    Height + = childheight;
    Height = Height/childcount; return modeheight = = measurespec.exactly?
  Sizeheight:height; /** * Give sub layout set position * * @Override protected void OnLayout (Boolean changed, int l, int t, int r, int b) {int Childleft = 0;//to the left of the space int childwidth;//view width int height = getheight ()//Screen width int childcount = Getchil
      DCount ();//number of view (int i = 0; i < ChildCount; i++) {View child = Getchildat (i); Marginlayoutparams LP = (marginlayoutparams) Child.getlayoutparaMS ();
      Childwidth = child.getmeasuredwidth () + Lp.leftmargin + lp.rightmargin;
      Child.layout (childleft, 0, Childleft + childwidth, height);
    Childleft + = Childwidth; } @Override Public Layoutparams generatelayoutparams (AttributeSet attrs) {return new Marginlayoutparams (getc
  Ontext (), attrs);
 }

}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.