轮播图带小点的

private void startRool() 
{
// 开始轮播
handler.sendEmptyMessageDelayed(0,2000);

}

//小点的设置

private void initDots() 
{
dotsList = new ArrayList<ImageView>();
dotsList.clear();
// 移除小点
lines.removeAllViews();
for (int i = 0; i <picUrls.length; i++) 
{
ImageView imageView = new ImageView(getActivity());
Drawable drawable = null;
if (i == 0) 
{
// 亮色图片
drawable = getResources().getDrawable(R.drawable.dots_focuse);
} else 
{
drawable = getResources().getDrawable(R.drawable.dots_normal);
}
imageView.setImageDrawable(drawable);
// 考虑屏幕适配
LayoutParams params = new LayoutParams(dip2px(this, 10), dip2px(this, 10));
params.setMargins(dip2px(this, 5), 0, dip2px(this, 5),0);
lines.addView(imageView, params);
// 将小点的控件添加到集合中
dotsList.add(imageView);
}
}

//像素设置

public int dip2px(Fragment1 fragment1, float dpValue) 
{
final float scale = fragment1.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}

//两个状态的设置

//dots_focuse.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <solid android:color="#ff0000" />
    <corners android:radius="8dp" />
</shape>

//dots_normal.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <solid android:color="#88000000" />
    <corners android:radius="8dp" />
</shape>

//viewpager的监听

viewpager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int position) 
{
for (int i = 0;i<picUrls.length;i++) 
{
if (i == position % picUrls.length) 
{
dotsList.get(i).setImageDrawable(getResources().getDrawable(R.drawable.dots_focuse));

else 
{
dotsList.get(i).setImageDrawable(getResources().getDrawable(R.drawable.dots_normal));
}
}
}

展开阅读全文
博主设置当前文章不允许评论。

没有更多推荐了,返回首页