Skip to content

Commit

Permalink
Merge pull request #76 from zhpanvip/dev_2.6.4
Browse files Browse the repository at this point in the history
v2.6.4
  • Loading branch information
zhpanvip committed Dec 28, 2019
2 parents d6f0e9d + 86ba63e commit aeba256
Show file tree
Hide file tree
Showing 15 changed files with 242 additions and 44 deletions.
Binary file modified app/release/app-release.apk
Binary file not shown.
2 changes: 2 additions & 0 deletions app/src/main/java/com/example/zhpan/circleviewpager/App.java
Expand Up @@ -2,6 +2,7 @@

import android.app.Application;

import com.zhpan.bannerview.utils.BannerUtils;
import com.zhpan.idea.utils.Utils;

/**
Expand All @@ -16,5 +17,6 @@ public class App extends Application {
public void onCreate() {
super.onCreate();
Utils.init(getApplicationContext());
BannerUtils.setDebugMode(true);
}
}
Expand Up @@ -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)
Expand All @@ -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() {
Expand Down
@@ -1,6 +1,5 @@
package com.example.zhpan.circleviewpager.adapter


import android.util.SparseArray
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
Expand All @@ -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

/**
* <pre>
* Created by zhangpan on 2019-12-05.
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
@@ -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;
}
}
}
Binary file added app/src/main/res/drawable-xxxhdpi/heart_empty.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-xxxhdpi/heart_red.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions app/src/main/res/layout/fragment_others.xml
Expand Up @@ -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>

Expand Down
2 changes: 1 addition & 1 deletion bannerview/build.gradle
Expand Up @@ -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,一般填你唯一的包名
Expand Down
36 changes: 16 additions & 20 deletions bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java
Expand Up @@ -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) {
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -252,7 +250,6 @@ private void setupViewPager(List<T> 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);
Expand All @@ -261,8 +258,7 @@ private void setupViewPager(List<T> list) {
mViewPager.setScrollDuration(bannerOptions.getScrollDuration());
mViewPager.disableTouchScroll(bannerOptions.isDisableTouchScroll());
mViewPager.setFirstLayout(true);
addView(mViewPager);
addView(mIndicatorLayout);
mViewPager.setOffscreenPageLimit(mBannerManager.bannerOptions().getOffScreenPageLimit());
initPageStyle();
startLoop();
}
Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -738,6 +729,11 @@ public ViewPager getViewPager() {
return mViewPager;
}

public BannerViewPager<T, VH> setOffScreenPageLimit(int offScreenPageLimit) {
mBannerManager.bannerOptions().setOffScreenPageLimit(offScreenPageLimit);
return this;
}


public BannerViewPager<T, VH> setIndicatorMargin(int left, int top, int right, int bottom) {
mBannerManager.bannerOptions().setIndicatorMargin(left, top, right, bottom);
Expand Down

0 comments on commit aeba256

Please sign in to comment.