Android implements automatic scrolling gallary control effect _android

Source: Internet
Author: User

This example describes the Gallary control that Android implements to automatically scroll. Share to everyone for your reference. as follows:

Import Java.util.Timer;
Import Java.util.TimerTask;
Import Android.content.Context;
Import Android.os.Handler;
Import Android.util.AttributeSet;
Import Android.util.Log;
Import android.view.KeyEvent;
Import android.view.MotionEvent;
Import Android.widget.Gallery; The values in the public class Homegallery extends Gallery {/** * Limit the maximum length of each scroll, and the width of the picture is 480PX. Here set the 600 effect better.
   The larger the value, the greater the length of the scroll. * That is, there will be one scrolling across multiple image.
  Here limit the length after each scroll can only cross one image/private static final int timeranimation = 1;
  private static final int time = 2000; Private final Handler Mhandler = new Handler () {public void Handlemessage (Android.os.Message msg) {SWITC
        H (msg.what) {case Timeranimation:int position = getselecteditemposition ();
        LOG.I ("msg", "Position:" +position);
        if (Position >= (GetCount ()-1)) {OnKeyDown (keyevent.keycode_dpad_left, NULL);
        else {onKeyDown (keyevent.keycode_dpad_right, NULL); } break;
      Default:break;
  }
    };
  };
  Private Final Timer timer = new timer (); Private final TimerTask task = new TimerTask () {public void run () {Mhandler.sendemptymessage (Timeranimat
    ION);
  }
  };
    Public Homegallery (Context Paramcontext) {super (Paramcontext);
  Timer.schedule (Task, time, time); } public Homegallery (context Paramcontext, AttributeSet Paramattributeset) {super (Paramcontext, Paramattributeset
    );
  Timer.schedule (Task, time, time); } public Homegallery (context Paramcontext, attributeset paramattributeset, int paramint) {Super (Paramcontex
    T, Paramattributeset, Paramint);
  Timer.schedule (Task, time, time);  Private Boolean Isscrollingleft (Motionevent paramMotionEvent1, motionevent paramMotionEvent2) {Float F2 =
    Parammotionevent2.getx ();
    Float F1 = Parammotionevent1.getx ();
    if (F2 > F1) return true;
  return false; public boolean onfling (motionevent paramMotionEvent1, Motionevent ParamMotionEvent2, float paramFloat1, float paramFloat2) {int keycode;
    if (Isscrollingleft (ParamMotionEvent1, ParamMotionEvent2)) {keycode = Keyevent.keycode_dpad_left;
    else {keycode = Keyevent.keycode_dpad_right;
    } onKeyDown (keycode, NULL);
  return true;
  public void Destroy () {timer.cancel ();

 }
}

I hope this article will help you with your Android program.

Related Article

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.