Skip to content

Commit

Permalink
Update 1.2.4.
Browse files Browse the repository at this point in the history
Icon size fraction feature.
Fixed random crash of bitmap.
  • Loading branch information
GIGAMOLE committed Jul 9, 2016
1 parent 026c72f commit bf18098
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 32 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ For NTB you can set such parameters as:

allows you to set corners radius of pointer.

- icon size fraction:

allows you to set icon size fraction relative to smaller model side.

- animation duration:

allows you to set animation duration.
Expand Down Expand Up @@ -166,6 +170,8 @@ By default badge bg color is the active model color and badge title color is the

By default badge sizes and title sizes is auto fit. To reset calculation just set AUTO_SIZE value to badge size and title size.

By default icon size fraction is 0.5 (half of smaller side of NTB model). To reset scale fraction of icon to automatic just put in method AUTO_SCALE value.

If your set ViewPager and enable swipe you can action down on active pointer and do like drag.

<b>Init</b>
Expand Down Expand Up @@ -232,6 +238,7 @@ navigationTabBar.setIsSwiped(true);
navigationTabBar.setBgColor(Color.BLACK);
navigationTabBar.setBadgeSize(10);
navigationTabBar.setTitleSize(10);
navigationTabBar.setIconSizeFraction(0.5);
```

If your models is in badge mode you can set title, hide, show, toggle and update badge title like this:
Expand Down Expand Up @@ -280,6 +287,7 @@ And XML init:
app:ntb_badge_use_typeface="true"
app:ntb_swiped="true"
app:ntb_bg_color="#000"
app:ntb_icon_size_fraction="0.5"
app:ntb_badge_size="10sp"
app:ntb_title_size="10sp"/>
```
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
dependencies {
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.1"
classpath 'com.android.tools.build:gradle:2.1.0'
classpath 'com.android.tools.build:gradle:2.1.2'
}
}

Expand Down
4 changes: 2 additions & 2 deletions navigationtabbar/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ apply plugin: "com.jfrog.bintray"
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'maven'

version = "1.2.3"
version = "1.2.4"

android {
compileSdkVersion 23
Expand All @@ -29,7 +29,7 @@ android {
minSdkVersion 11
targetSdkVersion 23
versionCode 1
versionName "1.2.3"
versionName "1.2.4"
}
buildTypes {
release {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,13 @@ public class NavigationTabBar extends View implements ViewPager.OnPageChangeList
private final static int INVALID_INDEX = -1;
public final static int AUTO_SIZE = -2;
public final static int AUTO_COLOR = -3;
public final static int AUTO_SCALE = -4;

private final static int DEFAULT_BADGE_ANIMATION_DURATION = 200;
private final static int DEFAULT_BADGE_REFRESH_ANIMATION_DURATION = 100;
private final static int DEFAULT_ANIMATION_DURATION = 300;
private final static float DEFAULT_ICON_SIZE_FRACTION = 0.5F;
private final static float DEFAULT_TITLE_ICON_SIZE_FRACTION = 0.5F;

private final static int DEFAULT_INACTIVE_COLOR = Color.parseColor("#9f90af");
private final static int DEFAULT_ACTIVE_COLOR = Color.WHITE;
Expand All @@ -97,10 +100,7 @@ public class NavigationTabBar extends View implements ViewPager.OnPageChangeList
private final static int MAX_ALPHA = 255;

private final static float ACTIVE_ICON_SCALE_BY = 0.3F;
private final static float ICON_SIZE_FRACTION = 0.45F;

private final static float TITLE_ACTIVE_ICON_SCALE_BY = 0.2F;
private final static float TITLE_ICON_SIZE_FRACTION = 0.45F;
private final static float TITLE_ACTIVE_SCALE_BY = 0.2F;
private final static float TITLE_SIZE_FRACTION = 0.2F;
private final static float TITLE_MARGIN_FRACTION = 0.15F;
Expand Down Expand Up @@ -221,6 +221,7 @@ public class NavigationTabBar extends View implements ViewPager.OnPageChangeList
// Variables for sizes
private float mModelSize;
private float mIconSize;
private float mIconSizeFraction;
// Corners radius for rect mode
private float mCornersRadius;

Expand Down Expand Up @@ -372,6 +373,12 @@ public NavigationTabBar(final Context context, final AttributeSet attrs, final i
setCornersRadius(
typedArray.getDimension(R.styleable.NavigationTabBar_ntb_corners_radius, 0.0F)
);
setIconSizeFraction(
typedArray.getFloat(
R.styleable.NavigationTabBar_ntb_icon_size_fraction,
AUTO_SCALE
)
);

// Init animator
mAnimator.setFloatValues(MIN_FRACTION, MAX_FRACTION);
Expand Down Expand Up @@ -672,6 +679,16 @@ public void setCornersRadius(final float cornersRadius) {
postInvalidate();
}

public float getIconSizeFraction() {
return mIconSizeFraction;
}

// To reset scale fraction of icon to automatic just put in method AUTO_SCALE value
public void setIconSizeFraction(final float iconSizeFraction) {
mIconSizeFraction = iconSizeFraction;
requestLayout();
}

public float getBadgeMargin() {
return mBadgeMargin;
}
Expand Down Expand Up @@ -943,7 +960,8 @@ protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec
float side = mModelSize > height ? height : mModelSize;
if (mIsBadged) side -= side * TITLE_SIZE_FRACTION;

mIconSize = side * (mIsTitled ? TITLE_ICON_SIZE_FRACTION : ICON_SIZE_FRACTION);
mIconSize = side * (mIconSizeFraction != AUTO_SCALE ? mIconSizeFraction :
(mIsTitled ? DEFAULT_TITLE_ICON_SIZE_FRACTION : DEFAULT_ICON_SIZE_FRACTION));
if (mModelTitleSize == AUTO_SIZE) mModelTitleSize = side * TITLE_SIZE_FRACTION;
mTitleMargin = side * TITLE_MARGIN_FRACTION;

Expand All @@ -967,7 +985,8 @@ protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec
mIsBadged = false;

mModelSize = (float) height / (float) mModels.size();
mIconSize = (int) ((mModelSize > width ? width : mModelSize) * ICON_SIZE_FRACTION);
mIconSize = (int) ((mModelSize > width ? width : mModelSize) *
(mIconSizeFraction == AUTO_SCALE ? DEFAULT_ICON_SIZE_FRACTION : mIconSizeFraction));
}

// Set bounds for NTB
Expand All @@ -976,24 +995,6 @@ protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec
final float barBadgeMargin = mBadgeGravity == BadgeGravity.TOP ? mBadgeMargin : 0.0F;
mBgBounds.set(0.0F, barBadgeMargin, mBounds.width(), mBounds.height() + barBadgeMargin);

// Set main bitmap
mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
mCanvas.setBitmap(mBitmap);

// Set pointer canvas
mPointerBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
mPointerCanvas.setBitmap(mPointerBitmap);

// Set icons canvas
mIconsBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
mIconsCanvas.setBitmap(mIconsBitmap);

// Set titles canvas
if (mIsTitled) {
mTitlesBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
mTitlesCanvas.setBitmap(mTitlesBitmap);
} else mTitlesBitmap = null;

// Set scale fraction for icons
for (Model model : mModels) {
final float originalIconSize = model.mIcon.getWidth() > model.mIcon.getHeight() ?
Expand All @@ -1003,6 +1004,12 @@ protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec
(mIsTitled ? TITLE_ACTIVE_ICON_SCALE_BY : ACTIVE_ICON_SCALE_BY);
}

// Reset bitmap to init it onDraw()
mBitmap = null;
mPointerBitmap = null;
mIconsBitmap = null;
if (mIsTitled) mTitlesBitmap = null;

// Set start position of pointer for preview or on start
if (isInEditMode() || !mIsViewPagerMode) {
mIsSetIndexFromTabBar = true;
Expand Down Expand Up @@ -1040,9 +1047,39 @@ protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec
@SuppressWarnings("ConstantConditions")
@Override
protected void onDraw(final Canvas canvas) {
if (mCanvas == null || mPointerCanvas == null ||
mIconsCanvas == null || mTitlesCanvas == null)
return;
// Get height of NTB with badge on nor
final int mBadgedHeight = (int) (mBounds.height() + mBadgeMargin);

// Set main canvas
if (mBitmap == null || mBitmap.isRecycled()) {
mBitmap = Bitmap.createBitmap(
(int) mBounds.width(), mBadgedHeight, Bitmap.Config.ARGB_8888
);
mCanvas.setBitmap(mBitmap);
}
// Set pointer canvas
if (mPointerBitmap == null || mPointerBitmap.isRecycled()) {
mPointerBitmap = Bitmap.createBitmap(
(int) mBounds.width(), mBadgedHeight, Bitmap.Config.ARGB_8888
);
mPointerCanvas.setBitmap(mPointerBitmap);
}
// Set icons canvas
if (mIconsBitmap == null || mIconsBitmap.isRecycled()) {
mIconsBitmap = Bitmap.createBitmap(
(int) mBounds.width(), mBadgedHeight, Bitmap.Config.ARGB_8888
);
mIconsCanvas.setBitmap(mIconsBitmap);
}
// Set titles canvas
if (mIsTitled) {
if (mTitlesBitmap == null || mTitlesBitmap.isRecycled()) {
mTitlesBitmap = Bitmap.createBitmap(
(int) mBounds.width(), mBadgedHeight, Bitmap.Config.ARGB_8888
);
mTitlesCanvas.setBitmap(mTitlesBitmap);
}
} else mTitlesBitmap = null;

// Reset and clear canvases
mCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
Expand Down Expand Up @@ -1188,14 +1225,17 @@ else if (i == mIndex)

// Draw original model icon
if (model.mSelectedIcon == null) {
mIconsCanvas.drawBitmap(model.mIcon, model.mIconMatrix, mIconPaint);
if (model.mIcon != null && !model.mIcon.isRecycled())
mIconsCanvas.drawBitmap(model.mIcon, model.mIconMatrix, mIconPaint);
} else {
if (mIconPaint.getAlpha() != MIN_ALPHA)
if (mIconPaint.getAlpha() != MIN_ALPHA
&& model.mIcon != null && !model.mIcon.isRecycled())
// Draw original icon when is visible
mIconsCanvas.drawBitmap(model.mIcon, model.mIconMatrix, mIconPaint);
}
// Draw selected icon when exist and visible
if (model.mSelectedIcon != null && mSelectedIconPaint.getAlpha() != MIN_ALPHA)
if (mSelectedIconPaint.getAlpha() != MIN_ALPHA
&& model.mSelectedIcon != null && !model.mSelectedIcon.isRecycled())
mIconsCanvas.drawBitmap(
model.mSelectedIcon, model.mIconMatrix, mSelectedIconPaint
);
Expand Down
1 change: 1 addition & 0 deletions navigationtabbar/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

<attr name="ntb_typeface" format="string"/>
<attr name="ntb_corners_radius" format="dimension"/>
<attr name="ntb_icon_size_fraction" format="float"/>
<attr name="ntb_animation_duration" format="integer"/>

<attr name="ntb_inactive_color" format="color"/>
Expand Down

0 comments on commit bf18098

Please sign in to comment.