效果图:
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="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="360dp">
android.support.v4.view.ViewPager>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="35dp"
android:orientation="vertical"
android:layout_marginTop="15dp"
android:padding="5dp">
<LinearLayout
android:id="@+id/dot_layout"
android:layout_width="200dp"
android:layout_height="15dp"
android:layout_marginTop="5dp"
android:layout_gravity="center"
android:orientation="horizontal"
>
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/gv_vampire"
android:gravity="center"
android:stretchMode="columnWidth"/>
LinearLayout>
LinearLayout>
/**
* Created by chenyang on 16/9/21.
*
* 轮播图封装类 , 解析viewpager图片用的Glide
* compile 'com.github.bumptech.glide:glide:3.5.2'
* 在onstart 和 ondestroy 分别调用startCarouselView,destroyCarouselView
* 得到PicUrls , 然后start
*/
public class CarouselView extends LinearLayout {
private Context context;
private ViewPager viewPager;
private LinearLayout dotLayout;
private List dotList; //指示点
// 点的集合 (需要传入的1)
private int[] points = {R.mipmap.lingxingshenhui,R.mipmap.lingxingqianhui};
// 轮播图 图片集合
private List picUrls ;
private GridView gridView;
private GridViewAdapter gridViewAdapter;
private int viewPagerSize;
public void setPicUrls(List picUrls){
this.picUrls = picUrls;
}
public void setContext(Context context) {
this.context = context;
}
Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
List finalyList = new ArrayList<>();
for (int i = 0; i < picUrls.size(); i++) {
if (i == viewPager.getCurrentItem() % picUrls.size()) {
finalyList.add(points[1]);
} else {
finalyList.add(points[0]);
}
}
gridViewAdapter.setList(finalyList);
Message m = new Message();
m.what = 1;
handler.sendMessageDelayed(m, 4000);
return false;
}
});
public void startCarouselView() {
Message message = handler.obtainMessage();
message.what = 1;
handler.sendMessageDelayed(message, 4000);
}
public void destroyCarouselView() {
handler.removeMessages(1);
}
public CarouselView(Context context) {
this(context, null);
}
public CarouselView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
init();
}
private void init() {
LayoutInflater.from(context).inflate(R.layout.carousel_layout, this, true);
initView();
initData();
}
private void initData() {
dotList = new ArrayList<>();
}
private void initView() {
viewPager = (ViewPager) findViewById(R.id.viewPager);
dotLayout = (LinearLayout) findViewById(R.id.dot_layout);
gridView = (GridView) findViewById(R.id.gv_vampire);
// 轮播图表示点视图
gridViewAdapter = new GridViewAdapter(context);
}
public void start(final List picUrls) {
this.picUrls = picUrls;
if (this.picUrls == null || this.picUrls.size() < 1) {
return;
}
View view = null;
final List integers = new ArrayList<>();
//根据轮播图要显示的数量来创建指示点的个数
for (int i = 0; i < this.picUrls.size(); i++) {
view = new View(context);
if (i == 0) {
integers.add(points[1]);
} else {
integers.add(points[0]);
}
dotList.add(view); //加入到list集合中
dotLayout.addView(view); //加入父布局
}
gridView.setNumColumns(this.picUrls.size());
gridView.setAdapter(gridViewAdapter);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
List finalyList = new ArrayList<>();
for (int i = 0; i < integers.size(); i++) {
viewPagerSize = viewPager.getCurrentItem() % integers.size();
if (i == viewPagerSize) {
finalyList.add(points[1]);
} else {
finalyList.add(points[0]);
}
}
gridViewAdapter.setList(finalyList);
switch (state) {
case ViewPager.SCROLL_STATE_DRAGGING ://当手按上去的时候 , remove掉handler
handler.removeCallbacksAndMessages(null);
break;
case ViewPager.SCROLL_STATE_IDLE://当手按上去的时候 , remove掉handler,在开始轮播
handler.removeCallbacksAndMessages(null);
startCarouselView();
break;
case ViewPager.SCROLL_STATE_SETTLING:
break;
default:
break;
}
}
});
gridViewAdapter.setList(integers);
// viewPager.setPageMargin(100);//两个页面之间的间距
viewPager.setAdapter(new MyPagerAdapter());
viewPager.setCurrentItem(3000, false);
viewPager.setPageTransformer(true , new MyPageTransformer());
}
class MyPagerAdapter 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 String pic = picUrls.get(position % picUrls.size());
ImageView imageView = new ImageView(context);
Glide.with(context)
.load(pic)
.into(imageView);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
container.addView(imageView);
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// clickCallback.onClick(0, position);
}
});
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
ClickCallback clickCallback;
interface ClickCallback {
void onClick(int id, int position);
}
public void setClickCallback(ClickCallback clickCallback) {
this.clickCallback = clickCallback;
}
/**
* Created by chenyang on 16/9/21.
* GridView , 用来显示轮播图的点
*/
public class GridViewAdapter extends BaseAdapter {
private Context context;
private List list;
private MyViewHolder myViewHolder;
public GridViewAdapter(Context context) {
this.context = context;
}
@Override
public int getCount() {
int count =list != null ? list.size() : 0;
return count;
}
public void setList(List list) {
this.list = list;
notifyDataSetChanged();
}
@Override
public Object getItem(int position) {
if (myViewHolder !=null){
return myViewHolder.imageView;
}
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.carousel_point,parent,false);
myViewHolder = new MyViewHolder(convertView);
convertView.setTag(myViewHolder);
} else {
myViewHolder = (MyViewHolder) convertView.getTag();
}
myViewHolder.imageView.setImageResource(list.get(position));
return convertView;
}
private class MyViewHolder {
private ImageView imageView;
public MyViewHolder(View view) {
imageView = (ImageView) view.findViewById(R.id.iv_vampire);
}
}
}
/**
* Created by chenyang on 16/9/23.
* viewpager 立方体切换效果
*/
public class MyPageTransformer implements ViewPager.PageTransformer {
public void transformPage(View view, float position) {
//左边0~-90度,右边90~0度,
//左边x 0~-width,右边x width~0;
if (position < -1) {
} else if (position <= 1) // a页滑动至b页 ; a页从 0.0 ~ -1 ;b页从1 ~ 0.0
{ // [-1,1]
if (position < 0)//滑动中左边页面
{
view.setPivotX(view.getMeasuredWidth());
view.setRotationY(position * 45);
} else//滑动中右边页面
{
view.setPivotX(0);
view.setRotationY(position * 45);
}
} else { // (1,+Infinity]
}
}
}
/**
* Created by chenyang on 16/9/23.
* viewpager 放大缩小切换效果
*/
public class ZoomOutPageTransformer implements ViewPager.PageTransformer {
private static final float MIN_SCALE = 0.85f;
private static final float MIN_ALPHA = 0.5f;
@SuppressLint("NewApi")
public void transformPage(View view, float position){
int pageWidth = view.getWidth();
int pageHeight = view.getHeight();
if (position < -1)
{ // [-Infinity,-1)
// This page is way off-screen to the left.
view.setAlpha(0);
} else if (position <= 1) //a页滑动至b页 ; a页从 0.0 -1 ;b页从1 ~ 0.0
{ // [-1,1]
// Modify the default slide transition to shrink the page as well
float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
float vertMargin = pageHeight * (1 - scaleFactor) / 2;
float horzMargin = pageWidth * (1 - scaleFactor) / 2;
if (position < 0)
{
view.setTranslationX(horzMargin - vertMargin / 2);
} else
{
view.setTranslationX(-horzMargin + vertMargin / 2);
}
// Scale the page down (between MIN_SCALE and 1)
view.setScaleX(scaleFactor);
view.setScaleY(scaleFactor);
// Fade the page relative to its size.
view.setAlpha(MIN_ALPHA + (scaleFactor - MIN_SCALE)
/ (1 - MIN_SCALE) * (1 - MIN_ALPHA));
} else
{ // (1,+Infinity]
// This page is way off-screen to the right.
view.setAlpha(0);
}
}
}
/**
* Created by chenyang on 16/9/22.
* viewpager 前后深浅不同切换动画
*/
public class DepthPageTransformer implements ViewPager.PageTransformer {
private static final float MIN_SCALE = 0.75f;
public void transformPage(View view, float position) {
int pageWidth = view.getWidth();
if (position < -1) { // [-Infinity,-1)
// This page is way off-screen to the left.
view.setAlpha(0);
} else if (position <= 0) { // [-1,0]
// Use the default slide transition when moving to the left page
view.setAlpha(1);
view.setTranslationX(0);
view.setScaleX(1);
view.setScaleY(1);
} else if (position <= 1) { // (0,1]
// Fade the page out.
view.setAlpha(1 - position);
// Counteract the default slide transition
view.setTranslationX(pageWidth * -position);
// Scale the page down (between MIN_SCALE and 1)
float scaleFactor = MIN_SCALE
+ (1 - MIN_SCALE) * (1 - Math.abs(position));
view.setScaleX(scaleFactor);
view.setScaleY(scaleFactor);
} else { // (1,+Infinity]
// This page is way off-screen to the right.
view.setAlpha(0);
}
}
}
//在activity或fragment中
private CarouselView carouselView;
//轮播图开始
@Override
protected void onStart() {
super.onStart();
carouselView.startCarouselView();
}
//轮播图结束
@Override
protected void onDestroy() {
super.onDestroy();
carouselView.destroyCarouselView();
}
carouselView = bindView(R.id.viewpager_fragment_designers_recommend_items);
carouselView.setContext(getApplicationContext());
//在网络解析的位置 : 从相应的Bean中得到图片 开始viewPager轮播
carouselView.setPicUrls(designersRecommendItemBean.getData().getIntroduce_images());
carouselView.start(designersRecommendItemBean.getData().getIntroduce_images());