Skip to content

Commit

Permalink
增加一屏多页(大于三页)的功能
Browse files Browse the repository at this point in the history
  • Loading branch information
gyf-dev committed May 28, 2021
1 parent 39ff0ca commit 5e9a29b
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 34 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 30
Expand Down Expand Up @@ -30,6 +31,8 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "androidx.core:core-ktx:+"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.recyclerview:recyclerview:1.2.0'
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
Expand Down
69 changes: 69 additions & 0 deletions app/src/main/java/com/test/banner/FivePagesOneScreenTransformer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.test.banner

import android.view.View
import androidx.viewpager2.widget.ViewPager2

/**
* 一拼五页
*
* @property scale Float
* @property translationX Float
* @constructor
*/
class FivePagesOneScreenTransformer(
private val scale: Float = DEFAULT_MIN_SCALE,
private val translationX: Float = -120f) : ViewPager2.PageTransformer {

companion object {
/**
* 默认的中心点
*/
private const val DEFAULT_CENTER = 0.5f

/**
* 默认的缩放比例
*/
private const val DEFAULT_MIN_SCALE = 0.85f
}

override fun transformPage(page: View, position: Float) {
val pageWidth = page.width
val pageHeight = page.height
page.pivotX = (pageWidth / 2).toFloat()
page.pivotY = (pageHeight / 2).toFloat()
when {
position < -2 -> {
// This page is way off-screen to the left.
page.scaleX = 0f
page.scaleY = 0f
page.pivotX = pageWidth.toFloat()
}
position < 0 -> {
//缩放
((1 + position) * (1 - scale) + scale).also {
page.scaleX = it
page.scaleY = it
}
page.pivotX = pageWidth * (DEFAULT_CENTER + DEFAULT_CENTER * -position)
//层级
page.translationZ = position
}
position < 3 -> {
//缩放
((1 - position) * (1 - scale) + scale).also {
page.scaleX = it
page.scaleY = it
}
page.pivotX = pageWidth * ((1 - position) * DEFAULT_CENTER)
//层级
page.translationZ = -position
}
else -> {
page.scaleX = 0f
page.scaleY = 0f
page.pivotX = pageWidth.toFloat()
}
}
page.translationX = translationX * position
}
}
23 changes: 16 additions & 7 deletions app/src/main/java/com/test/banner/ui/GalleryActivity.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package com.test.banner.ui;

import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.test.banner.R;
import com.test.banner.FivePagesOneScreenTransformer;
import com.test.banner.adapter.ImageAdapter;
import com.test.banner.adapter.ImageNetAdapter;
import com.test.banner.bean.DataBean;
import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.indicator.DrawableIndicator;
import com.youth.banner.transformer.AlphaPageTransformer;

import androidx.appcompat.app.AppCompatActivity;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class GalleryActivity extends AppCompatActivity {

Expand All @@ -25,6 +23,8 @@ public class GalleryActivity extends AppCompatActivity {
Banner mBanner2;
@BindView(R.id.indicator)
DrawableIndicator indicator;
@BindView(R.id.banner3)
Banner mBanner3;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -48,11 +48,20 @@ protected void onCreate(Bundle savedInstanceState) {
* 魅族效果
*/
mBanner2.setAdapter(new ImageAdapter(DataBean.getTestData()));
mBanner2.setIndicator(indicator,false);
mBanner2.setIndicator(indicator, false);
//添加魅族效果
mBanner2.setBannerGalleryMZ(20);


/**
* 一屏五页
*/
mBanner3.setAdapter(new ImageAdapter(DataBean.getTestData()));
mBanner3.setIndicator(new CircleIndicator(this));
mBanner3.getViewPager2().setOffscreenPageLimit(4);
mBanner3.setIncreaseCount(8);
mBanner3.setPageTransformer(new FivePagesOneScreenTransformer());
mBanner3.setRecyclerViewPadding(350);
mBanner3.setCurrentItem(3);

}

Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/layout/activity_gallery.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,13 @@
android:layout_gravity="center_horizontal"
banner:normal_drawable="@drawable/indicator_normal"
banner:selected_drawable="@drawable/indicator_selected"/>

<com.youth.banner.Banner
android:id="@+id/banner3"
android:layout_width="match_parent"
android:layout_height="140dp"
android:layout_margin="10dp"
banner:banner_radius="5dp"
banner:banner_indicator_normal_color="@android:color/white"
banner:banner_indicator_selected_color="@color/colorPrimary" />
</LinearLayout>
47 changes: 35 additions & 12 deletions banner/src/main/java/com/youth/banner/Banner.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
Expand Down Expand Up @@ -38,10 +37,9 @@
import com.youth.banner.listener.OnPageChangeListener;
import com.youth.banner.transformer.MZScaleInTransformer;
import com.youth.banner.transformer.ScaleInTransformer;
import com.youth.banner.util.BannerLifecycleObserver;
import com.youth.banner.util.BannerLifecycleObserverAdapter;
import com.youth.banner.util.BannerUtils;
import com.youth.banner.util.BannerLifecycleObserver;
import com.youth.banner.util.LogUtils;
import com.youth.banner.util.ScrollSpeedManger;

import java.lang.annotation.Retention;
Expand Down Expand Up @@ -90,6 +88,7 @@ public class Banner<T, BA extends BannerAdapter<T, ? extends RecyclerView.ViewHo
private int indicatorMarginBottom;
private int indicatorHeight = BannerConfig.INDICATOR_HEIGHT;
private int indicatorRadius = BannerConfig.INDICATOR_RADIUS;
private int increaseCount = BannerConfig.INCREASE_COUNT;

public static final int HORIZONTAL = 0;
public static final int VERTICAL = 1;
Expand All @@ -109,7 +108,7 @@ public class Banner<T, BA extends BannerAdapter<T, ? extends RecyclerView.ViewHo
private Paint mImagePaint;

@Retention(SOURCE)
@IntDef( {HORIZONTAL, VERTICAL})
@IntDef({HORIZONTAL, VERTICAL})
public @interface Orientation {
}

Expand Down Expand Up @@ -391,10 +390,11 @@ public void onPageScrollStateChanged(int state) {
//滑动闲置或滑动结束
isScrolled = false;
if (mTempPosition != INVALID_VALUE && mIsInfiniteLoop) {
if (mTempPosition == 0) {
setCurrentItem(getRealCount(), false);
} else if (mTempPosition == getItemCount() - 1) {
setCurrentItem(1, false);
int increaseCountHalf = increaseCount / 2;
if (mTempPosition <= increaseCountHalf - 1) {
setCurrentItem(getRealCount() + mTempPosition, false);
} else if (mTempPosition >= getRealCount() + increaseCountHalf) {
setCurrentItem(increaseCountHalf, false);
}
}
}
Expand Down Expand Up @@ -462,7 +462,7 @@ private void setInfiniteLoop() {
setStartPosition(isInfiniteLoop() ? mStartPosition : 0);
}

private void setRecyclerViewPadding(int itemPadding) {
public void setRecyclerViewPadding(int itemPadding) {
setRecyclerViewPadding(itemPadding, itemPadding);
}

Expand Down Expand Up @@ -537,6 +537,7 @@ public int getRealCount() {

/**
* 是否要拦截事件
*
* @param intercept
* @return
*/
Expand All @@ -547,6 +548,7 @@ public Banner setIntercept(boolean intercept) {

/**
* 跳转到指定位置(最好在设置了数据后在调用,不然没有意义)
*
* @param position
* @return
*/
Expand All @@ -556,6 +558,7 @@ public Banner setCurrentItem(int position) {

/**
* 跳转到指定位置(最好在设置了数据后在调用,不然没有意义)
*
* @param position
* @param smoothScroll
* @return
Expand Down Expand Up @@ -603,6 +606,25 @@ public Banner setUserInputEnabled(boolean enabled) {
return this;
}

public int getIncreaseCount() {
return increaseCount;
}

/**
* 设置增加的数量,适合一屏多页无限轮播
*
* @param increaseCount
*/
public Banner setIncreaseCount(int increaseCount) {
if (increaseCount % 2 == 0) {
this.increaseCount = increaseCount;
if (getAdapter() != null) {
getAdapter().setIncreaseCount(increaseCount);
}
}
return this;
}

/**
* 添加PageTransformer,可以组合效果
* {@link ViewPager2.PageTransformer}
Expand Down Expand Up @@ -720,12 +742,13 @@ public Banner setAdapter(BA adapter) {

/**
* 设置banner的适配器
*
* @param adapter
* @param isInfiniteLoop 是否支持无限循环
* @return
*/
public Banner setAdapter(BA adapter,boolean isInfiniteLoop) {
mIsInfiniteLoop=isInfiniteLoop;
public Banner setAdapter(BA adapter, boolean isInfiniteLoop) {
mIsInfiniteLoop = isInfiniteLoop;
setInfiniteLoop();
setAdapter(adapter);
return this;
Expand Down Expand Up @@ -825,7 +848,7 @@ public Banner setBannerGalleryEffect(int itemWidth, int pageMargin) {
* @param pageMargin 页面间距,单位dp
*/
public Banner setBannerGalleryEffect(int leftItemWidth, int rightItemWidth, int pageMargin) {
return setBannerGalleryEffect(leftItemWidth,rightItemWidth, pageMargin, .85f);
return setBannerGalleryEffect(leftItemWidth, rightItemWidth, pageMargin, .85f);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


public abstract class BannerAdapter<T, VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> implements IViewHolder<T, VH> {
protected List<T> mDatas = new ArrayList<>();
Expand Down Expand Up @@ -100,7 +97,7 @@ public int getRealCount() {
}

public int getRealPosition(int position) {
return BannerUtils.getRealPosition(mIncreaseCount == BannerConfig.INCREASE_COUNT, position, getRealCount());
return BannerUtils.getRealPosition(mIncreaseCount >= 2, position, getRealCount());
}

public void setOnBannerListener(OnBannerListener<T> listener) {
Expand Down
16 changes: 6 additions & 10 deletions banner/src/main/java/com/youth/banner/util/BannerUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,17 @@ public class BannerUtils {
* 获取真正的位置
*
* @param isIncrease 首尾是否有增加
* @param position 当前位置
* @param realCount 真实数量
* @param position 当前位置
* @param realCount 真实数量
* @return
*/
public static int getRealPosition(boolean isIncrease, int position, int realCount) {
if (!isIncrease) {
if (!isIncrease || realCount == 0) {
return position;
}
int realPosition;
if (position == 0) {
int realPosition = position % realCount - 1;
if (realPosition < 0) {
realPosition = realCount - 1;
} else if (position == realCount + 1) {
realPosition = 0;
} else {
realPosition = position - 1;
}
return realPosition;
}
Expand Down Expand Up @@ -68,7 +64,7 @@ public static int dp2px(float dp) {
* @return
*/
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public static void setBannerRound(View view,float radius) {
public static void setBannerRound(View view, float radius) {
view.setOutlineProvider(new ViewOutlineProvider() {
@Override
public void getOutline(View view, Outline outline) {
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ allprojects {
maven { url "https://s01.oss.sonatype.org/content/groups/public" }
jcenter()
google()

mavenCentral()
}
}

Expand Down

0 comments on commit 5e9a29b

Please sign in to comment.