在开发中,时常要用到图片轮播的功能,现记录一下:
首先自定义ImageLayout
public class ImagesShowLayout extends RelativeLayout {
private Context mContext;
private ViewPager mVpImages;
private LinearLayout mLlImagesHintGroup;
private ImageView[] mHintViews; 点点数组
private List<ImageView> mImageViewsList = new ArrayList<>();//ImageView列表
private List<String> mImageUrlList = new ArrayList<>(); //图片资源
private PagerAdapter pagerAdapter;
public ImagesShowLayout(Context context) {
super(context);
this.mContext = context;
initView();
}
public ImagesShowLayout(Context context, AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
initView();
}
public ImagesShowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.mContext = context;
initView();
}
public void setImagesList(List<String> imagesList) {
mImageUrlList.addAll(imagesList);
initInvitationImages();
initInvitationHintImages();
mVpImages.setAdapter(new PagerAdapter() {
@Override
public int getCount() {
return mImageUrlList.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(mImageViewsList.get(position));
return mImageViewsList.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(mImageViewsList.get(position));
}
});
initListener();
}
private void initView() {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context
.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.view_images_show_layout, this);
mVpImages = (ViewPager) findViewById(R.id.vp_images);
mLlImagesHintGroup = (LinearLayout) findViewById(R.id.ll_images_hint_group);
}
private void initListener() {
//图片滑动监听
mVpImages.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
//设置下方点点图片
mHintViews[position].setBackgroundResource(R.drawable.shape_round_rec_white_3dp);
for (int i = 0; i < mHintViews.length; i++) {
if (position != i) {
mHintViews[i].setBackgroundResource(R.drawable.shape_round_white_6dp);
}
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
/**
* 初始化图片
*/
private void initInvitationImages() {
LinearLayout.LayoutParams mLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
mLayoutParams.setMargins(0, 0, 0, 0);//setMargins(left, top, right, bottom);
for (String imgUrl : mImageUrlList) {
ImageView imageView = new ImageView(mContext);
imageView.setScaleType(ImageView.ScaleType.CENTER);
Glide.with(mContext).load(imgUrl).asBitmap()
.centerCrop().into(imageView);//
// imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT
// , ViewGroup.LayoutParams.WRAP_CONTENT));
imageView.setLayoutParams(mLayoutParams);
mImageViewsList.add(imageView);
}
}
/**
* 动态生成图片底部圆点
*/
private void initInvitationHintImages() {
mHintViews = new ImageView[mImageUrlList.size()];
//动态生成ImageView,实现底部圆点(圆点数量根据左右滑动View而确定)
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10, 0, 0, 0);//setMargins(left, top, right, bottom);
for (int i = 0; i < mImageUrlList.size(); i++) {
ImageView hintView = new ImageView(mContext);
hintView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT
, ViewGroup.LayoutParams.WRAP_CONTENT));
hintView.setLayoutParams(layoutParams);
//imageView.setPadding(10, 0, 10 , 0);
mHintViews[i] = hintView;
if (i == 0) {
// 默认选中第一张图片
mHintViews[i].setBackgroundResource(R.drawable.shape_round_rec_white_3dp);
} else {
mHintViews[i].setBackgroundResource(R.drawable.shape_round_white_6dp);
}
mLlImagesHintGroup.addView(mHintViews[i]);
}
}
}
shape_round_rec_white_3dpx.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffffff" />
<corners android:radius="3dp" />
<size android:width="13dp"
android:height="6dp"/>
</shape>
view_images_show_layout.xml布局如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="@dimen/xxx">
<android.support.v4.view.ViewPager
android:id="@+id/vp_images"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</android.support.v4.view.ViewPager>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_marginBottom="13dp"
android:gravity="center_horizontal|center_vertical">
</LinearLayout>
</RelativeLayout>
网友评论