Navigation Menu

Skip to content

Commit

Permalink
VectorDrawable was supported in DrawableIndicator.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhpanvip committed Jan 19, 2020
1 parent cf431c6 commit f379cfc
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 10 deletions.
Expand Up @@ -84,28 +84,29 @@ private void initRadioGroup() {
radioGroupStyle.setVisibility(View.VISIBLE);
radioGroupStyle.setVisibility(View.VISIBLE);
radioGroupStyle.setOnCheckedChangeListener((group, checkedId) -> {
resetBannerViewPager();
switch (checkedId) {
case R.id.rb_indicator_below:
resetBannerViewPager();
setIndicatorBelowOfBanner();
break;
case R.id.rb_dash:
resetBannerViewPager();
setupCustomIndicator();
break;
case R.id.rb_drawable:
resetBannerViewPager();
setDrawableIndicator();
setDrawableIndicator(getDrawableIndicator());
break;
case R.id.rb_vector_drawable:
setDrawableIndicator(getVectorDrawableIndicator());
break;
}
});
radioButton.performClick();
}

private void setDrawableIndicator() {
private void setDrawableIndicator(IIndicator indicator) {
mIndicatorView.setVisibility(View.INVISIBLE);
mViewPager
.setIndicatorView(getDrawableIndicator())
.setIndicatorView(indicator)
.setIndicatorSlideMode(IndicatorSlideMode.NORMAL)
.setIndicatorVisibility(View.VISIBLE)
.setIndicatorGravity(IndicatorGravity.CENTER)
Expand All @@ -120,6 +121,14 @@ private IIndicator getDrawableIndicator() {
.setIndicatorSize(dp10, dp10, dp10, dp10);
}

private IIndicator getVectorDrawableIndicator() {
int dp6 = getResources().getDimensionPixelOffset(R.dimen.dp_6);
return new DrawableIndicator(getContext())
.setIndicatorGap(getResources().getDimensionPixelOffset(R.dimen.dp_2_5))
.setIndicatorDrawable(R.drawable.banner_indicator_nornal, R.drawable.banner_indicator_focus)
.setIndicatorSize(dp6, dp6, getResources().getDimensionPixelOffset(R.dimen.dp_13), dp6);
}

private void setIndicatorBelowOfBanner() {
mIndicatorView.setVisibility(View.VISIBLE);
mViewPager
Expand Down
Expand Up @@ -5,10 +5,14 @@
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;

import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatDrawableManager;
import androidx.core.graphics.drawable.DrawableCompat;

import com.zhpan.bannerview.indicator.BaseIndicatorView;

Expand All @@ -27,6 +31,8 @@ public class DrawableIndicator extends BaseIndicatorView {
//未选中图片的宽高
private int mNormalBitmapWidth, mNormalBitmapHeight;
private IndicatorSize mIndicatorSize;
private boolean normalCanResize = true;
private boolean checkCanResize=true;

public DrawableIndicator(Context context) {
this(context, null);
Expand All @@ -51,7 +57,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (getPageSize() > 1 || mCheckedBitmap == null || mNormalBitmap == null) {
if (getPageSize() > 1 && mCheckedBitmap != null && mNormalBitmap != null) {
for (int i = 1; i < getPageSize() + 1; i++) {
int left;
int top;
Expand Down Expand Up @@ -83,7 +89,7 @@ private void drawIcon(Canvas canvas, int left, int top, Bitmap icon) {
private void initIconSize() {
if (mCheckedBitmap != null) {
if (mIndicatorSize != null) {
if (mCheckedBitmap.isMutable()) {
if (mCheckedBitmap.isMutable()&& checkCanResize) {
mCheckedBitmap.setWidth(mIndicatorSize.checkedWidth);
mCheckedBitmap.setHeight(mIndicatorSize.checkedHeight);
} else {
Expand All @@ -102,7 +108,7 @@ private void initIconSize() {

if (mNormalBitmap != null) {
if (mIndicatorSize != null) {
if (mNormalBitmap.isMutable()) {
if (mNormalBitmap.isMutable()&& normalCanResize) {
mNormalBitmap.setWidth(mIndicatorSize.normalWidth);
mNormalBitmap.setHeight(mIndicatorSize.normalHeight);
} else {
Expand All @@ -123,6 +129,14 @@ private void initIconSize() {
public DrawableIndicator setIndicatorDrawable(@DrawableRes int normalDrawable, @DrawableRes int checkedDrawable) {
mNormalBitmap = mCheckedBitmap = BitmapFactory.decodeResource(getResources(), normalDrawable);
mCheckedBitmap = BitmapFactory.decodeResource(getResources(), checkedDrawable);
if (mNormalBitmap == null) {
mNormalBitmap = getBitmapFromVectorDrawable(getContext(), normalDrawable);
normalCanResize =false;
}
if (mCheckedBitmap == null) {
mCheckedBitmap = getBitmapFromVectorDrawable(getContext(), checkedDrawable);
checkCanResize =false;
}
initIconSize();
postInvalidate();
return this;
Expand All @@ -143,6 +157,19 @@ public DrawableIndicator setIndicatorGap(int padding) {
return this;
}

public static Bitmap getBitmapFromVectorDrawable(Context context, int drawableId) {
Drawable drawable = AppCompatDrawableManager.get().getDrawable(context, drawableId);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
drawable = (DrawableCompat.wrap(drawable)).mutate();
}
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}

static class IndicatorSize {
int normalWidth;
int checkedWidth;
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/res/drawable/banner_indicator_focus.xml
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#fff" />
<stroke
android:width="0.5dp"
android:color="#B5B5B5" />
<size
android:width="15dp"
android:height="6dp" />
<corners android:radius="5dp" />
</shape>
12 changes: 12 additions & 0 deletions app/src/main/res/drawable/banner_indicator_nornal.xml
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size
android:width="6dp"
android:height="6dp" />
<solid android:color="#fff" />
<stroke
android:width="0.5dp"
android:color="#B5B5B5" />
<corners android:radius="2dp" />
</shape>
11 changes: 10 additions & 1 deletion app/src/main/res/layout/fragment_others.xml
Expand Up @@ -64,7 +64,16 @@
android:layout_height="wrap_content"
android:layout_marginEnd="15dp"
android:layout_marginBottom="@dimen/dp_10"
android:text="Drawable Indicator"
android:text="Drawable Indicator(BitmapDrawable)"
android:textSize="@dimen/sp_16" />

<RadioButton
android:id="@+id/rb_vector_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(VectorDrawable)"
android:textSize="@dimen/sp_16" />

</RadioGroup>
Expand Down

0 comments on commit f379cfc

Please sign in to comment.