日本免费全黄少妇一区二区三区-高清无码一区二区三区四区-欧美中文字幕日韩在线观看-国产福利诱惑在线网站-国产中文字幕一区在线-亚洲欧美精品日韩一区-久久国产精品国产精品国产-国产精久久久久久一区二区三区-欧美亚洲国产精品久久久久

android使用ViewPager實現(xiàn)輪播效果教程 android banner輪播圖


android使用ViewPager實現(xiàn)輪播效果教程 android banner輪播圖


ViewPager:一個可以實現(xiàn)視圖左右滑動切換的控件 。
ViewPager的基本使用:
ViewPager的視圖需要通過PagerAdapter來實現(xiàn)顯示 。
PagerAdapter的主要方法:
● getCount 視圖的數(shù)量
● instantiateItem 添加視圖
● destroyItem 銷毀視圖
● isViewFromObject 是否由對象創(chuàng)建視圖
ViewPager的常用方法:
setAdapter 設置適配器
addOnPageChangeListener 監(jiān)聽頁面切換的狀態(tài)
setCurrentItem 設置當前頁面的索引
實現(xiàn)廣告輪播
技術點分析:
1、網(wǎng)絡連接
2、JSON解析
3、ViewPager的初始化
4、圖片的異步加載
5、圓點指示器的實現(xiàn)
使用selector+shape實現(xiàn)圓點圖片
動態(tài)創(chuàng)建ImageView添加到集合中,同時添加到布局中
在ViewPager切換事件中,切換圓點狀態(tài)
6、自動切換效果
使用Handler的post方法,定時執(zhí)行代碼
資源文件:
mydot.xml //創(chuàng)建圓點資源,放在drawable文件下







布局文件:
activity_main.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv_list" />
activity_banner.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
android:id="@+id/vp_image"
android:layout_width="wrap_content"
android:layout_height="150dp"
android:layout_gravity="center" />
android:layout_width="match_parent"
android:layout_height="40dp"
android:id="@+id/layout"
android:layout_gravity="bottom"
android:gravity="center"
android:orientation="horizontal"
>
java代碼
ImageLoader.java //該代碼是通過URL獲取圖片資源
package cn.edu.huse.banner_re.util;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
* 圖片加載的工具類
* @author xray
*
*/
public class ImageLoader {
/**
* 圖片加載完成的回調(diào)接口
* 添加URL參數(shù),用于做圖片錯位判斷
*/
public interface OnImageLoadListener{
//完成圖片加載
void onImageLoadComplete(String url, Bitmap bitmap);
}
private OnImageLoadListener mListener;
/**
* 啟動圖片加載任務
* @param urlStr
* @param listener
*/
public void loadImage(String urlStr,OnImageLoadListener listener){
this.mListener = listener;
new ImageLoadTask().execute(urlStr);
}
/**
* 圖片加載任務
* @author xray
*
*/
class ImageLoadTask extends AsyncTask
@Override
protected UrlAndBitmap doInBackground(String... params) {
try {
//創(chuàng)建URL,指定圖片地址
URL url = new URL(params[0]);
//打開連接獲得HttpURLConnection對象
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//獲得文件輸入流
InputStream stream = conn.getInputStream();
//把輸入流轉換為圖片
Bitmap bmp = BitmapFactory.decodeStream(stream);
//關閉流
stream.close();
return new UrlAndBitmap(params[0],bmp);
} catch (MalformedURLException e) {
e.printStackTrace();

推薦閱讀