diff --git a/app/release/app-release.apk b/app/release/app-release.apk index afa9cd5b..ec92cd83 100644 Binary files a/app/release/app-release.apk and b/app/release/app-release.apk differ diff --git a/app/src/main/java/com/example/zhpan/circleviewpager/App.java b/app/src/main/java/com/example/zhpan/circleviewpager/App.java index bc2af51a..98ea267f 100644 --- a/app/src/main/java/com/example/zhpan/circleviewpager/App.java +++ b/app/src/main/java/com/example/zhpan/circleviewpager/App.java @@ -2,6 +2,7 @@ import android.app.Application; +import com.zhpan.bannerview.utils.BannerUtils; import com.zhpan.idea.utils.Utils; /** @@ -16,5 +17,6 @@ public class App extends Application { public void onCreate() { super.onCreate(); Utils.init(getApplicationContext()); + BannerUtils.setDebugMode(true); } } diff --git a/app/src/main/java/com/example/zhpan/circleviewpager/activity/MainActivity.kt b/app/src/main/java/com/example/zhpan/circleviewpager/activity/MainActivity.kt index 47c7b5fa..909fdb9d 100644 --- a/app/src/main/java/com/example/zhpan/circleviewpager/activity/MainActivity.kt +++ b/app/src/main/java/com/example/zhpan/circleviewpager/activity/MainActivity.kt @@ -25,7 +25,7 @@ class MainActivity : AppCompatActivity() { private fun initData() { vp_fragment.adapter = AdapterFragmentPager(this) vp_fragment.offscreenPageLimit = 3 - vp_fragment.isUserInputEnabled=false + vp_fragment.isUserInputEnabled = false vp_fragment.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() { override fun onPageSelected(position: Int) { super.onPageSelected(position) @@ -35,14 +35,13 @@ class MainActivity : AppCompatActivity() { } private fun getCheckedId(position: Int): Int { - var checkedId = R.id.rb_home - when (position) { - 0 -> checkedId = R.id.rb_home - 1 -> checkedId = R.id.rb_find - 2 -> checkedId = R.id.rb_add - 3 -> checkedId = R.id.rb_others + return when (position) { + 0 -> R.id.rb_home + 1 -> R.id.rb_find + 2 -> R.id.rb_add + 3 -> R.id.rb_others + else -> R.id.rb_home } - return checkedId } private fun setListener() { diff --git a/app/src/main/java/com/example/zhpan/circleviewpager/adapter/AdapterFragmentPager.kt b/app/src/main/java/com/example/zhpan/circleviewpager/adapter/AdapterFragmentPager.kt index 4eb23ce8..39082a88 100644 --- a/app/src/main/java/com/example/zhpan/circleviewpager/adapter/AdapterFragmentPager.kt +++ b/app/src/main/java/com/example/zhpan/circleviewpager/adapter/AdapterFragmentPager.kt @@ -1,6 +1,5 @@ package com.example.zhpan.circleviewpager.adapter - import android.util.SparseArray import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentActivity @@ -12,8 +11,6 @@ import com.example.zhpan.circleviewpager.fragment.IndicatorFragment import com.example.zhpan.circleviewpager.fragment.OthersFragment import com.example.zhpan.circleviewpager.fragment.PageFragment -import java.util.ArrayList - /** *
* Created by zhangpan on 2019-12-05. diff --git a/app/src/main/java/com/example/zhpan/circleviewpager/fragment/OthersFragment.java b/app/src/main/java/com/example/zhpan/circleviewpager/fragment/OthersFragment.java index 76ecec27..6fbf4d71 100644 --- a/app/src/main/java/com/example/zhpan/circleviewpager/fragment/OthersFragment.java +++ b/app/src/main/java/com/example/zhpan/circleviewpager/fragment/OthersFragment.java @@ -9,6 +9,7 @@ import com.example.zhpan.circleviewpager.R; import com.example.zhpan.circleviewpager.activity.PhotoViewActivity; +import com.example.zhpan.circleviewpager.view.DrawableIndicator; import com.example.zhpan.circleviewpager.view.FigureIndicatorView; import com.example.zhpan.circleviewpager.viewholder.ImageResourceViewHolder; import com.zhpan.bannerview.BannerViewPager; @@ -92,11 +93,32 @@ private void initRadioGroup() { resetBannerViewPager(); setupCustomIndicator(); break; + case R.id.rb_drawable: + resetBannerViewPager(); + setDrawableIndicator(); + break; } }); radioButton.performClick(); } + private void setDrawableIndicator() { + mIndicatorView.setVisibility(View.INVISIBLE); + mViewPager + .setIndicatorView(getDrawableIndicator()) + .setIndicatorSlideMode(IndicatorSlideMode.NORMAL) + .setIndicatorVisibility(View.VISIBLE) + .setIndicatorGravity(IndicatorGravity.CENTER) + .create(getMDrawableList()); + } + + private IIndicator getDrawableIndicator() { + int dp10 = getResources().getDimensionPixelOffset(R.dimen.dp_10); + return new DrawableIndicator(getContext()) + .setIndicatorGap(getResources().getDimensionPixelOffset(R.dimen.dp_2_5)) + .setIndicatorDrawable(R.drawable.heart_empty, R.drawable.heart_red) + .setIndicatorSize(dp10, dp10, dp10, dp10); + } private void setIndicatorBelowOfBanner() { mIndicatorView.setVisibility(View.VISIBLE); diff --git a/app/src/main/java/com/example/zhpan/circleviewpager/view/DrawableIndicator.java b/app/src/main/java/com/example/zhpan/circleviewpager/view/DrawableIndicator.java new file mode 100644 index 00000000..cfa7635d --- /dev/null +++ b/app/src/main/java/com/example/zhpan/circleviewpager/view/DrawableIndicator.java @@ -0,0 +1,159 @@ +package com.example.zhpan.circleviewpager.view; + +import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.Canvas; +import android.graphics.Matrix; +import android.util.AttributeSet; + +import androidx.annotation.DrawableRes; +import androidx.annotation.Nullable; + +import com.zhpan.bannerview.indicator.BaseIndicatorView; + +/** + * @ author : zhouweibin + * @ time: 2019/12/18 17:04. + * @ desc: 选中与未选中的图片长宽可能不一样 + **/ +public class DrawableIndicator extends BaseIndicatorView { + // 选中与未选中的图片 + private Bitmap mCheckedBitmap, mNormalBitmap; + // 图片之间的间距 + private int mIndicatorPadding; + // 选中图片的宽高 + private int mCheckedBitmapWidth, mCheckedBitmapHeight; + //未选中图片的宽高 + private int mNormalBitmapWidth, mNormalBitmapHeight; + private IndicatorSize mIndicatorSize; + + public DrawableIndicator(Context context) { + this(context, null); + } + + public DrawableIndicator(Context context, @Nullable AttributeSet attrs) { + this(context, attrs, 0); + } + + public DrawableIndicator(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + } + + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + int maxHeight = Math.max(mCheckedBitmapHeight, mNormalBitmapHeight); + int realWidth = mCheckedBitmapWidth + (mNormalBitmapWidth + mIndicatorPadding) * (getPageSize() - 1); + setMeasuredDimension(realWidth, maxHeight); + } + + @Override + protected void onDraw(Canvas canvas) { + super.onDraw(canvas); + if (getPageSize() > 1 || mCheckedBitmap == null || mNormalBitmap == null) { + for (int i = 1; i < getPageSize() + 1; i++) { + int left; + int top; + Bitmap bitmap = mNormalBitmap; + int index = i - 1; + if (index < getCurrentPosition()) { + left = (i - 1) * (mNormalBitmapWidth + mIndicatorPadding); + top = getMeasuredHeight() / 2 - mNormalBitmapHeight / 2; + } else if (index == getCurrentPosition()) { + left = (i - 1) * (mNormalBitmapWidth + mIndicatorPadding); + top = getMeasuredHeight() / 2 - mCheckedBitmapHeight / 2; + bitmap = mCheckedBitmap; + } else { + left = (i - 1) * mIndicatorPadding + (i - 2) * mNormalBitmapWidth + mCheckedBitmapWidth; + top = getMeasuredHeight() / 2 - mNormalBitmapHeight / 2; + } + drawIcon(canvas, left, top, bitmap); + } + } + } + + private void drawIcon(Canvas canvas, int left, int top, Bitmap icon) { + if (icon == null) { + return; + } + canvas.drawBitmap(icon, left, top, null); + } + + private void initIconSize() { + if (mCheckedBitmap != null) { + if (mIndicatorSize != null) { + if (mCheckedBitmap.isMutable()) { + mCheckedBitmap.setWidth(mIndicatorSize.checkedWidth); + mCheckedBitmap.setHeight(mIndicatorSize.checkedHeight); + } else { + int width = mCheckedBitmap.getWidth(); + int height = mCheckedBitmap.getHeight(); + float scaleWidth = ((float) (mIndicatorSize.checkedWidth) / width); + float scaleHeight = ((float) (mIndicatorSize.checkedHeight) / height); + Matrix matrix = new Matrix(); + matrix.postScale(scaleWidth, scaleHeight); + mCheckedBitmap = Bitmap.createBitmap(mCheckedBitmap, 0, 0, width, height, matrix, true); + } + } + mCheckedBitmapWidth = mCheckedBitmap.getWidth(); + mCheckedBitmapHeight = mCheckedBitmap.getHeight(); + } + + if (mNormalBitmap != null) { + if (mIndicatorSize != null) { + if (mNormalBitmap.isMutable()) { + mNormalBitmap.setWidth(mIndicatorSize.normalWidth); + mNormalBitmap.setHeight(mIndicatorSize.normalHeight); + } else { + int width = mNormalBitmap.getWidth(); + int height = mNormalBitmap.getHeight(); + float scaleWidth = ((float) (mIndicatorSize.normalWidth) / mNormalBitmap.getWidth()); + float scaleHeight = ((float) (mIndicatorSize.normalHeight) / mNormalBitmap.getHeight()); + Matrix matrix = new Matrix(); + matrix.postScale(scaleWidth, scaleHeight); + mNormalBitmap = Bitmap.createBitmap(mNormalBitmap, 0, 0, width, height, matrix, true); + } + } + mNormalBitmapWidth = mNormalBitmap.getWidth(); + mNormalBitmapHeight = mNormalBitmap.getHeight(); + } + } + + public DrawableIndicator setIndicatorDrawable(@DrawableRes int normalDrawable, @DrawableRes int checkedDrawable) { + mNormalBitmap = mCheckedBitmap = BitmapFactory.decodeResource(getResources(), normalDrawable); + mCheckedBitmap = BitmapFactory.decodeResource(getResources(), checkedDrawable); + initIconSize(); + postInvalidate(); + return this; + } + + public DrawableIndicator setIndicatorSize(int normalWidth, int normalHeight, int checkedWidth, int checkedHeight) { + this.mIndicatorSize = new IndicatorSize(normalWidth, normalHeight, checkedWidth, checkedHeight); + initIconSize(); + postInvalidate(); + return this; + } + + public DrawableIndicator setIndicatorGap(int padding) { + if (padding >= 0) { + mIndicatorPadding = padding; + postInvalidate(); + } + return this; + } + + static class IndicatorSize { + int normalWidth; + int checkedWidth; + int normalHeight; + int checkedHeight; + + public IndicatorSize(int normalWidth, int normalHeight, int checkedWidth, int checkedHeight) { + this.normalWidth = normalWidth; + this.checkedWidth = checkedWidth; + this.normalHeight = normalHeight; + this.checkedHeight = checkedHeight; + } + } +} diff --git a/app/src/main/res/drawable-xxxhdpi/heart_empty.png b/app/src/main/res/drawable-xxxhdpi/heart_empty.png new file mode 100644 index 00000000..3bd89268 Binary files /dev/null and b/app/src/main/res/drawable-xxxhdpi/heart_empty.png differ diff --git a/app/src/main/res/drawable-xxxhdpi/heart_red.png b/app/src/main/res/drawable-xxxhdpi/heart_red.png new file mode 100644 index 00000000..3de8dffb Binary files /dev/null and b/app/src/main/res/drawable-xxxhdpi/heart_red.png differ diff --git a/app/src/main/res/layout/fragment_others.xml b/app/src/main/res/layout/fragment_others.xml index 85995ab2..49d9bb7f 100644 --- a/app/src/main/res/layout/fragment_others.xml +++ b/app/src/main/res/layout/fragment_others.xml @@ -58,6 +58,14 @@ android:text="Custom Indicator" android:textSize="@dimen/sp_16" /> +diff --git a/bannerview/build.gradle b/bannerview/build.gradle index f161b843..40555989 100644 --- a/bannerview/build.gradle +++ b/bannerview/build.gradle @@ -32,7 +32,7 @@ dependencies { implementation 'androidx.appcompat:appcompat:1.0.2' } -version = "2.6.0" +version = "2.6.4" def siteUrl = 'https://github.com/zhpanvip/BannerViewPager' // 项目的主页 def gitUrl = 'https://github.com/zhpanvip/BannerViewPager.git' // Git仓库的url group = "com.zhpan.library" // Maven Group ID for the artifact,一般填你唯一的包名 diff --git a/bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java b/bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java index 51da7ba5..02c149e3 100644 --- a/bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java +++ b/bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java @@ -132,13 +132,11 @@ public boolean dispatchTouchEvent(MotionEvent ev) { @Override public void onPageSelected(int position) { - // Optimized For Issue #42 int size = mBannerPagerAdapter.getListSize(); - if (size > 0 && isCanLoop() && position == 0) { - position = MAX_VALUE / 2 - ((MAX_VALUE / 2) % size) + 1; - setCurrentItem(0, false); - } currentPosition = BannerUtils.getRealPosition(isCanLoop(), position, size); + if (size > 0 && isCanLoop() && position == 0 || position == MAX_VALUE - 1) { + setCurrentItem(currentPosition, false); + } if (mOnPageChangeListener != null) mOnPageChangeListener.onPageSelected(currentPosition); if (mIndicatorView != null) { @@ -159,14 +157,14 @@ public void onPageScrollStateChanged(int state) { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { int listSize = mBannerPagerAdapter.getListSize(); + int realPosition = BannerUtils.getRealPosition(isCanLoop(), position, listSize); if (listSize > 0) { if (mOnPageChangeListener != null) { - mOnPageChangeListener.onPageScrolled(BannerUtils.getRealPosition(isCanLoop(), position, listSize), - positionOffset, positionOffsetPixels); + mOnPageChangeListener.onPageScrolled(realPosition, positionOffset, positionOffsetPixels); + } + if (mIndicatorView != null) { + mIndicatorView.onPageScrolled(realPosition, positionOffset, positionOffsetPixels); } - if (mIndicatorView != null) - mIndicatorView.onPageScrolled(BannerUtils.getRealPosition(isCanLoop(), position, listSize), - positionOffset, positionOffsetPixels); } } @@ -252,7 +250,6 @@ private void setupViewPager(List list) { if (list.size() > 0 && isCanLoop()) { currentPosition = MAX_VALUE / 2 - ((MAX_VALUE / 2) % list.size()) + 1; } - removeAllViews(); mViewPager.setAdapter(getPagerAdapter(list)); mViewPager.setCurrentItem(currentPosition); mViewPager.removeOnPageChangeListener(this); @@ -261,8 +258,7 @@ private void setupViewPager(List list) { mViewPager.setScrollDuration(bannerOptions.getScrollDuration()); mViewPager.disableTouchScroll(bannerOptions.isDisableTouchScroll()); mViewPager.setFirstLayout(true); - addView(mViewPager); - addView(mIndicatorLayout); + mViewPager.setOffscreenPageLimit(mBannerManager.bannerOptions().getOffScreenPageLimit()); initPageStyle(); startLoop(); } @@ -306,7 +302,8 @@ private void setMultiPageStyle(boolean overlap, float scale) { params.rightMargin = params.leftMargin; mViewPager.setOverlapStyle(overlap); mViewPager.setPageMargin(overlap ? -bannerOptions.getPageMargin() : bannerOptions.getPageMargin()); - mViewPager.setOffscreenPageLimit(2); + int offScreenPageLimit = bannerOptions.getOffScreenPageLimit(); + mViewPager.setOffscreenPageLimit(offScreenPageLimit > 2 ? offScreenPageLimit : 2); setPageTransformer(new ScaleInTransformer(scale)); } @@ -667,10 +664,7 @@ public int getCurrentItem() { */ public void setCurrentItem(int item) { if (isCanLoop() && mBannerPagerAdapter.getListSize() > 1) { - removeAllViews(); mViewPager.setCurrentItem(MAX_VALUE / 2 - ((MAX_VALUE / 2) % mBannerPagerAdapter.getListSize()) + 1 + item); - addView(mViewPager); - addView(mIndicatorLayout); } else { mViewPager.setCurrentItem(item); } @@ -684,10 +678,7 @@ public void setCurrentItem(int item) { */ public void setCurrentItem(int item, boolean smoothScroll) { if (isCanLoop() && mBannerPagerAdapter.getListSize() > 1) { - removeAllViews(); mViewPager.setCurrentItem(MAX_VALUE / 2 - ((MAX_VALUE / 2) % mBannerPagerAdapter.getListSize()) + 1 + item, smoothScroll); - addView(mViewPager); - addView(mIndicatorLayout); } else { mViewPager.setCurrentItem(item, smoothScroll); } @@ -738,6 +729,11 @@ public ViewPager getViewPager() { return mViewPager; } + public BannerViewPager setOffScreenPageLimit(int offScreenPageLimit) { + mBannerManager.bannerOptions().setOffScreenPageLimit(offScreenPageLimit); + return this; + } + public BannerViewPager setIndicatorMargin(int left, int top, int right, int bottom) { mBannerManager.bannerOptions().setIndicatorMargin(left, top, right, bottom); diff --git a/bannerview/src/main/java/com/zhpan/bannerview/adapter/BannerPagerAdapter.java b/bannerview/src/main/java/com/zhpan/bannerview/adapter/BannerPagerAdapter.java index 3936bc21..01e12ac9 100644 --- a/bannerview/src/main/java/com/zhpan/bannerview/adapter/BannerPagerAdapter.java +++ b/bannerview/src/main/java/com/zhpan/bannerview/adapter/BannerPagerAdapter.java @@ -28,9 +28,7 @@ public class BannerPagerAdapter extends PagerAdapter { private PageClickListener mPageClickListener; -// private List mViewList = new ArrayList<>(); - - public static final int MAX_VALUE = 10000; + public static final int MAX_VALUE = 500; public BannerPagerAdapter(List list, HolderCreator holderCreator) { mList = new ArrayList<>(); @@ -90,8 +88,8 @@ private View createView(ViewHolder holder, int position, ViewGroup container) View itemView = LayoutInflater.from(container.getContext()).inflate(holder.getLayoutId(), container, false); if (mList != null && mList.size() > 0) { // holder.createView(itemView, position); - holder.onBind(itemView, mList.get(position), position, mList.size()); setViewListener(itemView, position); + holder.onBind(itemView, mList.get(position), position, mList.size()); } return itemView; } diff --git a/bannerview/src/main/java/com/zhpan/bannerview/indicator/drawer/RoundRectDrawer.java b/bannerview/src/main/java/com/zhpan/bannerview/indicator/drawer/RoundRectDrawer.java index 30d1343a..93eb0889 100644 --- a/bannerview/src/main/java/com/zhpan/bannerview/indicator/drawer/RoundRectDrawer.java +++ b/bannerview/src/main/java/com/zhpan/bannerview/indicator/drawer/RoundRectDrawer.java @@ -90,8 +90,10 @@ private void normalSlide(Canvas canvas, int i) { private void smoothSlide(Canvas canvas, int i) { mPaint.setColor(mIndicatorOptions.getNormalColor()); + float sliderHeight = mIndicatorOptions.getSliderHeight(); float left = i * (maxWidth) + i * +mIndicatorOptions.getIndicatorGap() + (maxWidth - minWidth); - canvas.drawRect(left, 0, left + minWidth, mIndicatorOptions.getSliderHeight(), mPaint); + RectF rectF = new RectF(left, 0, left + minWidth, sliderHeight); + canvas.drawRoundRect(rectF, sliderHeight, sliderHeight, mPaint); drawSliderStyle(canvas); } @@ -99,8 +101,11 @@ private void drawSliderStyle(Canvas canvas) { mPaint.setColor(mIndicatorOptions.getCheckedColor()); int currentPosition = mIndicatorOptions.getCurrentPosition(); float indicatorGap = mIndicatorOptions.getIndicatorGap(); + float sliderHeight = mIndicatorOptions.getSliderHeight(); float left = currentPosition * (maxWidth) + currentPosition * +indicatorGap + (maxWidth + indicatorGap) * mIndicatorOptions.getSlideProgress(); - canvas.drawRect(left, 0, left + maxWidth, mIndicatorOptions.getSliderHeight(), mPaint); + RectF rectF = new RectF(left, 0, left + minWidth, sliderHeight); + canvas.drawRoundRect(rectF, sliderHeight, sliderHeight, mPaint); +// canvas.drawRect(left, 0, left + maxWidth, mIndicatorOptions.getSliderHeight(), mPaint); } diff --git a/bannerview/src/main/java/com/zhpan/bannerview/manager/BannerOptions.java b/bannerview/src/main/java/com/zhpan/bannerview/manager/BannerOptions.java index 4921fe82..1cc7ddc6 100644 --- a/bannerview/src/main/java/com/zhpan/bannerview/manager/BannerOptions.java +++ b/bannerview/src/main/java/com/zhpan/bannerview/manager/BannerOptions.java @@ -19,6 +19,8 @@ public BannerOptions() { public static final int DEFAULT_SCROLL_DURATION = 500; + private int offScreenPageLimit; + private int interval; private int currentPosition; @@ -234,6 +236,14 @@ public void resetIndicatorOptions(){ mIndicatorOptions.setSlideProgress(0); } + public int getOffScreenPageLimit() { + return offScreenPageLimit; + } + + public void setOffScreenPageLimit(int offScreenPageLimit) { + this.offScreenPageLimit = offScreenPageLimit; + } + public static class IndicatorMargin { private int left, right, top, bottom; diff --git a/bannerview/src/main/java/com/zhpan/bannerview/utils/BannerUtils.java b/bannerview/src/main/java/com/zhpan/bannerview/utils/BannerUtils.java index e7ed752e..f843c98f 100644 --- a/bannerview/src/main/java/com/zhpan/bannerview/utils/BannerUtils.java +++ b/bannerview/src/main/java/com/zhpan/bannerview/utils/BannerUtils.java @@ -11,12 +11,14 @@ */ public class BannerUtils { - private static final boolean DEBUG = true; + private static boolean debugMode = false; - public float density; + public static void setDebugMode(boolean isDebug) { + debugMode = isDebug; + } - public BannerUtils() { - this.density = Resources.getSystem().getDisplayMetrics().density; + public static boolean isDebugMode() { + return debugMode; } public static int dp2px(float dpValue) { @@ -24,13 +26,13 @@ public static int dp2px(float dpValue) { } public static void log(String tag, String msg) { - if (DEBUG) { + if (debugMode) { Log.e(tag, msg); } } public static void log(String msg) { - if (DEBUG) { + if (debugMode) { Log.e("BannerView", msg); } }