自定义属性
<resources>
<declare-styleable name="CircleIndictor">
<attr name="mWidth" format="dimension" />
<attr name="mHeight" format="dimension" />
</declare-styleable>
</resources>
自定义控件
public class CircleIndictor extends View {
//空心画笔
private Paint strokePaint;
//总页面个数
int totolCount;
//当前第几个页面
int currentCount;
//半径
int mRadius = 30;
//间距
int mDistance = 20;
private Paint fillPaint;
public CircleIndictor(Context context) {
super(context);
}
public CircleIndictor(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//获得具体的宽高
setMeasuredDimension(getFinalMeasuredWidth(widthMeasureSpec), getFinalMeasuredHeight
(heightMeasureSpec));
}
//高
private int getFinalMeasuredHeight(int heightMeasureSpec) {
//模式和大小
int mode = MeasureSpec.getMode(heightMeasureSpec);
int size = MeasureSpec.getSize(heightMeasureSpec);
int finalHeight = 0;
switch (mode) {
case MeasureSpec.EXACTLY://精确模式
finalHeight = size;
break;
case MeasureSpec.AT_MOST:
finalHeight = getPaddingTop() + getPaddingBottom() + 2 * mRadius;
break;
}
return finalHeight;
}
//宽
private int getFinalMeasuredWidth(int widthMeasureSpec) {
//模式和大小
int mode = MeasureSpec.getMode(widthMeasureSpec);
int size = MeasureSpec.getSize(widthMeasureSpec);
int finalWith = 0;
switch (mode) {
case MeasureSpec.EXACTLY:
finalWith = size;
break;
case MeasureSpec.AT_MOST:
finalWith = getPaddingLeft() + mRadius +
(totolCount - 1) * (2 * mRadius + mDistance) +
getPaddingRight() + mRadius;
break;
}
return finalWith;
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//画两组圆
//一组空心圆
//空心画笔
strokePaint = new Paint();
strokePaint.setStyle(Paint.Style.STROKE);
strokePaint.setColor(Color.RED);
//实心圆画笔
fillPaint = new Paint();
fillPaint.setStyle(Paint.Style.FILL);
strokePaint.setColor(Color.BLACK);
//画圆
for (int i = 1; i <= totolCount; i++) {
//计算公式:getPaddingLeft+R+(圆个数-1)*(2R+间距)
//计算X轴坐标
canvas.drawCircle(getPaddingLeft() + mRadius + (i - 1) * (2 * mRadius + mDistance),
getPaddingTop() + mRadius,
mRadius,
strokePaint);
}
//一个实心圆
canvas.drawCircle(getPaddingLeft() + mRadius + (currentCount) * (2 * mRadius + mDistance),
getPaddingTop() + mRadius,
mRadius,
fillPaint);
}
//获取总数
public void getCount(int totolCount) {
this.totolCount = totolCount;
}
//获取当前到底是哪个被选中 获取页面下标
public void getCurrentCount(int cuttenCount) {
this.currentCount = cuttenCount;
//重绘方法
invalidate();
}
}
Layout
<android.support.v4.view.ViewPager
android:id="@+id/vp_container"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v4.view.ViewPager>
<com.songyang.com.lx_0104.widget.CircleIndictor
android:id="@+id/ci_indictor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
app:mHeight="40dp"
app:mWidth="500dp" />
Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
//事务
FragmentManager manager = getSupportFragmentManager();
//数据源
List<Fragment> fragments = new ArrayList<>();
HomeFragment homeFragment = new HomeFragment();
DetailFragment detailFragment = new DetailFragment();
MyFragment myFragment = new MyFragment();
fragments.add(homeFragment);
fragments.add(detailFragment);
fragments.add(myFragment);
//设置适配器
GuideAdapter adapter = new GuideAdapter(manager, fragments);
vpContainer.setAdapter(adapter);
//在自定义View中传入总页面个数
int count = adapter.getCount();
ciIndictor.getCount(count);
//页面选中
//当前哪个页面被选中
vpContainer.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
ciIndictor.getCurrentCount(i);
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
}