Android Custom Component Series "16"--the most handsome auto-scrolling ad bar

Source: Internet
Author: User

A period of time to implement a rolling banner, referring to a number of online implementations, found that the implementation is very troublesome, so I decided to use Viewflipper to achieve a, in this code to paste out, share with you.

Reprint please indicate source: Http://blog.csdn.net/dawanganban

Import Java.util.arraylist;import java.util.list;import Java.util.timer;import Java.util.timertask;import Com.guozha.buy.r;import Com.guozha.buy.util.dimenutil;import Android.content.context;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.os.handler;import Android.util.attributeset;import Android.view.gravity;import Android.view.motionevent;import Android.view.View; Import Android.view.viewconfiguration;import Android.widget.framelayout;import Android.widget.imageview;import Android.widget.imageview.scaletype;import Android.widget.linearlayout;import android.widget.ViewFlipper;/** * Customize Play Picture View * @author Lixiaoqiang * * CSDN Blog: Http://blog.csdn.net/dawanganban * */public class Autoviewflipper extends Fra       melayout{private static final int slop_delay_time = 3000;//Sliding wait time, unit msprivate viewflipper Mviewflipper;  The sliding view Private View mpointbar;//indicates the point display bar private int mitemcount; Number of entries private int mcurrentitem;//current entry private int mtouchslop;//effective shortest sliding distance private COntext context;private list<imageview> points = new arraylist<imageview> ();p rivate timertask mTimerTask; private static final int handler_slop_left = 0x0001;private HANDLER hander = new HANDLER () {public void Handlemessage (Andro Id.os.Message msg) {switch (msg.what) {case handler_slop_left:sloptoleft (); break;}};}; Public Autoviewflipper (context context, AttributeSet Attrs) {Super (context, attrs); this.context = Context;mtouchslop = Viewconfiguration.gettouchslop (); AddChild (context); Startautoplay ();} /** * Stop autoplay */public void Stopautoplay () {if (Mtimertask = = null) return;mtimertask.cancel (); mtimertask = null;} /** * start autoplay */public void Startautoplay () {if (mtimertask! = null) Return;mtimertask = new TimerTask () {@Overridepublic voi D run () {hander.sendemptymessage (handler_slop_left);}}; New Timer (). Scheduleatfixedrate (Mtimertask, Slop_delay_time, slop_delay_time);} /** * Add Child view * @param context */private void AddChild (context context) {Mviewflipper = Getviewflipper (context); This.addview (Mviewflipper); Mpointbar = Getpointbar (context); This.addview (Mpointbar);} /** * Get Viewflipper * @param context * @return */private viewflipper getviewflipper (context context) {Viewflipper Viewflipp ER = new Viewflipper (context); Addimageviews (context, viewflipper); return viewflipper;} /** * Get indicator Point Display bar * @param context * @return */private View Getpointbar (context context) {Linearlayout.layoutparams Pointbarpa Rams = new Linearlayout.layoutparams (LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_ PARENT);p ointbarparams.gravity = Gravity.right | Gravity.bottom; LinearLayout Pointbar = new LinearLayout (context);p ointbar.setorientation (linearlayout.horizontal); Pointbar.setlayoutparams (pointbarparams);p ointbar.setgravity (Gravity.right | Gravity.bottom); addpoints (context, Pointbar); return pointbar;} /** * Add Dots * @param context * @param pointbar */private void addpoints (context context, LinearLayout Pointbar) {Linearlayo Ut. Layoutparams pointviewparams = new Linearlayout.layoutparams (LInearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);p Ointviewparams.setmargins ( DIMENUTIL.DP2PX (context, 8), 0,dimenutil.dp2px (context, 8), dimenutil.dp2px (context, 8)); ImageView pointview;for (int i = 0; i < Mitemcount; i++) {Pointview = new ImageView (context);p Ointview.setscaletype (scaletype.center_inside);p Ointview.setlayoutparams (pointviewparams);p oints.add (Pointview);p Ointbar.addview (Pointview);} Setpointcolorbycurrentitem ();} /** * Sets the dot */private void Setpointcolorbycurrentitem () {Mcurrentitem = (Integer) Mviewflipper.getcurrentview () according to the current selection. Gettag (); Bitmap Graypointbitmap = getgraypointbitmap (context); Bitmap Lightpointbitmap = getlightpointbitmap (context); ImageView imageview;for (int i = 0; i < points.size (); i++) {imag Eview = Points.get (i); if (Mcurrentitem = = i) {imageview.setimagebitmap (lightpointbitmap);} Else{imageview.setimagebitmap (Graypointbitmap);}}} /** * Add Picture Resource * @param context * @param viewflipper */private void addimageviews (context context, ViewFlipper viewflipper) {if (Viewflipper = = null) return; list<bitmap> bitmaps = Getbitmaps (); if (bitmaps = = null) return; Linearlayout.layoutparams imageviewparams = new Linearlayout.layoutparams (LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); ImageView imageview;mitemcount = Bitmaps.size (); for (int i = 0; i < Mitemcount; i++) {ImageView = new ImageView (context); Imageview.setimagebitmap (Bitmaps.get (i)); Imageview.setscaletype ( Scaletype.center_crop); Imageview.setlayoutparams (imageviewparams); Imageview.settag (i); ViewFlipper.addView ( ImageView);}} /** * Get Picture resources * @return */private list<bitmap> getbitmaps () {//todo get pictures from the network list<bitmap> bitmaps = new arraylist& Lt Bitmap> (); Bitmaps.add (Bitmapfactory.decoderesource (Getresources (), r.drawable.main_page_scroll_image1)); Bitmaps.add (Bitmapfactory.decoderesource (Getresources (), r.drawable.main_page_scroll_image2)); Bitmaps.add ( Bitmapfactory.decoderesource (Getresources (), r.drawable.main_page_scroll_image3));return bitmaps;} /** * Get Select dots Picture * @param context * @return */private Bitmap Getlightpointbitmap (context context) {return bitmapfactory.decod EResource (Getresources (), r.drawable.main_page_scorll_point_selected);} /** * Get Gray dots Picture * @param context * @return */private Bitmap Getgraypointbitmap (context context) {return Bitmapfactory.decode Resource (Getresources (), r.drawable.main_page_scroll_point_unselected);} private float Mdownx; @Overridepublic boolean ontouchevent (Motionevent event) {Float eventx = event.getx (); switch ( Event.getaction ()) {Case MotionEvent.ACTION_DOWN:mDownX = eventx;break;case MotionEvent.ACTION_UP:float gap = eventx- Mdownx;if (Math.Abs (GAP) > Mtouchslop) {if (Gap > 0) {//Swipe right sloptoright ();} else{//Swipe left sloptoleft ();}} break;} return true;} /** * Swipe right */private void Sloptoright () {mviewflipper.setinanimation (context, r.anim.slide_in_left); Mviewflipper.setoutanimation (context, r.anim.slide_out_right); mviewflipper.showprevious (); Setpointcolorbycurrentitem ();} /** * Swipe left */private VOID Sloptoleft () {mviewflipper.setinanimation (context, r.anim.slide_in_right); Mviewflipper.setoutanimation ( context, R.anim.slide_out_left); Mviewflipper.shownext (); Setpointcolorbycurrentitem ();} Private Onsloptouchlistener monsloptouchlistener;/** * Monitor Swipe events * @author Administrator * */interface Onsloptouchlistener {/** * Touch event response */public void Ontouchedview ();} /** * Set the listener for events such as swipe * @param onsloptouchlistener */public void Setonsloptouchlistener (Onsloptouchlistener Onsloptouchlistener) {this.monsloptouchlistener = Onsloptouchlistener;}}


Android Custom Component Series "16"--the most handsome auto-scrolling ad bar

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.