From bf180980b97a45175fc0a21caff014d1438ffb6f Mon Sep 17 00:00:00 2001 From: GIGAMOLE Date: Sat, 9 Jul 2016 23:13:51 +0300 Subject: [PATCH] Update 1.2.4. Icon size fraction feature. Fixed random crash of bitmap. --- README.md | 8 ++ build.gradle | 2 +- navigationtabbar/build.gradle | 4 +- .../ntb/NavigationTabBar.java | 98 +++++++++++++------ .../src/main/res/values/attrs.xml | 1 + 5 files changed, 81 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 75a887f..119b2d4 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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. Init @@ -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: @@ -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"/> ``` diff --git a/build.gradle b/build.gradle index cd70ee5..c618080 100644 --- a/build.gradle +++ b/build.gradle @@ -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' } } diff --git a/navigationtabbar/build.gradle b/navigationtabbar/build.gradle index c3a2660..d28d0f2 100644 --- a/navigationtabbar/build.gradle +++ b/navigationtabbar/build.gradle @@ -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 @@ -29,7 +29,7 @@ android { minSdkVersion 11 targetSdkVersion 23 versionCode 1 - versionName "1.2.3" + versionName "1.2.4" } buildTypes { release { diff --git a/navigationtabbar/src/main/java/com/gigamole/navigationtabbar/ntb/NavigationTabBar.java b/navigationtabbar/src/main/java/com/gigamole/navigationtabbar/ntb/NavigationTabBar.java index e4d2307..42ced03 100644 --- a/navigationtabbar/src/main/java/com/gigamole/navigationtabbar/ntb/NavigationTabBar.java +++ b/navigationtabbar/src/main/java/com/gigamole/navigationtabbar/ntb/NavigationTabBar.java @@ -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; @@ -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; @@ -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; @@ -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); @@ -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; } @@ -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; @@ -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 @@ -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() ? @@ -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; @@ -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); @@ -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 ); diff --git a/navigationtabbar/src/main/res/values/attrs.xml b/navigationtabbar/src/main/res/values/attrs.xml index f59d52f..0b39aa9 100644 --- a/navigationtabbar/src/main/res/values/attrs.xml +++ b/navigationtabbar/src/main/res/values/attrs.xml @@ -30,6 +30,7 @@ +