Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rectangular Page Indicator #50

Open
wants to merge 19 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 42 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@

###**PageIndicatorView**
### **PageIndicatorView**
[ ![Download](https://api.bintray.com/packages/romandanylyk/maven/pageindicatorview/images/download.svg) ](https://bintray.com/romandanylyk/maven/pageindicatorview/_latestVersion)[![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-PageIndicatorView-green.svg?style=true)](https://android-arsenal.com/details/1/4555)
[![API](https://img.shields.io/badge/API-14%2B-brightgreen.svg?style=flat)](https://android-arsenal.com/api?level=14)


`PageIndicatorView` will simplify your life while you working with Android `ViewPager` and need to indicate selected page. It's easy to setup and customize as you need with run-time preview rendering.
`PageIndicatorView` is light library to indicate ViewPager's selected page with different animations and ability to customize it as you need.

![](https://github.com/romandanylyk/PageIndicatorView/blob/master/assets/preview_anim_drop.gif?raw=true)
![](https://raw.githubusercontent.com/romandanylyk/PageIndicatorView/master/assets/preview_anim_drop.gif)

###**Integration**
### **Integration**
To add `pageindicatorview` to your project, first make sure in root `build.gradle` you have specified the following repository:
```java
```groovy
repositories {
jcenter()
}
Expand All @@ -20,12 +20,24 @@ To add `pageindicatorview` to your project, first make sure in root `build.gradl
Once you make sure you have `jcenter` repository in your project, all you need to do is to add the following line in `dependencies` section of your project `build.gradle`.

See latest library version [ ![Download](https://api.bintray.com/packages/romandanylyk/maven/pageindicatorview/images/download.svg) ](https://bintray.com/romandanylyk/maven/pageindicatorview/_latestVersion)
```java
```groovy
compile 'com.romandanylyk:pageindicatorview:X.X.X'
```
Keep in mind, that `PageIndicatorView` has min [API level 14](https://developer.android.com/about/dashboards/index.html)
If your project already use `appcompat-v7` support library, you can omit `PageIndicatorView` dependencies by adding a single .aar file to your project, that will decrease total amount of methods used in your project.

```groovy
compile 'com.romandanylyk:pageindicatorview:X.X.X@aar'
```

Keep in mind, that `PageIndicatorView` has min [API level 14](https://developer.android.com/about/dashboards/index.html) and these dependencies:

```groovy
compile 'com.android.support:support-annotations:25.3.0'
compile 'com.android.support:support-compat:25.3.0'
compile 'com.android.support:support-core-ui:25.3.0'
```

###**Usage Sample**
### **Usage Sample**
During implementation of `PageIndicatorView` I tried to make it's setup as easy as possible.
After you set adapter to your `ViewPager`, all you need to do is to `setViewPager()` and that's it! `PageIndicatorView` will get count from your adapter and start working with instance of your `ViewPager` automatically.

Expand All @@ -48,18 +60,23 @@ Keep in mind that all public methods are also exist as attributes, so you can ev
attrs:piv_viewPager="@id/viewPager"/>
```

###**Customization**
### **Customization**
One of the most important feature of every custom view is ability to customize its look as user need. By calling the following methods (or attributes) you will be able to customize `PageIndicatorView` as you need.

```java
//set shape
setShape(IndicatorShape shape)
//set size
setCount(int count)
setDynamicCount(boolean dynamicCount)

setCornerRadius(int cornerRadiusDp)
setRadius(int radiusDp)
setPadding(int paddingDp)
setStrokeWidth(int strokeDp)

setAutoVisibility(boolean autoVisibility)
setOrientation(Orientation orientation)

//set color
setUnselectedColor(int color)
setSelectedColor(int color)
Expand All @@ -74,33 +91,28 @@ setProgress(int selectingPosition, float progress)
setSelection(int position)
```

```xml
// set orientation
app:orientation="{vertical||horizontal}"
```

![](https://github.com/romandanylyk/PageIndicatorView/blob/master/assets/attributes.gif?raw=true)

Here you can see all the animations `PageIndicatorView` support.

Name| Support version| Preview
-------- | --- | ---
`AnimationType.NONE`| 0.0.1 | ![anim_none](https://raw.githubusercontent.com/romandanylyk/PageIndicatorView/master/assets/anim_none.gif)
`AnimationType.COLOR`| 0.0.1 |![anim_color](https://raw.githubusercontent.com/romandanylyk/PageIndicatorView/master/assets/anim_color.gif)
`AnimationType.SCALE`| 0.0.1 |![anim_scale](https://raw.githubusercontent.com/romandanylyk/PageIndicatorView/master/assets/anim_scale.gif)
`AnimationType.SLIDE`| 0.0.1 |![anim_slide](https://raw.githubusercontent.com/romandanylyk/PageIndicatorView/master/assets/anim_slide.gif)
`AnimationType.WORM`| 0.0.1 |![anim_worm](https://raw.githubusercontent.com/romandanylyk/PageIndicatorView/master/assets/anim_worm.gif)
`AnimationType.FILL`| 0.0.6 |![anim_worm](https://raw.githubusercontent.com/romandanylyk/PageIndicatorView/master/assets/anim_fill.gif)
`AnimationType.THIN_WORM`| 0.0.7 |![anim_thin_worm](https://raw.githubusercontent.com/romandanylyk/PageIndicatorView/master/assets/anim_thin_worm.gif)
`AnimationType.DROP`| 0.1.0 |![anim_drop](https://raw.githubusercontent.com/romandanylyk/PageIndicatorView/master/assets/anim_drop.gif)
`AnimationType.SWAP`| 0.1.1 |![anim_swap](https://raw.githubusercontent.com/romandanylyk/PageIndicatorView/master/assets/anim_swap.gif)

###**Release Note**
Name| Circle Supported| Circle Preview | Rectangle Supported | Rectangle Preview
-------- | --- | --- | --- | --|
`AnimationType.NONE`| 0.0.1 | ![anim_none](https://raw.githubusercontent.com/romandanylyk/PageIndicatorView/master/assets/anim_none.gif)|TBD|![anim_rect_none](assets/anim_rect_none.gif)
`AnimationType.COLOR`| 0.0.1 |![anim_color](https://raw.githubusercontent.com/romandanylyk/PageIndicatorView/master/assets/anim_color.gif)|TBD|![anim_rect_color](assets/anim_rect_color.gif)
`AnimationType.SCALE`| 0.0.1 |![anim_scale](https://raw.githubusercontent.com/romandanylyk/PageIndicatorView/master/assets/anim_scale.gif)|TBD|![anim_rect_scale](assets/anim_rect_scale.gif)
`AnimationType.SLIDE`| 0.0.1 |![anim_slide](https://raw.githubusercontent.com/romandanylyk/PageIndicatorView/master/assets/anim_slide.gif)|TBD|![anim_rect_slide](assets/anim_rect_slide.gif)
`AnimationType.WORM`| 0.0.1 |![anim_worm](https://raw.githubusercontent.com/romandanylyk/PageIndicatorView/master/assets/anim_worm.gif)|TODO|
`AnimationType.FILL`| 0.0.6 |![anim_worm](https://raw.githubusercontent.com/romandanylyk/PageIndicatorView/master/assets/anim_fill.gif)|TBD|![anim_rect_fill](assets/anim_rect_fill.gif)
`AnimationType.THIN_WORM`| 0.0.7 |![anim_thin_worm](https://raw.githubusercontent.com/romandanylyk/PageIndicatorView/master/assets/anim_thin_worm.gif)|TODO|
`AnimationType.DROP`| 0.1.0 |![anim_drop](https://raw.githubusercontent.com/romandanylyk/PageIndicatorView/master/assets/anim_drop.gif)|TBD|![anim_rect_drop](assets/anim_rect_drop.gif)
`AnimationType.SWAP`| 0.1.1 |![anim_swap](https://raw.githubusercontent.com/romandanylyk/PageIndicatorView/master/assets/anim_swap.gif)|TBD|![anim_rect_swap](assets/anim_rect_swap.gif)

### **Release Note**
See release notes on [github releases](https://github.com/romandanylyk/PageIndicatorView/releases) or [Bintray release notes](https://bintray.com/romandanylyk/maven/pageindicatorview#release).

###**License**
### **License**

Copyright 2016 Roman Danylyk
Copyright 2017 Roman Danylyk

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
Binary file added assets/anim_rect_color.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/anim_rect_drop.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/anim_rect_fill.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/anim_rect_none.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/anim_rect_scale.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/anim_rect_slide.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/anim_rect_swap.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion pageindicatorview/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ext {
siteUrl = 'https://github.com/romandanylyk/PageIndicatorView'
gitUrl = 'https://github.com/romandanylyk/PageIndicatorView.git'

libraryVersion = '0.1.1'
libraryVersion = '0.2.0'

developerId = 'romandanylyk'
developerName = 'Roman Danylyk'
Expand Down
19 changes: 19 additions & 0 deletions pageindicatorview/src/main/java/com/rd/PageIndicatorView.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import android.util.Pair;
import android.view.View;
import com.rd.animation.type.*;
import com.rd.draw.IndicatorShape;
import com.rd.draw.data.Indicator;
import com.rd.draw.data.Orientation;
import com.rd.draw.data.PositionSavedState;
Expand Down Expand Up @@ -395,6 +396,24 @@ public long getAnimationDuration() {
return manager.indicator().getAnimationDuration();
}


/**
* Set the shape to display as an indicator.
* Default animation type is {@link IndicatorShape#CIRCLE}.
*
* @param shape type of shape, one of {@link IndicatorShape}
*/
public void setShape(@Nullable IndicatorShape shape) {
manager.onValueUpdated(null);

if (shape!= null) {
manager.indicator().setShape(shape);
} else {
manager.indicator().setShape(IndicatorShape.CIRCLE);
}
invalidate();
}

/**
* Set animation type to perform while selecting new circle indicator.
* Default animation type is {@link AnimationType#NONE}.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.rd.draw;

public enum IndicatorShape { CIRCLE, RECTANGLE}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.util.AttributeSet;
import android.view.View;
import com.rd.animation.type.*;
import com.rd.draw.IndicatorShape;
import com.rd.draw.data.Indicator;
import com.rd.draw.data.Orientation;
import com.rd.draw.data.RtlMode;
Expand All @@ -28,6 +29,7 @@ public void init(@NonNull Context context, @Nullable AttributeSet attrs) {
initColorAttribute(typedArray);
initAnimationAttribute(typedArray);
initSizeAttribute(typedArray);
initShapeAttribute(typedArray);
typedArray.recycle();
}

Expand Down Expand Up @@ -86,6 +88,11 @@ private void initAnimationAttribute(@NonNull TypedArray typedArray) {
indicator.setRtlMode(rtlMode);
}

private void initShapeAttribute(@NonNull TypedArray typedArray) {
int shapeIndex = typedArray.getInt(R.styleable.PageIndicatorView_piv_indicatorShape,IndicatorShape.CIRCLE.ordinal());
indicator.setShape(getIndicatorShape(shapeIndex));
}

private void initSizeAttribute(@NonNull TypedArray typedArray) {
int orientationIndex = typedArray.getInt(R.styleable.PageIndicatorView_piv_orientation, Orientation.HORIZONTAL.ordinal());
Orientation orientation;
Expand Down Expand Up @@ -123,11 +130,29 @@ private void initSizeAttribute(@NonNull TypedArray typedArray) {
stroke = 0;
}

int rectWidth = (int) typedArray.getDimension(R.styleable.PageIndicatorView_piv_rectWidth,DensityUtils.dpToPx(Indicator.DEFAULT_RECT_WIDTH_DP));
if( rectWidth < 0 ) {
rectWidth = 0;
}

int rectHeight = (int) typedArray.getDimension(R.styleable.PageIndicatorView_piv_rectHeight,DensityUtils.dpToPx(Indicator.DEFAULT_RECT_HEIGHT_DP));
if( rectHeight < 0 ) {
rectHeight = 0;
}

int cornerRadius = (int) typedArray.getDimension(R.styleable.PageIndicatorView_piv_cornerRadius,DensityUtils.dpToPx(Indicator.DEFAULT_CORNER_RADIUS_DP));
if( cornerRadius < 0 ) {
cornerRadius = 0;
}

indicator.setRadius(radius);
indicator.setOrientation(orientation);
indicator.setPadding(padding);
indicator.setScaleFactor(scaleFactor);
indicator.setStroke(stroke);
indicator.setRectWidth(rectWidth);
indicator.setRectHeight(rectHeight);
indicator.setCornerRadius(cornerRadius);
}

private AnimationType getAnimationType(int index) {
Expand All @@ -150,9 +175,20 @@ private AnimationType getAnimationType(int index) {
return AnimationType.DROP;
case 8:
return AnimationType.SWAP;
default:
return AnimationType.NONE;
}
}

return AnimationType.NONE;
private IndicatorShape getIndicatorShape(int index) {
switch (index) {
case 0:
return IndicatorShape.CIRCLE;
case 1:
return IndicatorShape.RECTANGLE;
default:
return IndicatorShape.CIRCLE;
}
}

private RtlMode getRtlMode(int index) {
Expand Down
41 changes: 41 additions & 0 deletions pageindicatorview/src/main/java/com/rd/draw/data/Indicator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.support.annotation.NonNull;
import android.view.View;
import com.rd.animation.type.AnimationType;
import com.rd.draw.IndicatorShape;

public class Indicator {

Expand All @@ -13,6 +14,10 @@ public class Indicator {
public static final int DEFAULT_RADIUS_DP = 6;
public static final int DEFAULT_PADDING_DP = 8;

public static final int DEFAULT_RECT_WIDTH_DP = 20;
public static final int DEFAULT_RECT_HEIGHT_DP = 12;
public static final int DEFAULT_CORNER_RADIUS_DP = 3;

private int height;
private int width;
private int radius;
Expand Down Expand Up @@ -45,6 +50,10 @@ public class Indicator {
private Orientation orientation;
private AnimationType animationType;
private RtlMode rtlMode;
private IndicatorShape shape;
private int rectWidth;
private int rectHeight;
private int cornerRadius;

public int getHeight() {
return height;
Expand Down Expand Up @@ -249,4 +258,36 @@ public int getViewPagerId() {
public void setViewPagerId(int viewPagerId) {
this.viewPagerId = viewPagerId;
}

public void setShape(IndicatorShape shape) {
this.shape = shape;
}

public IndicatorShape getShape() {
return shape;
}

public void setRectWidth(int rectWidth) {
this.rectWidth = rectWidth;
}

public int getRectWidth() {
return rectWidth;
}

public void setRectHeight(int rectHeight) {
this.rectHeight = rectHeight;
}

public int getRectHeight() {
return rectHeight;
}

public void setCornerRadius(int cornerRadius) {
this.cornerRadius = cornerRadius;
}

public int getCornerRadius() {
return cornerRadius;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.rd.draw.drawer.type;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.NonNull;

import com.rd.draw.IndicatorShape;
import com.rd.draw.data.Indicator;

class BaseDrawer {
Expand All @@ -13,4 +17,55 @@ class BaseDrawer {
this.paint = paint;
this.indicator = indicator;
}

private void drawRectWithCenter(Canvas canvas, Paint paint,
int centerX, int centerY, int width, int height) {
canvas.drawRect(centerX-(width/2),
centerY-(height/2),
centerX+(width/2),
centerY+(height/2),
paint);
}

private void drawRectWithCenter(Canvas canvas, Paint paint,
int centerX, int centerY, int width, int height, int cornerRadius) {
canvas.drawRoundRect( new RectF(centerX-(width/2),
centerY-(height/2),
centerX+(width/2),
centerY+(height/2)),
cornerRadius,cornerRadius,paint);
}

void drawIndicator(Canvas canvas, Paint paint, int centerX, int centerY ) {
drawIndicator(canvas,paint,centerX,centerY,
indicator.getRadius(),indicator.getRectWidth(),indicator.getRectHeight());
}

void drawIndicator(Canvas canvas, Paint paint, int centerX, int centerY,
float radius, int rectWidth, int rectHeight ) {
if ( indicator.getShape() == IndicatorShape.CIRCLE ) {
canvas.drawCircle(centerX, centerY, radius, paint);
} else if ( indicator.getShape() == IndicatorShape.RECTANGLE ) {
if ( indicator.getCornerRadius() > 0 ) {
drawRectWithCenter(
canvas,
paint,
centerX,
centerY,
rectWidth,
rectHeight,
indicator.getCornerRadius());
} else {
drawRectWithCenter(
canvas,
paint,
centerX,
centerY,
rectWidth,
rectHeight);
}
}
}


}
Loading