Android-cyclic ad space component and android-ad space component

Source: Internet
Author: User
Tags gety

Android-cyclic ad space component and android-ad space component

The cyclic advertising space is also a very common component. There are various implementations on the Internet. On that day, I saw a singwhatiwanna implementation, which is very simple and then combined with the one I have seen before, A little bit.

Reprinted please indicate the source: http://blog.csdn.net/goldenfish1919/article/details/46811889

Let's take a look at the usage:

<com.xjs.demo.view.BannerViewandroid:id="@+id/bannerView"android:layout_width="match_parent"android:layout_height="150dp" android:paddingLeft="10dp"android:paddingRight="10dp"><android.support.v4.view.ViewPagerandroid:id="@+id/banner_viewpager"android:layout_width="match_parent"android:layout_height="match_parent" /><com.xjs.demo.view.DotViewandroid:id="@+id/banner_dotview"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="bottom|center_horizontal"android:layout_marginBottom="10dp"app:dot_number="5"app:dot_radius="4dp"app:dot_selected_color="0xffffffff"app:dot_span="8dp"app:dot_unselected_color="0x80ffffff" /></com.xjs.demo.view.BannerView>

The custom BannerView contains two sub-elements: ViewPager and DotView:

BannerView banner = (BannerView) this.findViewById(R.id.bannerView);banner.setOnBannerClickListener(new OnBannerClickListener(){@Overridepublic void OnBannerClicked(int pos) {Toast.makeText(MainActivity.this, "OnBannerClickListener:" + pos,Toast.LENGTH_SHORT).show();}});int[] imagesSrc = new int[] { R.mipmap.img1, R.mipmap.img2,R.mipmap.img3, R.mipmap.img4, R.mipmap.img5 };banner.update(imagesSrc);
You can add click events to the Banner, pass the image id, and call the update () method of the banner.

The following focuses on BannerView:


Public class BannerView extends FrameLayout {private DotView mBannerDotView; private ViewPager mBannerViewPager; private BannerAdapter mBannerAdapter;/** current position */private int mBannerPosition = 0; /** callback after Banner clicks */private OnBannerClickListener mBannerClickListener;/** auto play */private Handler mHandler = new Handler (); private Runnable task = new Runnable () {@ Overridepublic void run () {mBannerPosition = (MBannerPosition + 1) % mBannerAdapter. getCount (); mBannerViewPager. setCurrentItem (mBannerPosition); Log. d (TAG, "tname:" + Thread. currentThread (). getName (); mHandler. postDelayed (task, 3000) ;}}; private static final String TAG = BannerView. class. getSimpleName (); public BannerView (Context context) {this (context, null);} public BannerView (Context context, AttributeSet attrs) {this (context, attrs, 0 );} Public BannerView (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); init (context);} private void init (Context context) {}@ Overrideprotected void onFinishInflate () {super. onFinishInflate (); // note the following rules: mBannerViewPager = (ViewPager) getChildAt (0); mBannerDotView = (DotView) getChildAt (1); mBannerDotView. setDotNumber (0); mBannerAdapter = new BannerAdapter (getContext (), ne W int [0]); mBannerViewPager. setAdapter (mBannerAdapter); mBannerViewPager. setOnPageChangeListener (mBannerAdapter);} public void update (int [] imagesSrc) {if (imagesSrc = null | imagesSrc. length <= 0) {return;} mBannerDotView. setDotNumber (imagesSrc. length); mBannerDotView. setSelected (0); mBannerAdapter. update (imagesSrc); mHandler. postDelayed (task, 3000);} private void setIndicator (int position) {position % = MBannerAdapter. getSize (); mBannerDotView. setSelected (position);} private int mDownX; private int mDownY; private long mDownTime; @ Override public boolean dispatchTouchEvent (MotionEvent event) {int action = event. getAction (); if (action = MotionEvent. ACTION_DOWN) {mHandler. removeCallbacks (task); mDownX = (int) event. getX (); mDownY = (int) event. getY (); mDownTime = System. currentTimeMillis ();} e Lse if (action = MotionEvent. ACTION_UP) {if (System. currentTimeMillis ()-mDownTime <500 & Math. abs (mDownX-event. getX () <5 & Math. abs (mDownY-event. getY () <5) {// interface callback if (mBannerClickListener! = Null) {mBannerClickListener. onBannerClicked (mBannerPosition % mBannerAdapter. getSize () ;}} mHandler. postDelayed (task, 3000);} else if (action = MotionEvent. ACTION_CANCEL) {mHandler. postDelayed (task, 3000);} else if (action = MotionEvent. ACTION_MOVE) {// do nothing} return super. dispatchTouchEvent (event);} private class BannerAdapter extends PagerAdapter implements ViewPager. onPageChangeListener {private Context mContext; private int [] mImagesSrc; private int mSize; private int mFakeSize; public BannerAdapter (Context context, int [] imagesSrc) {mContext = context; update (mImagesSrc);} public void update (int [] imagesSrc) {if (imagesSrc = null | imagesSrc. length <= 0) {return;} this. mImagesSrc = imagesSrc; this. mSize = imagesSrc. length; this. mFakeSize = mSize * 10; yydatasetchanged () ;}@ Override public int getCount () {return mFakeSize;} public int getSize () {return mSize ;} @ Override public boolean isViewFromObject (View view, Object o) {return view = o ;}@ Override public Object instantiateItem (ViewGroup container, int position) {position % = mSize; imageView imageView = new ImageView (mContext); imageView. setLayoutParams (new ViewGroup. layoutParams (ViewGroup. layoutParams. MATCH_PARENT, ViewGroup. layoutParams. MATCH_PARENT); imageView. setScaleType (ScaleType. CENTER_CROP); imageView. setImageResource (mImagesSrc [position]); container. addView (imageView); return imageView;} @ Override public void destroyItem (ViewGroup container, int position, Object object) {container. removeView (View) object);} @ Override public void finishUpdate (ViewGroup container) {int position = mBannerViewPager. getCurrentItem (); Log. d (TAG, "finish update before, position =" + position); if (position = 0) {position = mSize; mBannerViewPager. setCurrentItem (position, false);} else if (position = getCount ()-1) {position = mSize-1; mBannerViewPager. setCurrentItem (position, false);} Log. d (TAG, "finish update after, position =" + position) ;}@ Override public void onPageScrolled (int position, float positionOffset, int positionOffsetPixels) {}@ Override public void onPageSelected (int position) {mBannerPosition = position; setIndicator (position) ;}@ Override public void onPageScrollStateChanged (int state) {}} public interface OnBannerClickListener {public void OnBannerClicked (int pos);} public void setOnBannerClickListener (OnBannerClickListener bannerClickListener) {this. mBannerClickListener = bannerClickListener;} @ Overridepublic void onDetachedFromWindow () {mHandler. removeCallbacksAndMessages (null); this. removeAllViews (); this. mBannerClickListener = null; super. onDetachedFromWindow ();}}
Take a look at DotView:

Public class DotView extends LinearLayout {private int pair; private int mDotSpan = 36; private float mDotRadius = 6f; private int mDotNumber = 5; private int mCurrent = 0; private int mSelectedColor = 0xFF377BEE; private int mUnSelectedColor = 0xFFC5CEDB; public DotView (Context context) {super (context);} public DotView (Context context, AttributeSet attrs) {super (context, attrs); // get Taken from the defined attribute TypedArray arr = context. obtainStyledAttributes (attrs, R. styleable. DotView, 0, 0); if (arr! = Null) {if (arr. hasValue (R. styleable. dotView_dot_radius) {mDotRadius = arr. getDimension (R. styleable. dotView_dot_radius, mDotRadius);} if (arr. hasValue (R. styleable. dotView_dot_span) {mDotSpan = (int) arr. getDimension (R. styleable. dotView_dot_span, mDotSpan);} if (arr. hasValue (R. styleable. dotView_dot_number) {mDotNumber = (int) arr. getInt (R. styleable. dotView_dot_number, mDotNumber);} mSelecte DColor = arr. getColor (R. styleable. dotView_dot_selected_color, mSelectedColor); mUnSelectedColor = arr. getColor (R. styleable. dotView_dot_unselected_color, mUnSelectedColor); arr. recycle ();} mLittleDotWidth = (int) (mDotSpan/2 + mDotRadius * 2); // draw the dot addDotViews ();} public void setDotNumber (int dotNumber) {this. mDotNumber = dotNumber; addDotViews ();} private void addDotViews () {setGravity (Gr Avity. CENTER_HORIZONTAL); setOrientation (HORIZONTAL); removeAllViews (); for (int I = 0; I <mDotNumber; I ++) {LittleDot dot = new LittleDot (getContext (), i); if (I = 0) {dot. setColor (mSelectedColor);} else {dot. setColor (mUnSelectedColor);} dot. setLayoutParams (new LayoutParams (int) mLittleDotWidth, (int) mDotRadius * 2, 1); addView (dot) ;}} public final void setSelected (int index) {if (in Dex> = getChildCount () | index <0 | mCurrent = index) {return;} if (mCurrent <getChildCount () & mCurrent> = 0) {(LittleDot) getChildAt (mCurrent )). setColor (mUnSelectedColor);} (LittleDot) getChildAt (index )). setColor (mSelectedColor); mCurrent = index;} private class LittleDot extends View {private int mColor; private Paint mPaint; public LittleDot (Context context, int index) {super (co Ntext); mPaint = new Paint (); mPaint. setAntiAlias (true);} public void setColor (int color) {if (color = mColor) {return;} mColor = color; invalidate ();} @ Override protected void onDraw (Canvas canvas) {super. onDraw (canvas); mPaint. setColor (mColor); canvas. drawCircle (mLittleDotWidth/2, mDotRadius, mDotRadius, mPaint) ;}} public void setSelectedColor (int color) {if (mSelectedColor! = Color) {mSelectedColor = color; invalidate () ;}} public void setUnSelectedColor (int color) {if (mUnSelectedColor! = Color) {mSelectedColor = color; invalidate () ;}} public void setColor (int selectedColor, int unSelectedColor) {if (mSelectedColor! = SelectedColor | mUnSelectedColor! = UnSelectedColor) {mSelectedColor = selectedColor; mUnSelectedColor = unSelectedColor; invalidate ();}}}
Finally, attrs_dotview.xml:

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="DotView">        <attr name="dot_number" format="integer" />        <attr name="dot_radius" format="dimension" />        <attr name="dot_span" format="dimension" />        <attr name="dot_unselected_color" format="integer" />        <attr name="dot_selected_color" format="integer" />    </declare-styleable></resources>

The advantage of this control is that it is very simple to use. You only need to pass the image to the update method of BannerView, and the source code is also very simple to modify.

In addition, the implementation of DotView is very clever and worth learning!

We do not produce code. We are just code porters. Thank you for your selfless dedication:

BannerView reference: http://blog.csdn.net/singwhatiwanna/article/details/46541225

DotView reference: https://github.com/etao-open-source/cube-sdk/tree/master/core/src/in/srain/cube/views/banner

Touch interception: http://blog.csdn.net/wuseyukui/article/details/46627961

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.