git地址:https://github.com/H07000223/FlycoBanner_Master
導入FlaycoBanner框架依賴:
compile 'com.flyco.banner:FlycoBanner_Lib:2.0.2@aar'//banner
compile 'com.nineoldandroids:library:2.4.0'//banner
導入Glide框架依賴:
compile 'com.github.bumptech.glide:glide:3.7.0'
自定義類SimpleImageBanner繼承BaseIndicatorBanner<BannerItem, SimpleImageBanner>:
public class SimpleImageBanner extends BaseIndicatorBanner<BannerItem, SimpleImageBanner> {
private ColorDrawable colorDrawable;
public SimpleImageBanner(Context context) {
this(context,null,0);
}
public SimpleImageBanner(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SimpleImageBanner(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
colorDrawable = new ColorDrawable(Color.parseColor("#555555"));
}
@Override
public void onTitleSlect(TextView tv, int position) {
BannerItem bannerItem=mDatas.get(position);
tv.setText(bannerItem.title);
}
@Override
public View onCreateItemView(int position) {
View inflate = View.inflate(mContext, R.layout.layout_banner, null);
ImageView imageView = ViewFindUtils.find(inflate, R.id.iv);
Log.i("bannerItem.title", "bannerItem.title: "+imageView);
BannerItem bannerItem=mDatas.get(position);
int itemWidth=mDisplayMetrics.widthPixels;
int itemHight=(int) (itemWidth * 360 * 1.0f / 640);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new LinearLayout.LayoutParams(itemWidth,itemHight));
String imgUrl=bannerItem.imgUrl;
if (!TextUtils.isEmpty(imgUrl)){ Glide.with(mContext).load(imgUrl).override(itemWidth,itemHight).centerCrop().placeholder(R.mipmap.ic_launcher).into(imageView);
}else {
imageView.setImageDrawable(colorDrawable);
}
return inflate;
}
}
資源類DataProvider:
public class DataProvider {
public static String[] titles = new String[]{
"歡樂頌:五美破案總動員",
"無心法師:生死離別!月牙遭虐殺",
"花千骨:尊上淪為花千骨",
"綜藝飯:胖軒偷看夏天洗澡掀波瀾",
"碟中諜4:阿湯哥高塔命懸一線,超越不可能",
};
public static String[] urls = new String[]{//640*360 360/640=0.5625
"http://r2.ykimg.com/050C0000571EB4F867BC3D14E908D907",//"歡樂頌:五美破案總動員"
"http://photocdn.sohu.com/tvmobilemvms/20150907/144158380433341332.jpg",//無心法師:生死離別!月牙遭虐殺
"http://photocdn.sohu.com/tvmobilemvms/20150907/144160286644953923.jpg",//花千骨:尊上淪為花千骨
"http://photocdn.sohu.com/tvmobilemvms/20150902/144115156939164801.jpg",//綜藝飯:胖軒偷看夏天洗澡掀波瀾
"http://photocdn.sohu.com/tvmobilemvms/20150907/144159406950245847.jpg",//碟中諜4:阿湯哥高塔命懸一線,超越不可能
};
public static ArrayList<BannerItem> getList() {
ArrayList<BannerItem> list = new ArrayList<>();
for (int i = 0; i < urls.length; i++) {
BannerItem item = new BannerItem();
item.imgUrl = urls[i];
item.title = titles[i];
list.add(item);
}
return list;
}
}
findViewById幫助類ViewFindUtils:
public class ViewFindUtils
{
/**
* ViewHolder簡潔寫法,避免適配器中重復定義ViewHolder,減少代碼量 用法:
*
* <pre>
* if (convertView == null)
* {
* convertView = View.inflate(context, R.layout.ad_demo, null);
* }
* TextView tv_demo = ViewHolderUtils.get(convertView, R.id.tv_demo);
* ImageView iv_demo = ViewHolderUtils.get(convertView, R.id.iv_demo);
* </pre>
*/
public static <T extends View> T hold(View view, int id)
{
SparseArray<View> viewHolder = (SparseArray<View>) view.getTag();
if (viewHolder == null)
{
viewHolder = new SparseArray<>();
view.setTag(viewHolder);
}
View childView = viewHolder.get(id);
if (childView == null)
{
childView = view.findViewById(id);
viewHolder.put(id, childView);
}
return (T) childView;
}
/**
* 替代findviewById方法
*/
public static <T extends View> T find(View view, int id)
{
return (T) view.findViewById(id);
}
}
實體BannerItem:
public class BannerItem {
public String imgUrl;
public String title;
}
MainActivity:
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
SimpleImageBanner simpleImageBanner= (SimpleImageBanner) findViewById(R.id.ban);
simpleImageBanner.setSource(DataProvider.getList()).startScroll();
simpleImageBanner.setOnItemClickL(new BaseBanner.OnItemClickL() {
@Override
public void onItemClick(int position) {
Toast.makeText(MainActivity.this,"position->"+position,Toast.LENGTH_SHORT).show();
}
});
}
}
主布局文件main_layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/layout_simpbanner"/>
</LinearLayout>
輪播圖中的imageview:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"/>
</LinearLayout>
輪播SimpleImageBanner:
<com.cutv.myflaycobanner.SimpleImageBanner xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:banner="http://schemas.android.com/apk/res-auto"
android:id="@+id/ban"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
banner:bb_barColor="#88000000"
banner:bb_barPaddingBottom="5dp"
banner:bb_barPaddingLeft="10dp"
banner:bb_barPaddingRight="10dp"
banner:bb_barPaddingTop="5dp"
banner:bb_delay="5"
banner:bb_indicatorCornerRadius="3dp" //指示器圓點半徑
banner:bb_indicatorGap="8dp" //兩個指標之間的差距筐骇,dp,默認的6dp
banner:bb_indicatorGravity="RIGHT" //指示器位置
banner:bb_indicatorHeight="5dp"
banner:bb_indicatorSelectColor="#07a7e1"
banner:bb_indicatorStyle="CORNER_RECTANGLE" //指示器樣式:矩形
banner:bb_indicatorUnselectColor="#88ffffff"
banner:bb_indicatorWidth="5dp"
banner:bb_isAutoScrollEnable="true"
banner:bb_isBarShowWhenLast="true" //當位置是最后的時候江滨,設置底部的欄顯示
banner:bb_isIndicatorShow="true"
banner:bb_isLoopEnable="true" //是否為LoopViewPager铛纬,默認為true
banner:bb_isTitleShow="true" //設置標題顯示或不顯示,默認為true
banner:bb_period="5" //滾動周期唬滑,單位秒告唆,默認5秒
banner:bb_scale="0.5" //橫幅高度和寬度,高度/屏幕寬度
banner:bb_textColor="#ffffff"
banner:bb_textSize="13.5sp" />