This article shares the specific code for Android to realize the effect display of carousel images for your reference. The specific content is as follows
public class MainActivity extends AppCompatActivity { private LinearLayout ll_dots; private TextView viewpager_tv; // Define ViewPager as a global variable for easy use. private ViewPager viewpager_vp; // Create an ArrayList collection. The generic is specified as ImageView. ArrayList<ImageView> imageViews = new ArrayList<ImageView>(); //Create a handler object, rewrite the handlerMessage method, use the switch method, and get the identification by obtaining the .333 private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { switch () { case 1: //Get the item entry of the current VIewPager and user interaction.VIewPager object.getCurrentItem 333 int currentItem = viewpager_vp.getCurrentItem(); //Set the current interface displayed by ViewPager, and get ITem+1 viewpager_vp.setCurrentItem(currentItem + 1); //Use the static method sendEmptyMessageDelayed to execute the command repeatedly in a delay. Note that it is not sendEmptyMessageAtTime 333 sendEmptyMessageDelayed(1, 3000); break; default: break; } } }; //Picture int array resource private int[] imageResIds = {, , , , , }; //Image string array String[] resource. private String[] descs = { "Work Designers Alliance", "Tutorial Network", "PS Alliance", "25 Schools", "The class factory will take you to counterattack and help you reach the peak of your life", "When you are frequently modified by the project manager due to your needs and want to be beaten up, please first look at the picture of Teacher Yi's silly laughing. Do you want to hit me more?" }; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); //Initialize the control init(); } /** * Initialize VIewPager. */ private void init() { //Find the object of the container LinearLayout that can set points and add points ll_dots = (LinearLayout) findViewById(.ll_dots); //Find the object of the text. viewpager_tv = (TextView) findViewById(.viewpager_tv); //Finish the VIewPager object. viewpager_vp = (ViewPager) findViewById(.viewpager_vp); //More image int array resources, dynamically create ImageView control. There are several images, several ImageView, and for loops. for (int x = 0; x < ; x++) { //Create ImageView object ImageView imageView = new ImageView(this); //Add image resources through this object.setBackgroundResource method. (imageResIds[x]); //Add controls to the collection ImageViews to facilitate the acquisition of the instantiateItem method in the VIewPager adapter. (imageView); //Add points, the number of them is consistent with the number of pictures, so put them in this loop. 222 dot(); } //Set the adapter.setAdapter viewpager_vp.setAdapter(new Myadapter()); //Set the ViewPager's sliding listener, addOnPageChangeListener, set method was eliminated due to the name, in onPageScrolled.222 viewpager_vp.addOnPageChangeListener(new () { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { //Get the item location of the ViewPager that the current user interacts with by the getCurrentItem method. int currentItem = viewpager_vp.getCurrentItem(); //By using the obtained item, select the text and point. changeTextAndDot(currentItem % ); ("aaa", "onPageScrolled: Position-" + position + " positionOffset-" + positionOffset + " positionOffsetPixels-" + positionOffsetPixels); } @Override public void onPageSelected(int position) { ("aaa", "onPageSelected: position" + position); } @Override public void onPageScrollStateChanged(int state) { ("aaa", "onPageScrollStateChanged: state" + state); } }); //Specify that the VIewPager jumps to a certain page by default. It is generally the largest number. SetCurrentItem is to set which page the VIewPager jumps to, get is to get.333 viewpager_vp.setCurrentItem(Integer.MAX_VALUE / 2 - 3); //Through the handler, start looping in 3 seconds, 333 (1, 3000); //Set the touch event of VIewPager. The final commission method.333 ViewPagerTouchEvent(); } /** * Note: Look at the information shortcut keys of a method or class: Ctrl+Q; * Like ListVIew, create a ViewPager adapter and customize a class to inherit PagerAdapter */ private class Myadapter extends PagerAdapter { //getContent, set the number of entries of ViewPager. Generally, it is the length of the collection or resource array. @Override public int getCount() { //Set the returned entry to infinite size.333 //Note: Generally, the page that is currently selected by the ViewPager must be changed to position (current page number)%() (take the remainder). You can see which error is reported and correct 333 according to the error report return Integer.MAX_VALUE; } //isViewFromObject, determine whether the entry View object of ViewPager and the Object object returned by InstantiateItem are consistent, fixed format: return view==object; @Override public boolean isViewFromObject(View view, Object object) { return view == object; } //Ctrl+H: See the infrastructure diagram of a class ctrl+h+l: Quick format. //InstantiateItem, ViewPager's operation to add an entry. container: the incarnation of VIewPager, the controls are all added to it, position: represents the position of the user's sliding entry. @Override public Object instantiateItem(ViewGroup container, int position) { //Fetch the corresponding ImageVIew picture from the ImageViews collection based on the location of the entry (using Position). ImageView imageView = (position % ); //Add the ImageView object to the VIewPager object, that is, the container, and use addView (imageView); //Note: What control you add to VIewPager, you must return the control and compare and judge the isViewFromObject. Here is an ImageView, and the returned ImageView is returned return imageView; } //Prevent memory leaks. It is equivalent to the multiplexed container of ListView, destroying a page. The actual method is to remove the VIew object returned by the instantiateItem from the ViewPager. //container: Or the ViewPager control itself position: object: It represents the View control. When using it, you must force it to view. //Supplement: Why is the parameter Object instead of a View directly? Because although 99% is a view, it may also be a Fragment, so using Object improves scalability. @Override public void destroyItem(ViewGroup container, int position, Object object) { //After deleting the construction method, it is also a fixed format: ((View) object); ((View) object); } } /** * More image resources, dynamic creation points 222 */ public void dot() { //Create a View object; View view = new View(this); //Set the background setBackgroundResource for this View object (.dot_normal); //Set the width and height parameters for the View object, use the parameter object LayoutParams(int,int), and use which container to create it with which container to give it. layoutParams = new (8, 8); //Use parameter object =int, which is equivalent to padding in the layout. = 8; //Set the prepared layoutParams parameter object to the View object.setLayoutParams (layoutParams); //The last container object.addView(VIew); ll_dots.addView(view); } /** * According to the change of the ViewPager item, that is, the operation of image switching, set the corresponding points and text. 222 * * @param position int ViewPager item, also the location of the picture */ public void changeTextAndDot(int position) { //Set the corresponding text according to the change of the ViewPager item.setText(descs[position]); viewpager_tv.setText(descs[position]); //Judge whether the point is the point on the current page, use the for loop, get the positions of all points, and then compare with position for (int x = 0; x < ; x++) { //.getChildAt(x); Get the child control of the container. Get the VIew object View childAt = ll_dots.getChildAt(x); //Set the background image for the View, use the ternary operator. (x == position ? .dot_focus : .dot_normal); } } /** * This method mainly sets the touch event of VIewPager, so that it will no longer automatically play when the user touches it. .333 in switch * Another implementation idea is to judge the status of the ViewPager in the ViewPager listening event. Idle-slide, and remove the slide when interacting with the user. * public void onPageScrollStateChanged(int state) { * //When the sliding state changes, the interface switching operation cannot be performed when sliding manually * //SCROLL_STATE_IDLE: Idle state * if (state == ViewPager.SCROLL_STATE_IDLE) { * //Automatic switching interface * (VIEWPAGER_SWITCH_PAGE, 3000); * }else{ * //Stop automatic switching * //Stop the interface switching operation * (VIEWPAGER_SWITCH_PAGE); * } * } */ public void ViewPagerTouchEvent() { viewpager_vp.setOnTouchListener(new () { @Override public boolean onTouch(View view, MotionEvent motionEvent) { switch (()) { case MotionEvent.ACTION_DOWN: //At that time, MotionEvent.ACTION_DOWN and ACTION_MOVE, the .333 sent by the handler was removed. (1); break; case MotionEvent.ACTION_MOVE: (1); break; // When the user releases his hand ACTION_UP, continue to use sendEmptyMessageDelayed to send the handler message.333 case MotionEvent.ACTION_UP: (1, 3000); default: break; } return false; } }); } @Override protected void onDestroy() { (); (1); } }
activity_main.xml
<RelativeLayout xmlns:andro xmlns:tools="/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <. android: android:layout_width="wrap_content" android:layout_height="300dp" /> <!-- definitionlineatLayouthour,background="#88000000",gravity="center_horizontal" padding="8dp" android:layout_alignBottom="@+id/viewpager_vp" Use a smaller oneLinearLayoutCome to package.--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#88000000" android:gravity="center_horizontal" android:padding="8dp" android:layout_alignBottom="@+id/viewpager_vp" android:orientation="vertical"> <!--Here the selection attribute of the status selector isstate_selected,Insteadstate_pressed, You need to know their differences,Quote picturesdrawable,NoticeVYes capital, Points are based on the number of pictures,Dynamically created,All of oursViewComment out.-->text <TextView android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/white" android:text="I'm a handsome guy"/> <LinearLayout android: android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center_horizontal"> <!-- <View android:layout_width="5dp" android:layout_height="5dp" android:background="@drawable/dot_focus"/>--> </LinearLayout> </LinearLayout> </RelativeLayout>
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.