diff --git a/README.md b/README.md index b5f0c57..2af79cc 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ |卡片模式|![效果示例](/pic/1.png)| |自定义指示器模式|![效果示例](/pic/2.png)| |自定义混合模式|![效果示例](/pic/2.gif)| +|底部弧形模式|![效果示例](/pic/3.png)| ## 使用步骤 @@ -27,7 +28,7 @@ Gradle ```groovy dependencies{ - compile 'com.github.wenchaosong:Banner:2.1.8' + compile 'com.github.wenchaosong:Banner:2.1.9' } ``` 或者引用本地lib diff --git a/app/src/main/java/com/test/demo/CustomViewPagerActivity.java b/app/src/main/java/com/test/demo/CustomViewPagerActivity.java index fc9b05b..17de12e 100644 --- a/app/src/main/java/com/test/demo/CustomViewPagerActivity.java +++ b/app/src/main/java/com/test/demo/CustomViewPagerActivity.java @@ -19,6 +19,7 @@ public class CustomViewPagerActivity extends AppCompatActivity { Banner banner1; Banner banner2; + Banner banner3; @Override protected void onCreate(Bundle savedInstanceState) { @@ -27,6 +28,7 @@ protected void onCreate(Bundle savedInstanceState) { banner1 = (Banner) findViewById(R.id.banner1); banner2 = (Banner) findViewById(R.id.banner2); + banner3 = (Banner) findViewById(R.id.banner3); List list = new ArrayList<>(); list.add(new CustomData("", "CustomLayout", false)); @@ -61,6 +63,16 @@ public BannerViewHolder createViewHolder() { } }) .start(); + + banner3.setAutoPlay(true) + .setOffscreenPageLimit(list.size()) + .setPages(arrList, new HolderCreator() { + @Override + public BannerViewHolder createViewHolder() { + return new CustomViewHolder3(); + } + }) + .start(); } //如果你需要考虑更好的体验,可以这么操作 @@ -69,6 +81,8 @@ protected void onStart() { super.onStart(); //开始轮播 banner1.startAutoPlay(); + banner2.startAutoPlay(); + banner3.startAutoPlay(); } @Override @@ -76,5 +90,7 @@ protected void onStop() { super.onStop(); //结束轮播 banner1.stopAutoPlay(); + banner2.stopAutoPlay(); + banner3.stopAutoPlay(); } } diff --git a/app/src/main/res/layout/activity_custom_view_pager.xml b/app/src/main/res/layout/activity_custom_view_pager.xml index b178123..1fff90c 100644 --- a/app/src/main/res/layout/activity_custom_view_pager.xml +++ b/app/src/main/res/layout/activity_custom_view_pager.xml @@ -1,29 +1,40 @@ - + android:scrollbars="none"> - + android:layout_height="wrap_content" + android:orientation="vertical"> - + - + + + + + - + + diff --git a/pic/3.png b/pic/3.png new file mode 100644 index 0000000..cae4317 Binary files /dev/null and b/pic/3.png differ diff --git a/rollbanner/src/main/java/com/ms/banner/Banner.java b/rollbanner/src/main/java/com/ms/banner/Banner.java index 87f4ad8..7150574 100644 --- a/rollbanner/src/main/java/com/ms/banner/Banner.java +++ b/rollbanner/src/main/java/com/ms/banner/Banner.java @@ -22,6 +22,7 @@ import com.ms.banner.holder.BannerViewHolder; import com.ms.banner.holder.HolderCreator; import com.ms.banner.listener.OnBannerListener; +import com.ms.banner.view.ArcShapeView; import com.ms.banner.view.BannerViewPager; import java.lang.reflect.Field; @@ -34,7 +35,8 @@ public class Banner extends FrameLayout implements OnPageChangeListener { private static final String TAG = "Banner"; - private int mIndicatorMargin = BannerConfig.PADDING_SIZE; + private int mIndicatorPadding = BannerConfig.PADDING_SIZE; + private int mIndicatorMargin = BannerConfig.MARGIN_BOTTOM; private int mIndicatorWidth; private int mIndicatorHeight; private int indicatorSize; @@ -70,6 +72,9 @@ public class Banner extends FrameLayout implements OnPageChangeListener { private OnBannerListener listener; private DisplayMetrics dm; private int mPageMargin; + private int mArcHeight; + private int mArcBg; + private int mArcDirection; private WeakHandler handler = new WeakHandler(); @@ -96,6 +101,15 @@ private void initView(Context context, AttributeSet attrs) { handleTypedArray(context, attrs); View view = LayoutInflater.from(context).inflate(R.layout.banner, this, true); bannerDefaultImage = (ImageView) view.findViewById(R.id.bannerDefaultImage); + ArcShapeView arcShapeView = (ArcShapeView) view.findViewById(R.id.bannerArcView); + if (mArcHeight <= 0) { + arcShapeView.setVisibility(GONE); + } else { + arcShapeView.setVisibility(VISIBLE); + arcShapeView.setArcHeight(mArcHeight); + arcShapeView.setBackground(mArcBg); + arcShapeView.setDirection(mArcDirection); + } viewPager = (BannerViewPager) view.findViewById(R.id.bannerViewPager); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); @@ -104,6 +118,10 @@ private void initView(Context context, AttributeSet attrs) { viewPager.setLayoutParams(params); titleView = (LinearLayout) view.findViewById(R.id.titleView); indicator = (LinearLayout) view.findViewById(R.id.circleIndicator); + RelativeLayout.LayoutParams indicatorParam = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, + LayoutParams.WRAP_CONTENT); + indicatorParam.bottomMargin = mIndicatorMargin; + indicator.setLayoutParams(indicatorParam); indicatorInside = (LinearLayout) view.findViewById(R.id.indicatorInside); bannerTitle = (TextView) view.findViewById(R.id.bannerTitle); numIndicator = (TextView) view.findViewById(R.id.numIndicator); @@ -119,7 +137,8 @@ private void handleTypedArray(Context context, AttributeSet attrs) { TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.Banner); mIndicatorWidth = typedArray.getDimensionPixelSize(R.styleable.Banner_indicator_width, indicatorSize); mIndicatorHeight = typedArray.getDimensionPixelSize(R.styleable.Banner_indicator_height, indicatorSize); - mIndicatorMargin = typedArray.getDimensionPixelSize(R.styleable.Banner_indicator_margin, BannerConfig.PADDING_SIZE); + mIndicatorPadding = typedArray.getDimensionPixelSize(R.styleable.Banner_indicator_padding, BannerConfig.PADDING_SIZE); + mIndicatorMargin = typedArray.getDimensionPixelSize(R.styleable.Banner_indicator_margin, BannerConfig.MARGIN_BOTTOM); mIndicatorSelectedResId = typedArray.getResourceId(R.styleable.Banner_indicator_drawable_selected, R.drawable.gray_radius); mIndicatorUnselectedResId = typedArray.getResourceId(R.styleable.Banner_indicator_drawable_unselected, R.drawable.white_radius); delayTime = typedArray.getInt(R.styleable.Banner_delay_time, BannerConfig.TIME); @@ -132,6 +151,9 @@ private void handleTypedArray(Context context, AttributeSet attrs) { titleTextSize = typedArray.getDimensionPixelSize(R.styleable.Banner_title_textsize, BannerConfig.TITLE_TEXT_SIZE); bannerBackgroundImage = typedArray.getResourceId(R.styleable.Banner_banner_default_image, R.drawable.no_banner); mPageMargin = typedArray.getDimensionPixelSize(R.styleable.Banner_pageMargin, BannerConfig.PAGE_MARGIN); + mArcHeight = typedArray.getDimensionPixelSize(R.styleable.Banner_arc_height, BannerConfig.ARC_HEIGHT); + mArcBg = typedArray.getColor(R.styleable.Banner_arc_background, BannerConfig.ARC_BACKGROUND); + mArcDirection = typedArray.getInt(R.styleable.Banner_arc_direction, BannerConfig.ARC_DIRECTION); typedArray.recycle(); } @@ -362,12 +384,12 @@ private void createIndicator() { ImageView imageView = new ImageView(context); imageView.setScaleType(ScaleType.CENTER_CROP); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(mIndicatorWidth, mIndicatorHeight); - params.leftMargin = mIndicatorMargin; - params.rightMargin = mIndicatorMargin; + params.leftMargin = mIndicatorPadding; + params.rightMargin = mIndicatorPadding; LinearLayout.LayoutParams custom_params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); - custom_params.leftMargin = mIndicatorMargin; - custom_params.rightMargin = mIndicatorMargin; + custom_params.leftMargin = mIndicatorPadding; + custom_params.rightMargin = mIndicatorPadding; if (i == 0) { imageView.setImageResource(mIndicatorSelectedResId); } else { diff --git a/rollbanner/src/main/java/com/ms/banner/BannerConfig.java b/rollbanner/src/main/java/com/ms/banner/BannerConfig.java index 9cd5a6d..1859958 100644 --- a/rollbanner/src/main/java/com/ms/banner/BannerConfig.java +++ b/rollbanner/src/main/java/com/ms/banner/BannerConfig.java @@ -24,6 +24,7 @@ public class BannerConfig { * banner */ public static final int PADDING_SIZE = 5; + public static final int MARGIN_BOTTOM = 10; public static final int TIME = 2000; public static final int DURATION = 800; public static final boolean IS_AUTO_PLAY = true; @@ -42,4 +43,12 @@ public class BannerConfig { * margin */ public static final int PAGE_MARGIN = 0; + + /** + * arc + */ + public static final int ARC_HEIGHT = 0; + public static final int ARC_BACKGROUND = 0XFFFFFFFF; + public static final int ARC_DIRECTION = 0; + } diff --git a/rollbanner/src/main/java/com/ms/banner/view/ArcShapeView.java b/rollbanner/src/main/java/com/ms/banner/view/ArcShapeView.java new file mode 100644 index 0000000..ae12e59 --- /dev/null +++ b/rollbanner/src/main/java/com/ms/banner/view/ArcShapeView.java @@ -0,0 +1,71 @@ +package com.ms.banner.view; + +import android.content.Context; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.graphics.Path; +import android.util.AttributeSet; +import android.view.View; + +public class ArcShapeView extends View { + + private Paint mPaint; + private Path mPath; + private int arcHeight = 0; + private int background = 0XFFFFFFFF; + private int direction = 0; + + public ArcShapeView(Context context) { + this(context, null); + } + + public ArcShapeView(Context context, AttributeSet attrs) { + this(context, attrs, 0); + } + + public ArcShapeView(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + + mPaint = new Paint(); + mPaint.setAntiAlias(true); + mPaint.setStyle(Paint.Style.FILL); + mPath = new Path(); + } + + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + setMeasuredDimension(widthMeasureSpec, heightMeasureSpec); + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + } + + @Override + protected void onDraw(Canvas canvas) { + super.onDraw(canvas); + + mPaint.setColor(background); + + if (direction == 0) { + mPath.moveTo(0, getHeight()); + mPath.quadTo(getWidth() / 2, getHeight() - 2 * arcHeight, getWidth(), getHeight()); + canvas.drawPath(mPath, mPaint); + } else { + mPath.moveTo(0, getHeight()); + mPath.lineTo(0, getHeight() - arcHeight); + mPath.quadTo(getWidth() / 2, getHeight(), getWidth(), getHeight() - arcHeight); + mPath.lineTo(getWidth(), getHeight()); + canvas.drawPath(mPath, mPaint); + } + } + + public void setArcHeight(int arcHeight) { + this.arcHeight = arcHeight; + } + + public void setBackground(int background) { + this.background = background; + } + + public void setDirection(int direction) { + this.direction = direction; + } +} diff --git a/rollbanner/src/main/res/layout/banner.xml b/rollbanner/src/main/res/layout/banner.xml index c6e4c1d..5858659 100644 --- a/rollbanner/src/main/res/layout/banner.xml +++ b/rollbanner/src/main/res/layout/banner.xml @@ -23,7 +23,6 @@ android:layout_gravity="bottom" android:gravity="center" android:orientation="horizontal" - android:padding="10dp" android:visibility="gone" /> + + + \ No newline at end of file diff --git a/rollbanner/src/main/res/values/attr.xml b/rollbanner/src/main/res/values/attr.xml index 640c9d2..2435517 100644 --- a/rollbanner/src/main/res/values/attr.xml +++ b/rollbanner/src/main/res/values/attr.xml @@ -12,10 +12,17 @@ + + + + + + + \ No newline at end of file diff --git a/rollbanner/src/main/res/values/ids.xml b/rollbanner/src/main/res/values/ids.xml index ffe5541..d051fc8 100644 --- a/rollbanner/src/main/res/values/ids.xml +++ b/rollbanner/src/main/res/values/ids.xml @@ -9,4 +9,5 @@ + \ No newline at end of file