Android使用開(kāi)源框架ANDROID-IMAGE-INDICATOR實(shí)現(xiàn)圖片輪播部署
之前的博文中有介紹關(guān)于圖片輪播的實(shí)現(xiàn)方式,分別為(含超鏈接):
1、《Android中使用ViewFlipper實(shí)現(xiàn)屏幕切換》
2、《Android中使用ViewPager實(shí)現(xiàn)屏幕頁(yè)面切換和頁(yè)面輪播效果》
3、《Android中使用ImageViewSwitcher實(shí)現(xiàn)圖片切換輪播導(dǎo)航效果》
今天通過(guò)使用GitHub中的開(kāi)源項(xiàng)目android-image-indicator來(lái)簡(jiǎn)單實(shí)現(xiàn)APP自帶圖片的輪播以及加載網(wǎng)絡(luò)圖片進(jìn)行輪播。

一、從GitHub上下載項(xiàng)目
GitHub地址:https://github.com/panxw/android-image-indicator
其中介紹了簡(jiǎn)單的使用示例,大家可以看看

二、導(dǎo)入依賴包
(1)我嘗試使用AndroidStudio2,2通過(guò)Import Module來(lái)導(dǎo)入下載文件中的library來(lái)導(dǎo)入依賴包,但本次下載的項(xiàng)目使用Maven來(lái)構(gòu)建,
導(dǎo)入過(guò)程出現(xiàn)錯(cuò)誤提示:Error:(2, 0) Plugin with id‘com.github.dcendents.Android-maven' not found。嘗試了多種解決方案,無(wú)法有效解決依賴包導(dǎo)入問(wèn)題。建議使用第二種方法導(dǎo)入
(2)在build.gradle(Module.app)中dependencies下直接添加以下代碼
compile 'com.panxw.imageindicator:library:1.0.2'
添加示例如下:

添加完后,點(diǎn)擊界面上的提示,同步以下就好。
三、演示加載APP自帶圖片
(1)Layout布局文件如下:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.mly.panhouye.demo.MainActivity"> <com.panxw.android.imageindicator.ImageIndicatorView android:id="@+id/indicate_view" android:layout_width="match_parent" android:layout_height="match_parent"> </com.panxw.android.imageindicator.ImageIndicatorView> </RelativeLayout>
(2)Java實(shí)現(xiàn)代碼如下:
package com.mly.panhouye.demo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.panxw.android.imageindicator.AutoPlayManager;
import com.panxw.android.imageindicator.ImageIndicatorView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
ImageIndicatorView indicate_view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
indicate_view = (ImageIndicatorView) findViewById(R.id.indicate_view);
local();
}
//系統(tǒng)本地圖片加載
public void local() {
// 聲明一個(gè)數(shù)組, 指定圖片的ID
final Integer[] resArray = new Integer[] {R.mipmap.a1, R.mipmap.a2,
R.mipmap.a3, R.mipmap.a4};
// 把數(shù)組交給圖片展播組件
indicate_view.setupLayoutByDrawable(resArray);
// 展播的風(fēng)格
// indicate_view.setIndicateStyle(ImageIndicatorView.INDICATE_ARROW_ROUND_STYLE);
indicate_view.setIndicateStyle(ImageIndicatorView.INDICATE_USERGUIDE_STYLE);
// 顯示組件
indicate_view.show();
final AutoPlayManager autoBrocastManager = new AutoPlayManager(indicate_view);
//設(shè)置開(kāi)啟自動(dòng)廣播
autoBrocastManager.setBroadcastEnable(true);
//autoBrocastManager.setBroadCastTimes(5);//loop times
//設(shè)置開(kāi)始時(shí)間和間隔時(shí)間
autoBrocastManager.setBroadcastTimeIntevel(3000, 3000);
//設(shè)置循環(huán)播放
autoBrocastManager.loop();
}
}
四、加載網(wǎng)絡(luò)圖片
(1)首先在Java中自定義NetworkImageIndicatorView.class
其中在加載網(wǎng)絡(luò)圖片到imageView中使用了網(wǎng)絡(luò)通信框架-VolLey。這里主要使用其中的ImageRequest,
ImageRequest的構(gòu)造函數(shù)接收六個(gè)參數(shù),分別代表的含義是:
第一個(gè)參數(shù)就是圖片的URL地址,這個(gè)沒(méi)什么需要解釋的。
第二個(gè)參數(shù)是圖片請(qǐng)求成功的回調(diào),這里我們把返回的Bitmap參數(shù)設(shè)置到ImageView中。
第三第四個(gè)參數(shù)分別用于指定允許圖片最大的寬度和高度,如果指定的網(wǎng)絡(luò)圖片的寬度或高度大于這里的最大值,則會(huì)對(duì)圖片進(jìn)行壓縮,指定成0的話就表示不管圖片有多大,都不會(huì)進(jìn)行壓縮。
第五個(gè)參數(shù)用于指定圖片的顏色屬性,Bitmap.Config下的幾個(gè)常量都可以在這里使用,其中ARGB_8888可以展示最好的顏色屬性,每個(gè)圖片像素占據(jù)4個(gè)字節(jié)的大小,而RGB_565則表示每個(gè)圖片像素占據(jù)2個(gè)字節(jié)大小。
第六個(gè)參數(shù)是圖片請(qǐng)求失敗的回調(diào),這里我們當(dāng)請(qǐng)求失敗時(shí)在ImageView中顯示一張默認(rèn)圖片。
package com.mly.panhouye.demo;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.widget.ImageView;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
import com.panxw.android.imageindicator.ImageIndicatorView;
import java.util.List;
/**
* Created by panchengjia on 2017/1/10 0010.
*/
public class NetworkImageIndicatorView extends ImageIndicatorView {
public NetworkImageIndicatorView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NetworkImageIndicatorView(Context context) {
super(context);
}
public void setupLayoutByImageUrl(List<String> urlList) {
for(String url: urlList) {
final ImageView imageView = new ImageView(getContext());
//load image from url and set to imageView, you can use UIL or Volley to do this work
//本次我們使用Volley
//創(chuàng)建一個(gè)請(qǐng)求對(duì)列
RequestQueue queue = Volley.newRequestQueue(getContext());
ImageRequest request = new ImageRequest(url, new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap bitmap) {
imageView.setImageBitmap(bitmap);
}
}, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
System.out.println(volleyError);
}
});
queue.add(request);
addViewItem(imageView);
}
}
}
(2)Layout布局展示文件如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.mly.panhouye.demo.MainActivity"> <com.mly.panhouye.demo.NetworkImageIndicatorView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/internet_iv"> </com.mly.panhouye.demo.NetworkImageIndicatorView> </LinearLayout>
(3)java實(shí)現(xiàn)代碼如下:
package com.mly.panhouye.demo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.panxw.android.imageindicator.AutoPlayManager;
import com.panxw.android.imageindicator.ImageIndicatorView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
NetworkImageIndicatorView internet_iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
internet_iv= (NetworkImageIndicatorView) findViewById(R.id.internet_iv);
internet();
}
public void internet(){
final List<String> urlList= new ArrayList<String>();
urlList.add("http://r.photo.store.qq.com/psb?/V12kkHqD1CWRD4/1*CDpMdmLbUg.gga4PxHTxZUSZqZ1ei76FIDnprasXI!/r/dKEAAAAAAAAA");
urlList.add("http://r.photo.store.qq.com/psb?/V12kkHqD1CWRD4/40Y896PFEJ0ZdQyzrd0Nar48yCs5g9lkH3jI7zSRCQQ!/r/dKEAAAAAAAAA");
urlList.add("http://r.photo.store.qq.com/psb?/V12kkHqD1CWRD4/7oqQQKh5D5OKezdyC0geEGaTQjJirH8.GbQ9mY13aIY!/r/dKAAAAAAAAAA");
internet_iv.setupLayoutByImageUrl(urlList);
internet_iv.show();
//設(shè)置自動(dòng)播放
AutoPlayManager autoBrocastManager = new AutoPlayManager(internet_iv);
autoBrocastManager.setBroadcastEnable(true);
autoBrocastManager.setBroadCastTimes(5);//循環(huán)次數(shù)設(shè)置
autoBrocastManager.setBroadcastTimeIntevel(500, 500);
autoBrocastManager.loop();
}
}
使用開(kāi)源框架實(shí)現(xiàn)起來(lái)還是很方便的,本次演示只為實(shí)現(xiàn)功能,大家有時(shí)間可以優(yōu)化下界面,實(shí)現(xiàn)自己想要的結(jié)果(網(wǎng)絡(luò)加載中引用了本人的玉照哦,謝謝大家觀賞)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android開(kāi)發(fā)使用Handler的PostDelayed方法實(shí)現(xiàn)圖片輪播功能
- Android開(kāi)發(fā)使用Handler實(shí)現(xiàn)圖片輪播功能示例
- Android線程實(shí)現(xiàn)圖片輪播
- Android ViewPager實(shí)現(xiàn)圖片輪播效果
- Android自動(dòng)播放Banner圖片輪播效果
- Android客戶端實(shí)現(xiàn)圖片輪播控件
- Android實(shí)現(xiàn)廣告圖片輪播效果
- Android實(shí)現(xiàn)圖片輪播切換實(shí)例代碼
- Android實(shí)現(xiàn)圖片輪播效果
- Android自定義圖片輪播Banner控件使用解析
相關(guān)文章
Flutter 日期時(shí)間DatePicker控件及國(guó)際化
這篇文章主要介紹了Flutter 日期時(shí)間DatePicker控件及國(guó)際化,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
android使用NotificationListenerService監(jiān)聽(tīng)通知欄消息
本篇文章主要介紹了android使用NotificationListenerService監(jiān)聽(tīng)通知欄消息,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-01-01
Android StickyListHeaders實(shí)現(xiàn)電話本列表效果
這篇文章主要為大家詳細(xì)介紹了Android StickyListHeaders實(shí)現(xiàn)電話本列表效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
Android中webView加載H5綁定cookie實(shí)例
這篇文章主要介紹了Android中webView加載H5綁定cookie實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
Android獲取和讀取短信驗(yàn)證碼的實(shí)現(xiàn)方法
這篇文章主要介紹了Android獲取和讀取短信驗(yàn)證碼的實(shí)現(xiàn)方法,文章內(nèi)容介紹了如何讀取剛收到的短信的相關(guān)內(nèi)容,以及Android獲取短信驗(yàn)證碼的方法,感興趣的小伙伴們可以參考一下2016-03-03
Android 定位系統(tǒng)(GPS)開(kāi)發(fā)詳解
GPS定位是智能手機(jī)上一個(gè)比較有意思的功能,LBS等服務(wù)都有效的利用了GPS定位功能,本文就跟大家分享下Android開(kāi)發(fā)中的GPS定位知識(shí)2016-07-07
Android動(dòng)畫教程之屬性動(dòng)畫詳解
這篇文章主要給大家介紹了關(guān)于Android動(dòng)畫教程之屬性動(dòng)畫的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位Android開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05

