Browse Source

Add Drawable Indicator demo.

zhpanvip 5 years ago
parent
commit
ae53a36008

+ 22 - 0
app/src/main/java/com/example/zhpan/circleviewpager/fragment/OthersFragment.java

@@ -9,6 +9,7 @@ import android.widget.RadioGroup;
 
 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 @@ public class OthersFragment extends BaseFragment implements View.OnClickListener
                     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);

+ 74 - 27
app/src/main/java/com/example/zhpan/circleviewpager/view/DrawableIndicator.java

@@ -2,9 +2,12 @@ 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;
@@ -16,13 +19,14 @@ import com.zhpan.bannerview.indicator.BaseIndicatorView;
  **/
 public class DrawableIndicator extends BaseIndicatorView {
     // 选中与未选中的图片
-    private Bitmap mCheckedIcon, mNormalIcon;
+    private Bitmap mCheckedBitmap, mNormalBitmap;
     // 图片之间的间距
     private int mIndicatorPadding;
     // 选中图片的宽高
-    private int mFocusIconWidth, mFocusIconHeight;
+    private int mCheckedBitmapWidth, mCheckedBitmapHeight;
     //未选中图片的宽高
-    private int mNormalIconWidth, mNormalIconHeight;
+    private int mNormalBitmapWidth, mNormalBitmapHeight;
+    private IndicatorSize mIndicatorSize;
 
     public DrawableIndicator(Context context) {
         this(context, null);
@@ -39,30 +43,30 @@ public class DrawableIndicator extends BaseIndicatorView {
     @Override
     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
-        int maxHeight = Math.max(mFocusIconHeight, mNormalIconHeight);
-        int realWidth = mFocusIconWidth + (mNormalIconWidth + mIndicatorPadding) * (getPageSize() - 1);
+        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 || mCheckedIcon == null || mNormalIcon == null) {
+        if (getPageSize() > 1 || mCheckedBitmap == null || mNormalBitmap == null) {
             for (int i = 1; i < getPageSize() + 1; i++) {
-                int left = 0;
-                int top = 0;
-                Bitmap bitmap = mNormalIcon;
+                int left;
+                int top;
+                Bitmap bitmap = mNormalBitmap;
                 int index = i - 1;
                 if (index < getCurrentPosition()) {
-                    left = (i - 1) * (mNormalIconWidth + mIndicatorPadding);
-                    top = getMeasuredHeight() / 2 - mNormalIconHeight / 2;
+                    left = (i - 1) * (mNormalBitmapWidth + mIndicatorPadding);
+                    top = getMeasuredHeight() / 2 - mNormalBitmapHeight / 2;
                 } else if (index == getCurrentPosition()) {
-                    left = (i - 1) * (mNormalIconWidth + mIndicatorPadding);
-                    top = getMeasuredHeight() / 2 - mFocusIconHeight / 2;
-                    bitmap = mCheckedIcon;
+                    left = (i - 1) * (mNormalBitmapWidth + mIndicatorPadding);
+                    top = getMeasuredHeight() / 2 - mCheckedBitmapHeight / 2;
+                    bitmap = mCheckedBitmap;
                 } else {
-                    left = (i - 1) * mIndicatorPadding + (i - 2) * mNormalIconWidth + mFocusIconWidth;
-                    top = getMeasuredHeight() / 2 - mNormalIconHeight / 2;
+                    left = (i - 1) * mIndicatorPadding + (i - 2) * mNormalBitmapWidth + mCheckedBitmapWidth;
+                    top = getMeasuredHeight() / 2 - mNormalBitmapHeight / 2;
                 }
                 drawIcon(canvas, left, top, bitmap);
             }
@@ -77,36 +81,79 @@ public class DrawableIndicator extends BaseIndicatorView {
     }
 
     private void initIconSize() {
-        if (mCheckedIcon != null) {
-            mFocusIconWidth = mCheckedIcon.getWidth();
-            mFocusIconHeight = mCheckedIcon.getHeight();
+        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 (mNormalIcon != null) {
-            mNormalIconWidth = mNormalIcon.getWidth();
-            mNormalIconHeight = mNormalIcon.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 setFocusIcon(Bitmap icon) {
-        mCheckedIcon = icon;
+    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 setNormalIcon(Bitmap icon) {
-        mNormalIcon = icon;
+    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 setIndicatorPadding(int padding) {
+    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;
+        }
+    }
 }

BIN
app/src/main/res/drawable-xxxhdpi/heart_empty.png


BIN
app/src/main/res/drawable-xxxhdpi/heart_red.png


+ 8 - 0
app/src/main/res/layout/fragment_others.xml

@@ -58,6 +58,14 @@
             android:text="Custom Indicator"
             android:textSize="@dimen/sp_16" />
 
+        <RadioButton
+            android:id="@+id/rb_drawable"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_marginEnd="15dp"
+            android:layout_marginBottom="@dimen/dp_10"
+            android:text="Drawable Indicator"
+            android:textSize="@dimen/sp_16" />
 
     </RadioGroup>
 

+ 0 - 1
bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java

@@ -158,7 +158,6 @@ public class BannerViewPager<T, VH extends ViewHolder> extends RelativeLayout im
     public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
         int listSize = mBannerPagerAdapter.getListSize();
         int realPosition = BannerUtils.getRealPosition(isCanLoop(), position, listSize);
-        BannerUtils.log("position:" + position + " realPosition:" + realPosition);
         if (listSize > 0) {
             if (mOnPageChangeListener != null) {
                 mOnPageChangeListener.onPageScrolled(realPosition, positionOffset, positionOffsetPixels);

+ 1 - 1
bannerview/src/main/java/com/zhpan/bannerview/adapter/BannerPagerAdapter.java

@@ -88,8 +88,8 @@ public class BannerPagerAdapter<T, VH extends ViewHolder> extends PagerAdapter {
         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;
     }