com.zhi.android.libs.pager.PageIndicatorView.java Source code

Java tutorial

Introduction

Here is the source code for com.zhi.android.libs.pager.PageIndicatorView.java

Source

/*
 * Copyright [2017] [zhi]
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zhi.android.libs.pager;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.ColorInt;
import android.support.annotation.IntRange;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;

public class PageIndicatorView extends View implements ViewPager.OnPageChangeListener {

    @ColorInt
    private final int mNormalColor;
    @ColorInt
    private final int mSelectedColor;
    private final boolean mAnimated;
    private final float mRadius;
    private final float mSpacing;
    private final Paint mPaint;
    private int mIndicatedPages;
    private int mIndicatedPosition;
    private float mIndicatedOffset;

    @Nullable
    private PageIndicatorInterface mPageIndicatorInterface;

    public PageIndicatorView(Context context) {
        this(context, null);
    }

    public PageIndicatorView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, R.attr.pageIndicatorViewStyle);
    }

    public PageIndicatorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public PageIndicatorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);

        final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PageIndicatorView, defStyleAttr,
                defStyleRes);
        mNormalColor = a.getColor(R.styleable.PageIndicatorView_pageIndicatorNormalColor, 0);
        mSelectedColor = a.getColor(R.styleable.PageIndicatorView_pageIndicatorSelectedColor, 0);
        mAnimated = a.getBoolean(R.styleable.PageIndicatorView_pageIndicatorAnimated, true);
        mRadius = a.getDimensionPixelOffset(R.styleable.PageIndicatorView_pageIndicatorRadius, 0);
        mSpacing = a.getDimensionPixelOffset(R.styleable.PageIndicatorView_pageIndicatorSpacing, 0);
        a.recycle();

        mPaint = new Paint();
        mPaint.setAntiAlias(false);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // Get the center point location.
        final float centerX = (getRight() - getLeft()) / 2;
        final float centerY = (getBottom() - getTop()) / 2;

        // Calculate the start offset relative the start
        final float offset = centerX - (mIndicatedPages / 2 - ((mIndicatedPages & 1) == 0 ? 0.5f : 0f)) * mSpacing;

        // draw normal indicator.
        mPaint.setColor(mNormalColor);
        for (int i = 0; i < mIndicatedPages; i++) {
            canvas.drawCircle(offset + i * mSpacing, centerY, mRadius, mPaint);
        }
        // draw selected indicator.
        mPaint.setColor(mSelectedColor);
        canvas.drawCircle(offset + (mIndicatedPosition + mIndicatedOffset) * mSpacing, centerY, mRadius, mPaint);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        if (mAnimated) {
            mIndicatedPosition = Math.min(position, mIndicatedPages - 1);
            mIndicatedOffset = mIndicatedPosition == mIndicatedPages - 1 ? 0 : positionOffset;
            invalidate();
        }
    }

    @Override
    public void onPageSelected(int position) {
        if (mPageIndicatorInterface != null) {
            if (!mAnimated) {
                mIndicatedPosition = Math.min(position, mIndicatedPages - 1);
                mIndicatedOffset = 0;
                invalidate();
            }
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        // LEFT EMPTY
    }

    public void setPageIndicatorInterface(@Nullable PageIndicatorInterface pageIndicatorInterface) {
        mPageIndicatorInterface = pageIndicatorInterface;
        if (mPageIndicatorInterface != null) {
            mIndicatedPages = mPageIndicatorInterface.getPageIndicatedCount();
        } else {
            mIndicatedPages = 0;
        }
    }

    /**
     * Page indicator interface. It should be implemented by the host object.
     */
    public interface PageIndicatorInterface {
        /**
         * Get a specified view indicated pages. For a ViewPager,
         * it maybe not the {@link PagerAdapter#getCount()},
         */
        @IntRange(from = 0)
        int getPageIndicatedCount();
    }
}