




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】怎么在Android中自定義一個(gè)圖片輪播Banner控件
這篇文章給大家介紹怎么在Android中自定義一個(gè)圖片輪播Banner控件,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。功能特點(diǎn)支持自定義寬高比例支持自定義圖片切換時(shí)間支持自定義指示點(diǎn)的顏色支持自定義指示點(diǎn)的背景色支持自定義指示點(diǎn)的高度支持是否顯示指示點(diǎn)支持每個(gè)圖片設(shè)置不同的點(diǎn)擊事件使用簡(jiǎn)單
<com.xiaomai.bannerview.BannerView
android:id="@+id/bannerView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:aspectRatio="1.5"
app:defaultSrc="@mipmap/ic_launcher"
app:indicatorBackground="@color/white"
app:indicatorHeight="50dp"
app:indicatorPositionSize="8dp"
app:updateTime="3000"
/>實(shí)現(xiàn)步驟聲明自定義的屬性創(chuàng)建一個(gè)類繼承RelativeLayout解析屬性聲明自定義的屬性在values/attrs文件中創(chuàng)建自定義的屬性<resources>
<declare-styleable
name="BannerView">
<attr
name="aspectRatio"
format="float"
/>
<!--
寬高比
-->
<attr
name="defaultSrc"
format="integer|reference"
/>
<!--
占位圖
-->
<attr
name="updateTime"
format="integer"
/>
<!--
圖片切換時(shí)間
-->
<attr
name="indicatorVisible"
format="boolean"
/>
<!--
是否顯示指示器
-->
<attr
name="indicatorHeight"
format="dimension"
/>
<!--
指示器的高度
-->
<attr
name="indicatorBackground"
format="color|reference"
/>
<!--
指示器的背景顏色
-->
<attr
name="indicatorPositionSize"
format="dimension"
/>
<!--
指示點(diǎn)的大小
-->
</declare-styleable>
</resources>創(chuàng)建BannerView類public
class
BannerView
extends
RelativeLayout
{
public
BannerView(Context
context)
{
this(context,
null);
}
public
BannerView(Context
context,
AttributeSet
attrs)
{
this(context,
attrs,
0);
}
public
BannerView(Context
context,
AttributeSet
attrs,
int
defStyleAttr)
{
super(context,
attrs,
defStyleAttr);}在BannerView中聲明變量屬性private
Context
context;
private
Handler
handler;
private
ImageLoader
imageLoader;
private
DisplayImageOptions
options;
private
boolean
isHaveHandler
=
true;//
當(dāng)用戶點(diǎn)擊輪播圖時(shí),取消handler隊(duì)列,也就是取消滾動(dòng)
//
控件Start
private
ViewPager
viewPager;
private
LinearLayout
indicator;//
指示器
private
onItemClickListener
listener;
//
控件End
//
自定義屬性Start
private
float
mAspectRatio;
//
寬高比
private
int
defaultImageResource;
//
默認(rèn)占位圖
private
int
updateTime;
//
圖片切換的時(shí)間間隔,默認(rèn)3秒
private
boolean
showIndicator;
//
是否顯示指示器,默認(rèn)顯示
private
int
indicatorHeight;//
指示器的高度,默認(rèn)35dp
private
int
indicatorPositionSize;
//
指示器的大小
private
int
indicatorBackground;
//
指示器的背景顏色
//
自定義屬性End
//
數(shù)據(jù)Start
private
int
imageCount;
private
int
lastPosition;
private
List<Integer>
imageResources;
private
List<Image>
imageUrls;
//
數(shù)據(jù)End解析自定義屬性的值接下來(lái)為自定義的屬性賦值,在3個(gè)參數(shù)的構(gòu)造方法中初始化變量。public
BannerView(Context
context,
AttributeSet
attrs,
int
defStyleAttr)
{
super(context,
attrs,
defStyleAttr);
parseCustomAttributes(context,
attrs);
this.context
=
context;
handler
=
new
Handler();
imageLoader
=
ImageLoader.getInstance();
options
=
HSApplication.getDisplayOptions().build();
initViews();
}
/**
*
解析自定義屬性
*
*
@param
context
*
@param
attrs
*/
private
void
parseCustomAttributes(Context
context,
AttributeSet
attrs)
{
TypedArray
typedArray
=
context.obtainStyledAttributes(attrs,
R.styleable.BannerView);
mAspectRatio
=
typedArray.getFloat(R.styleable.BannerView_aspectRatio,
0f);
defaultImageResource
=
typedArray.getResourceId(R.styleable.BannerView_defaultSrc,
R.drawable.about_us);
updateTime
=
typedArray.getInt(R.styleable.BannerView_updateTime,
3000);
showIndicator
=
typedArray.getBoolean(R.styleable.BannerView_indicatorVisible,
true);
indicatorHeight
=
(int)
(typedArray.getDimension(R.styleable.BannerView_indicatorHeight,
Utils.dip2px(context,
35)));
indicatorBackground
=
typedArray.getResourceId(R.styleable.BannerView_indicatorBackground,
R.color.white_alpha00);
indicatorPositionSize
=
(int)
typedArray.getDimension(
R.styleable.BannerView_indicatorPositionSize,
Utils.dip2px(context,
5));
typedArray.recycle();
}
private
void
initViews()
{
viewPager
=
new
ViewPager(context);
viewPager.addOnPageChangeListener(new
ViewPager.OnPageChangeListener()
{
@Override
public
void
onPageScrolled(int
position,
float
positionOffset,
int
positionOffsetPixels)
{
}
@Override
public
void
onPageSelected(int
position)
{
if
(showIndicator)
{
for
(int
i
=
0;
i
<
indicator.getChildCount();
i++)
{
indicator.getChildAt(i).setSelected(false);
}
indicator.getChildAt(position
%
imageCount).setSelected(true);
}
lastPosition
=
position;
}
@Override
public
void
onPageScrollStateChanged(int
state)
{
switch
(state)
{
case
ViewPager.SCROLL_STATE_IDLE://
空閑狀態(tài)
if
(!isHaveHandler)
{
isHaveHandler
=
true;
handler.postDelayed(updateRunnable,
updateTime);
}
break;
case
ViewPager.SCROLL_STATE_DRAGGING://
用戶滑動(dòng)狀態(tài)
handler.removeCallbacks(updateRunnable);
isHaveHandler
=
false;
break;
}
}
});
addView(viewPager,
new
LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
if
(showIndicator)
{
indicator
=
new
LinearLayout(context);
indicator.setOrientation(LinearLayout.HORIZONTAL);
indicator.setGravity(Gravity.CENTER);
indicator.setBackgroundResource(indicatorBackground);
RelativeLayout.LayoutParams
layoutParams
=
new
LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
indicatorHeight);
layoutParams.addRule(ALIGN_PARENT_BOTTOM);
addView(indicator,
layoutParams);
}
}控件和自定義的屬性都經(jīng)過(guò)賦值和初始化了,接下來(lái),該為設(shè)置圖片資源了。
public
void
setImageResources(List<Integer>
imageResources)
{
if
(imageResources
==
null
||
imageResources.size()
==
0)
{
throw
new
RuntimeException("圖片資源為空");
}
this.imageResources
=
imageResources;
imageCount
=
imageResources.size();
}
public
void
setImageUrls(List<Image>
imageUrls)
{
if
(imageUrls
==
null
||
imageUrls.size()
==
0)
{
throw
new
RuntimeException("圖片地址資源為空");
}
this.imageUrls
=
imageUrls;
imageCount
=
imageUrls.size();
loadImages();
}
private
void
loadImages()
{
if
(showIndicator)
{
addIndicationPoint();
}
viewPager.setAdapter(new
MyViewPageAdapter());
viewPager.setCurrentItem(200
-
(200
%
imageCount));
handler.removeCallbacks(updateRunnable);
handler.postDelayed(updateRunnable,
updateTime);
}
/**
*
添加指示點(diǎn)到指示器中
*/
private
void
addIndicationPoint()
{
//
防止刷新重復(fù)添加
if
(indicator.getChildCount()
>
0)
{
indicator.removeAllViews();
}
View
pointView;
int
margin
=
Utils.dip2px(context,
5f);
LinearLayout.LayoutParams
layoutParams
=
new
LinearLayout.LayoutParams(
indicatorPositionSize,
indicatorPositionSize);
layoutParams.setMargins(margin,
margin,
margin,
margin);
for
(int
i
=
0;
i
<
imageCount;
i++)
{
pointView
=
new
View(context);
pointView.setBackgroundResource(R.drawable.indicator_selector);
if
(i
==
lastPosition)
{
pointView.setSelected(true);
}
else
{
pointView.setSelected(false);
}
indicator.addView(pointView,
layoutParams);
}
}
private
class
MyViewPageAdapter
extends
PagerAdapter
{
@Override
public
int
getCount()
{
return
Integer.MAX_VALUE;
}
@Override
public
boolean
isViewFromObject(View
view,
Object
object)
{
return
view
==
object;
}
@Override
public
Object
instantiateItem(ViewGroup
container,
final
int
position)
{
final
ImageView
imageView
=
new
ImageView(container.getContext());
imageView.setImageResource(defaultImageResource);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
final
Image
image
=
imageUrls.get(position
%
imageCount);
imageLoader.displayImage(image.getImageUrl(),
imageView,
options);
imageView.setOnClickListener(new
OnClickListener()
{
@Override
public
void
onClick(View
v)
{
if
(listener
!=
null)
{
listener.onClick(v,
position
%
imageCount,
image.getAction(),
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年上半年安徽安慶桐城電力配電運(yùn)維人員招聘配電線路運(yùn)維人員12人易考易錯(cuò)模擬試題(共500題)試卷后附參考答案
- 2025年上半年寧波余姚市鹿亭鄉(xiāng)人民政府招考村級(jí)勞保協(xié)理員(2名)易考易錯(cuò)模擬試題(共500題)試卷后附參考答案
- 2025年上半年寧夏賀蘭縣招募普通高校畢業(yè)生到事業(yè)單位實(shí)習(xí)168人易考易錯(cuò)模擬試題(共500題)試卷后附參考答案
- 【2025】河北省外貿(mào)資產(chǎn)經(jīng)營(yíng)有限公司選聘會(huì)計(jì)師事務(wù)所筆試考點(diǎn)考試試題及答案
- 2024遼寧沈陽(yáng)盛京資產(chǎn)管理集團(tuán)有限公司所屬子公司沈陽(yáng)盛京私募基金管理有限公司招聘2人筆試參考題庫(kù)附帶答案詳解
- 高中生物細(xì)胞核1練習(xí)浙科版必修1
- 遼寧省示范校北票市尹湛納希高級(jí)中學(xué)高中政治1.1人民民主專政本質(zhì)是人民當(dāng)家作主學(xué)案新人教版必修2
- 2024浙江麗水市松陽(yáng)縣建投集團(tuán)下屬子公司松陽(yáng)縣工程建設(shè)監(jiān)理有限公司招聘監(jiān)理員3人筆試參考題庫(kù)附帶答案詳解
- 分批交付貨物合同范本
- 2024年下半年廣東惠州市城市建設(shè)投資集團(tuán)有限公司校園招聘24人筆試參考題庫(kù)附帶答案詳解
- 2024年資格考試-WSET二級(jí)認(rèn)證考試近5年真題集錦(頻考類試題)帶答案
- 2022塔式太陽(yáng)能熱發(fā)電站吸熱系統(tǒng)施工規(guī)范
- 七年級(jí)下學(xué)期數(shù)學(xué)開(kāi)學(xué)第一課課件
- 接從事管供水人員衛(wèi)生知識(shí)培訓(xùn)試題
- 江西新余特別重大火災(zāi)事故調(diào)查報(bào)告公布吸取教訓(xùn)研討發(fā)言稿
- 上海市建設(shè)工程監(jiān)理施工安全監(jiān)督規(guī)程(DGTJ-08-2035-2024)
- 生態(tài)河道治理工程施工組織設(shè)計(jì)
- ip形象設(shè)計(jì)合同協(xié)議書(shū)
- 五下音樂(lè)《馴鹿、冬獵、鴻雁(簡(jiǎn)譜、五線譜)》課件
- 部編人教版九年級(jí)下冊(cè)語(yǔ)文-第5單元-17-屈原-課件-課件
- 2024版PLC控制系統(tǒng)合同
評(píng)論
0/150
提交評(píng)論