com.microsoft.mimickeralarm.onboarding.BubblePagerIndicator.java Source code

Java tutorial

Introduction

Here is the source code for com.microsoft.mimickeralarm.onboarding.BubblePagerIndicator.java

Source

/*
 *
 * Copyright (c) Microsoft. All rights reserved.
 * Licensed under the MIT license.
 *
 * Project Oxford: http://ProjectOxford.ai
 *
 * Project Oxford Mimicker Alarm Github:
 * https://github.com/Microsoft/ProjectOxford-Apps-MimickerAlarm
 *
 * Copyright (c) Microsoft Corporation
 * All rights reserved.
 *
 * MIT License:
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

package com.microsoft.mimickeralarm.onboarding;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.widget.Button;

public class BubblePagerIndicator extends Button implements ViewPager.OnPageChangeListener {
    private Paint mPaint;
    private int mPosition;
    private int mTotalPositions;

    public BubblePagerIndicator(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.FILL);
        mTotalPositions = 0;

        invalidate();
    }

    public BubblePagerIndicator(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

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

    public void setTotalPositions(int totalPositions) {
        mTotalPositions = totalPositions;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int width = canvas.getWidth();
        int height = canvas.getHeight();
        if (mTotalPositions == 0) {
            return;
        }
        mPaint.setAlpha(122);
        float buckets = (float) width / mTotalPositions;
        float radius = Math.min(buckets / 2f, height / 2f);
        for (int i = 0; i < mTotalPositions; i++) {
            canvas.drawCircle(i * buckets + buckets / 2f, height / 2f, radius, mPaint);
        }
        mPaint.setAlpha(255);
        canvas.drawCircle(mPosition * buckets + buckets / 2f, height / 2f, radius, mPaint);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        mPosition = position;
        invalidate();
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }
}