diff --git a/app/build.gradle b/app/build.gradle index 4fed9a8..3dcaf76 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,4 +1,5 @@ apply plugin: 'com.android.application' +apply plugin: 'kotlin-android' android { compileSdkVersion 30 @@ -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' diff --git a/app/src/main/java/com/test/banner/FivePagesOneScreenTransformer.kt b/app/src/main/java/com/test/banner/FivePagesOneScreenTransformer.kt new file mode 100644 index 0000000..dfc14b6 --- /dev/null +++ b/app/src/main/java/com/test/banner/FivePagesOneScreenTransformer.kt @@ -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 + } +} \ No newline at end of file diff --git a/app/src/main/java/com/test/banner/ui/GalleryActivity.java b/app/src/main/java/com/test/banner/ui/GalleryActivity.java index dfda729..7ddcf67 100644 --- a/app/src/main/java/com/test/banner/ui/GalleryActivity.java +++ b/app/src/main/java/com/test/banner/ui/GalleryActivity.java @@ -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 { @@ -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) { @@ -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); } diff --git a/app/src/main/res/layout/activity_gallery.xml b/app/src/main/res/layout/activity_gallery.xml index a5356a7..132383b 100644 --- a/app/src/main/res/layout/activity_gallery.xml +++ b/app/src/main/res/layout/activity_gallery.xml @@ -30,4 +30,13 @@ android:layout_gravity="center_horizontal" banner:normal_drawable="@drawable/indicator_normal" banner:selected_drawable="@drawable/indicator_selected"/> + + \ No newline at end of file diff --git a/banner/src/main/java/com/youth/banner/Banner.java b/banner/src/main/java/com/youth/banner/Banner.java index 8259351..db86b71 100644 --- a/banner/src/main/java/com/youth/banner/Banner.java +++ b/banner/src/main/java/com/youth/banner/Banner.java @@ -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; @@ -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; @@ -90,6 +88,7 @@ public class Banner= getRealCount() + increaseCountHalf) { + setCurrentItem(increaseCountHalf, false); } } } @@ -462,7 +462,7 @@ private void setInfiniteLoop() { setStartPosition(isInfiniteLoop() ? mStartPosition : 0); } - private void setRecyclerViewPadding(int itemPadding) { + public void setRecyclerViewPadding(int itemPadding) { setRecyclerViewPadding(itemPadding, itemPadding); } @@ -537,6 +537,7 @@ public int getRealCount() { /** * 是否要拦截事件 + * * @param intercept * @return */ @@ -547,6 +548,7 @@ public Banner setIntercept(boolean intercept) { /** * 跳转到指定位置(最好在设置了数据后在调用,不然没有意义) + * * @param position * @return */ @@ -556,6 +558,7 @@ public Banner setCurrentItem(int position) { /** * 跳转到指定位置(最好在设置了数据后在调用,不然没有意义) + * * @param position * @param smoothScroll * @return @@ -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} @@ -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; @@ -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); } /** diff --git a/banner/src/main/java/com/youth/banner/adapter/BannerAdapter.java b/banner/src/main/java/com/youth/banner/adapter/BannerAdapter.java index b8d1e7c..5d15764 100644 --- a/banner/src/main/java/com/youth/banner/adapter/BannerAdapter.java +++ b/banner/src/main/java/com/youth/banner/adapter/BannerAdapter.java @@ -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 extends RecyclerView.Adapter implements IViewHolder { protected List mDatas = new ArrayList<>(); @@ -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 listener) { diff --git a/banner/src/main/java/com/youth/banner/util/BannerUtils.java b/banner/src/main/java/com/youth/banner/util/BannerUtils.java index 1ce85ac..748bb9c 100644 --- a/banner/src/main/java/com/youth/banner/util/BannerUtils.java +++ b/banner/src/main/java/com/youth/banner/util/BannerUtils.java @@ -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; } @@ -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) { diff --git a/build.gradle b/build.gradle index 07aef9a..3b032b5 100644 --- a/build.gradle +++ b/build.gradle @@ -24,7 +24,7 @@ allprojects { maven { url "https://s01.oss.sonatype.org/content/groups/public" } jcenter() google() - + mavenCentral() } }