这是githud上地址 https://github.com/saiwu-bigkoo/Android-ConvenientBanner
如果只实现简单的效果 首先导入依赖
compile 'com.bigkoo:convenientbanner:1.1.4'
布局中引用<com.bigkoo.convenientbanner.ConvenientBanner
android:layout_width="match_parent"
android:id="@+id/viewpager"
android:layout_height="200dp">
</com.bigkoo.convenientbanner.ConvenientBanner>
主Activity代码package com.example.longfei.viewpagerwuxian;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.bigkoo.convenientbanner.CBViewHolderCreator;
import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.OnItemClickListener;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements OnItemClickListener {
private ConvenientBanner viewpager;
private List<Integer> data = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
data.add(R.mipmap.a2);
data.add(R.mipmap.a16);
data.add(R.mipmap.a28);
data.add(R.mipmap.a6);
data.add(R.mipmap.a20);
data.add(R.mipmap.a21);
data.add(R.mipmap.a32);
viewpager = (ConvenientBanner) findViewById(R.id.viewpager);
viewpager.setPages(new CBViewHolderCreator() {
@Override
public Object createHolder() {
//我们需要实现一个Holder
return new NetworkImageHolderView();
}
},data)
//设置我们现实的动画
.setPageTransformer(ConvenientBanner.Transformer.RotateDownTransformer)
.startTurning(2000).setPageIndicator(new int[]{R.drawable.select,R.drawable.def})
.setOnItemClickListener(this); }
@Override public void onItemClick(int position) {
}
//实现的holder
package com.example.longfei.viewpagerwuxian;
import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import com.bigkoo.convenientbanner.CBPageAdapter;
/**
* Created by longfei on 2016/10/14.
*/
public class NetworkImageHolderView implements CBPageAdapter.Holder<Integer> {
ImageView imageView;
@Override
public View createView(Context context) {
imageView = new ImageView(context);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
return imageView;
}
@Override
public void UpdateUI(Context context, int position, Integer data) {
imageView.setImageResource(data);
}
}
}
可以自定义两个小圆点
def
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<size android:width="5dp" android:height="5dp"/>
<solid android:color="#ccc" />
<corners android:radius="9dp" />
<stroke android:width="1dp" android:color="#0f0"/>
</shape>
select
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="#f00" />
<stroke android:width="1dp" android:color="#0f0"/>
<size android:width="5dp" android:height="5dp"/>
</shape>
这就可以了 记住如果是本地图片 holder里是integer类型 网络请求的是String