Android uses the open-source framework android-image-indicator to implement image carousel deployment,
The previous blog posts introduced the implementation methods for image carousel, including hyperlinks ):
1. Use ViewFlipper in Android to implement screen Switching
2. Use ViewPager in Android to implement screen page switching and page carousel Effect
3. Using ImageViewSwitcher in Android to implement image switching carousel navigation Effect
Today, the android-image-indicator, an open-source project in GitHub, is used to achieve Carousel of built-in APP images and to load network images for carousel.
1. Download the project from GitHub
GitHub address: https://github.com/panxw/android-image-indicator
A simple example is provided.
Ii. Import dependency packages (1) I tried to use AndroidStudio2, and 2 used the Import Module to Import the library in the downloaded file to Import the dependency packages. However, this download project was built using Maven,
Error message during import: Error :( 2, 0) Plugin with id 'com. github. dcendents. Android-maven 'not found. A variety of solutions have been tried, which cannot effectively solve the dependency package import problem. The second method is recommended for import.
(2) Add the following code under dependencies in build. gradle (Module. app ):
1 compile 'com.panxw.imageindicator:library:1.0.2'
Add example: after adding the file, click the prompt on the page to synchronize the following information. Iii. Demonstration of loading APP built-in images (1) Layout file:
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:id="@+id/activity_main" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context="com.mly.panhouye.demo.MainActivity"> 8 <com.panxw.android.imageindicator.ImageIndicatorView 9 android:id="@+id/indicate_view"10 android:layout_width="match_parent"11 android:layout_height="match_parent">12 </com.panxw.android.imageindicator.ImageIndicatorView>13 </RelativeLayout>
(2) The Java Implementation Code is as follows:
1 package com. mly. panhouye. demo; 2 import android. OS. bundle; 3 import android. support. v7.app. appCompatActivity; 4 import com. panxw. android. imageindicator. autoPlayManager; 5 import com. panxw. android. imageindicator. imageIndicatorView; 6 import java. util. arrayList; 7 import java. util. list; 8 9 public class MainActivity extends AppCompatActivity {10 ImageIndicatorView indicate_view; 11 @ Override12 protected void onCreate (Bundle savedInstanceState) {13 super. onCreate (savedInstanceState); 14 setContentView (R. layout. activity_main); 15 indicate_view = (ImageIndicatorView) findViewById (R. id. indicate_view); 16 local (); 17} 18 // system local image loading 19 public void local () {20 // declare an array, ID21 final Integer [] resArray = new Integer [] {R. mipmap. a1, R. mipmap. a2, 22 R. mipmap. a3, R. mipmap. a4}; 23 // hand over the array to the image play component 24 indicate_view.setupLayoutByDrawable (resArray); 25 // The style 26 // indicate_view.setIndicateStyle (ImageIndicatorView. INDICATE_ARROW_ROUND_STYLE); 27 indicate_view.setIndicateStyle (ImageIndicatorView. INDICATE_USERGUIDE_STYLE); 28 // display component 29 indicate_view.show (); 30 final AutoPlayManager autoBrocastManager = new AutoPlayManager (indicate_view); 31 // you can enable Automatic Broadcast 32 autoBrocastManager. setBroadcastEnable (true); 33 // autoBrocastManager. setBroadCastTimes (5); // loop times34 // set the start time and interval 35 autoBrocastManager. setBroadcastTimeIntevel (3000,300 0); 36 // set loop playback 37 autoBrocastManager. loop (); 38} 39}4. Attach network images (1) first, customize NetworkImageIndicatorView. class in Java
The Network Communication Framework VolLey is used to load network images to imageView. Here, we mainly use ImageRequest,
The constructor of ImageRequest receives the following six parameters:
The first parameter is the URL of the image. There is nothing to explain.
The second parameter is the successful callback of the image request. Here we set the returned Bitmap parameter to ImageView.
The third and fourth parameters are used to specify the maximum width and height of the image. If the width or height of the specified network image is greater than the maximum value, the image is compressed, if it is set to 0, the image will not be compressed regardless of the size of the image.
The fifth parameter is used to specify the color attribute of the image, Bitmap. several constants in Config can be used here. ARGB_8888 can display the best color attributes, and each image pixel occupies 4 bytes, RGB_565 indicates that each image pixel occupies 2 bytes.
The sixth parameter is the callback for image request failure. Here, a default image is displayed in ImageView when the request fails.
1 package com. mly. panhouye. demo; 2 import android. content. context; 3 import android. graphics. bitmap; 4 import android. util. attributeSet; 5 import android. widget. imageView; 6 import com. android. volley. requestQueue; 7 import com. android. volley. response; 8 import com. android. volley. volleyError; 9 import com. android. volley. toolbox. imageRequest; 10 import com. android. volley. toolbox. volley; 11 import com. panxw. android. imageindicator. imageIndicatorView; 12 import java. util. list; 13/** 14 * Created by panchengjia on 0010.15 */16 public class NetworkImageIndicatorView extends ImageIndicatorView {17 public partition (Context context, AttributeSet attrs) {18 super (context, Context, attrs); 19} 20 public NetworkImageIndicatorView (Context context) {21 super (context); 22} 23 public void setupLayoutByImageUrl (List <String> urlList) {24 for (String url: urlList) {25 final ImageView imageView = new ImageView (getContext (); 26 // load image from url and set to imageView, you can use UIL or Volley to do this work27 // this time we use Volley28 // create a request for column 29 RequestQueue queue = Volley. newRequestQueue (getContext (); 30 ImageRequest request = new ImageRequest (url, new Response. listener <Bitmap> () {31 @ Override32 public void onResponse (Bitmap bitmap) {33 imageView. setImageBitmap (bitmap); 34} 35}, 0, 0, Bitmap. config. RGB_565, new Response. errorListener () {36 @ Override37 public void onErrorResponse (VolleyError volleyError) {38 System. out. println (volleyError); 39} 40}); 41 queue. add (request); 42 addViewItem (imageView); 43} 44} 45}(2) Layout display file:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:id="@+id/activity_main" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:orientation="vertical" 8 tools:context="com.mly.panhouye.demo.MainActivity"> 9 10 <com.mly.panhouye.demo.NetworkImageIndicatorView11 android:layout_width="match_parent"12 android:layout_height="match_parent"13 android:id="@+id/internet_iv">14 </com.mly.panhouye.demo.NetworkImageIndicatorView>15 16 </LinearLayout>
(3) The java Implementation Code is as follows:
1 package com. mly. panhouye. demo; 2 import android. OS. bundle; 3 import android. support. v7.app. appCompatActivity; 4 import com. panxw. android. imageindicator. autoPlayManager; 5 import com. panxw. android. imageindicator. imageIndicatorView; 6 import java. util. arrayList; 7 import java. util. list; 8 public class MainActivity extends AppCompatActivity {9 NetworkImageIndicatorView internet_iv; 10 @ Override11 pr Otected void onCreate (Bundle savedInstanceState) {12 super. onCreate (savedInstanceState); 13 setContentView (R. layout. activity_main); 14 internet_iv = (NetworkImageIndicatorView) findViewById (R. id. internet_iv); 15 internet (); 16} 17 public void internet () {18 final List <String> urlList = new ArrayList <String> (); 19 urlList. add ("http://r.photo.store.qq.com/psb? /V12kkHqD1CWRD4/1 * cdpmmlbug. gga4PxHTxZUSZqZ1ei76FIDnprasXI! /R/dKEAAAAAAAAA "); 20 urlList. add (" http://r.photo.store.qq.com/psb? /V12kkHqD1CWRD4/40Y896PFEJ0ZdQyzrd0Nar48yCs5g9lkH3jI7zSRCQQ! /R/dKEAAAAAAAAA "); 21 urlList. add (" http://r.photo.store.qq.com/psb? /V12kkHqD1CWRD4/7oqQQKh5D5OKezdyC0geEGaTQjJirH8. GbQ9mY13aIY! /R/dKAAAAAAAAAA "); 22 seek (urlList); 23 internet_iv.show (); 24 // set AutoPlayManager autoBrocastManager = new AutoPlayManager (internet_iv); 26 autoBrocastManager. setBroadcastEnable (true); 27 autoBrocastManager. setBroadCastTimes (5); // set the number of cycles to 28 autoBrocastManager. setBroadcastTimeIntevel (500,500); 29 autoBrocastManager. loop (); 30} 31}
It is very convenient to use the open-source framework. This demonstration only serves to implement functions. You have time to optimize the interface, implement the desired results (I have referenced my yuzhao in network loading. Thank you for watching it)