Custom image carousel framework. It is also used as a dependent library. Custom framework

Source: Internet
Author: User

Custom image carousel framework. It is also used as a dependent library. Custom framework

I believe everyone is familiar with the Carousel of images, right. Today, I am going to express my opinions here...

It also starts from inheriting LineaLayout...

Not much. Code above

Package com. weight. adscrollviewlib;

Import java. util. ArrayList;
Import java. util. Timer;
Import java. util. TimerTask;

Import android. content. Context;
Import android. OS. Bundle;
Import android. OS. Handler;
Import android. OS. Message;
Import android. support. v4.view. PagerAdapter;
Import android. support. v4.view. ViewPager;
Import android. support. v4.view. ViewPager. OnPageChangeListener;
Import android. util. AttributeSet;
Import android. util. Log;
Import android. view. Gravity;
Import android. view. LayoutInflater;
Import android. view. View;
Import android. widget. ImageView;
Import android. widget. LinearLayout;

Import com. weight. adscrollviewlib. entity. Ad;

Public class AdScrollLayout extends LinearLayout {
ArrayList <Integer> bitMapData = new ArrayList <Integer> ();
ArrayList <View> bitValue = new ArrayList <View> ();
ArrayList <ImageView> imgData = new ArrayList <ImageView> ();
ViewPager myViewPage;
LinearLayout myImgState; // container Status display
Timer timer;
TimerTask task;
Int index = 0; // The specific sliding position of the current page
Int stateImgNormal;
Int stateImgSelect;

Public AdScrollLayout (Context context, AttributeSet attrs ){
Super (context, attrs );
InitView ();

}

Public AdScrollLayout (Context context, AttributeSet attrs, int defStyle ){
Super (context, attrs, defStyle );
InitView ();
}

Public AdScrollLayout (Context context ){
Super (context );
InitView ();
}

/**
*
* @ Param content
*: Image Content
* @ Param normalBitMap
*: Images normally displayed
* @ Param selectBitMap
*: Select the image to be displayed.
*/
Public void addImageAndBitMapState (ArrayList <Ad> content, int normalBitMap,
Int selectBitMap ){
StateImgNormal = normalBitMap;
StateImgSelect = selectBitMap;
LayoutInflater inflater = LayoutInflater. from (getContext ());
For (int I = 0; I <content. size (); I ++ ){
ImageView imageView = null;
If (I = 0 ){
ImageView = new ImageView (getContext ());
ImageView. setBackgroundResource (selectBitMap );
} Else {
ImageView = new ImageView (getContext ());
ImageView. setBackgroundResource (normalBitMap );
}
MyImgState. setGravity (Gravity. CENTER );
LayoutParams params = new LinearLayout. LayoutParams (8, 8 );
Params. leftMargin = 10;
MyImgState. addView (imageView, params );
ImgData. add (imageView );

}
// The following is mainly about Data Reconstruction.
For (int I = 0; I <content. size (); I ++ ){
View view = inflater. inflate (R. layout. ad_scroll_item, null );
View. findViewById (R. id. adImg). setBackgroundResource (
Content. get (I). getImg ());
BitValue. add (view );
}
MyViewPage. setAdapter (new MyAdapter ());
SetPageFromTime (3000 );
}

/**
*
* @ Title: initView
* @ Description: Initialize the layout page.
* @ Param
* @ Return
* @ Throws
*/
Private void initView (){
LayoutInflater inflater = LayoutInflater. from (getContext ());
View view = inflater. inflate (R. layout. ad_scroll, this );
FindView (view );
MyViewPage. setOnPageChangeListener (new MyPageChangeLister ());
}

/**
* Sliding monitoring of pages
*
* @ Author Administrator
*
*/
Private class MyPageChangeLister implements OnPageChangeListener {
@ Override
Public void onPageScrollStateChanged (int arg0 ){
}

@ Override
Public void onPageScrolled (int arg0, float arg1, int arg2 ){

}

@ Override
Public void onPageSelected (int id ){
Index = id;
// Log. e ("arg0", id + "");

Hand ();
}
}

/**
* ViewPager Adapter
*
* @ Author Administrator
*
*/
Private class MyAdapter extends PagerAdapter {
@ Override
Public int getCount (){
Return bitValue. size ();
}

@ Override
Public boolean isViewFromObject (View view, Object obj ){
Return view = obj;
}

@ Override
Public void destroyItem (View container, int position, Object object Object ){
(ViewPager) container). removeView (bitValue. get (position ));
}

@ Override
Public Object instantiateItem (View container, int position ){
(ViewPager) container). addView (bitValue. get (position ));
Return bitValue. get (position );
}
}

/**
*
* @ Title: findView
* @ Description: Find the control.
* @ Param view
* @ Return
* @ Throws
*/
Private void findView (View view ){
MyViewPage = (ViewPager) view. findViewById (R. id. myViewPage );
MyImgState = (LinearLayout) view. findViewById (R. id. myImgState );
}

// A method is required: Use ViewPage dynamically through time to implement a page switching function.

Public void setPageFromTime (int delayTime ){
Timer = new Timer ();
Task = new TimerTask (){
@ Override
Public void run (){
// Business logic
Hand ();
Index ++;
If (index = imgData. size ()){
Index = 0;
}
}
};
Timer. schedule (task, delayTime, delayTime );
}

/**
*
* @ Title: hand
* @ Description: handle sends data
* @ Param
* @ Return
* @ Throws
*/
Protected void hand (){
Bundle bundle = new Bundle ();
Bundle. putInt ("index", index );
Message msg = new Message ();
Msg. setData (bundle );
Msg. what = 100;
Handler. sendMessage (msg );
}

Handler handler = new Handler (){
Public void handleMessage (android. OS. Message msg ){
Switch (msg. what ){
Case 100:
Int index = msg. getData (). getInt ("index ");
InitPic (index );
Break;
}

}
};

/**
*
* @ Title: initPic
* @ Description: Scheduled Update page
* @ Param index2: a location to be updated on the current page
* @ Return
* @ Throws
*/
Protected void initPic (int index2 ){
MyViewPage. setCurrentItem (index2 );
ChangeImage (index2 );
}

/**
* Pass an id.
*
* @ Param id
*/
Public void changeImage (int id ){
For (int I = 0; I If (id = I ){
ImgData. get (I). setBackgroundResource (stateImgSelect );
} Else {
ImgData. get (I). setBackgroundResource (stateImgNormal );
}
}
}

Public ViewPager getViewPage (){
Return myViewPage;
}

}

This is the custom code, which is actually very simple ..

Do good deeds .. All the real code is for you.

Package com. weight. adscrollviewlib. entity;

/**
* Advertisement entity
*
* @ Author Administrator
*
*/
Public class Ad {
Private int img; // the image of the ad content

Public Ad (int img ){
This. img = img;
}

Public Ad (){
}

Public int getImg (){
Return img;
}

Public void setImg (int img ){
This. img = img;
}
}

Then this part is ready for the source code partners .. You can find me...

Source Code address: Link: http://pan.baidu.com/s/1nvoyQbJ password: y375

 

Use it as a dependent library ..

 

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.