Indictor指示器

//布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.activity.MainActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        ></android.support.v4.view.ViewPager>
    <com.bw.day0104_1.ui.wedigt.CircleIndictor
        android:id="@+id/circle_indictor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        />

</RelativeLayout>

//新建一个view类继承view

package com.bw.day0104_1.ui.wedigt;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class CircleIndictor extends View {

    private Paint strokepaint;
    private Paint fillpaint;
    //总页面数
    int allcount;
    //当前是第几个页面
    int currentcount;
    //半径的大小
    int mRadius=30;
    //间距
    int mDistance=20;

    public CircleIndictor(Context context) {
        super(context);
    }

    public CircleIndictor(Context context,AttributeSet attrs) {
        super(context, attrs);
    }
//5.重写onMeasure方法
    @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+(allcount-1)*(2*mRadius+mDistance)+getPaddingRight()+mRadius;
                    break;
        }
        return finalWith;
    }
//1.重写ondraw方法
    @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);
        fillpaint.setColor(Color.BLUE);
        //画圆
        for (int i=0;i<=allcount;i++){
            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);
    }
    //2.获取总数
    public void getcount(int allcount){
        this.allcount=allcount;
    }
    //3.获取当前到底被那个所选中,获取当前页面的小标
    public void getCurrentCount(int currentcount){
        this.currentcount=currentcount;
        //4.重绘方法
        invalidate();
    }
}

//主页面中

package com.bw.day0104_1.ui.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.bw.day0104_1.R;
import com.bw.day0104_1.ui.frag.Listfrag;
import com.bw.day0104_1.ui.frag.Myfrag;
import com.bw.day0104_1.ui.frag.Showfrag;
import com.bw.day0104_1.ui.wedigt.CircleIndictor;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ViewPager pager;
    private CircleIndictor circle;
    private Myfrag myfrag;
    private Showfrag showfrag;
    private Listfrag listfrag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pager = (ViewPager) findViewById(R.id.pager);
        circle = (CircleIndictor)findViewById(R.id.circle_indictor);
        final List<Fragment> list=new ArrayList<Fragment>();
        myfrag = new Myfrag();
        showfrag = new Showfrag();
        listfrag = new Listfrag();
        list.add(myfrag);
        list.add(showfrag);
        list.add(listfrag);
        //适配器
        pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return list.get(i);
            }

            @Override
            public int getCount() {
                return list.size();
            }
        });
        //把页面数传给自定义view类中
        int count=list.size();
        circle.getcount(count);
        //设置监听事件
        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
            //当前在哪个页面被选中
                circle.getCurrentCount(i);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值