Skip to content

Commit

Permalink
添加弧形模式
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaomsh committed Sep 20, 2018
1 parent e6991b5 commit ac8911e
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 28 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
|卡片模式|![效果示例](/pic/1.png)|
|自定义指示器模式|![效果示例](/pic/2.png)|
|自定义混合模式|![效果示例](/pic/2.gif)|
|底部弧形模式|![效果示例](/pic/3.png)|

## 使用步骤

Expand All @@ -27,7 +28,7 @@
Gradle
```groovy
dependencies{
compile 'com.github.wenchaosong:Banner:2.1.8'
compile 'com.github.wenchaosong:Banner:2.1.9'
}
```
或者引用本地lib
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/java/com/test/demo/CustomViewPagerActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class CustomViewPagerActivity extends AppCompatActivity {

Banner banner1;
Banner banner2;
Banner banner3;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -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<CustomData> list = new ArrayList<>();
list.add(new CustomData("", "CustomLayout", false));
Expand Down Expand Up @@ -61,6 +63,16 @@ public BannerViewHolder createViewHolder() {
}
})
.start();

banner3.setAutoPlay(true)
.setOffscreenPageLimit(list.size())
.setPages(arrList, new HolderCreator<BannerViewHolder>() {
@Override
public BannerViewHolder createViewHolder() {
return new CustomViewHolder3();
}
})
.start();
}

//如果你需要考虑更好的体验,可以这么操作
Expand All @@ -69,12 +81,16 @@ protected void onStart() {
super.onStart();
//开始轮播
banner1.startAutoPlay();
banner2.startAutoPlay();
banner3.startAutoPlay();
}

@Override
protected void onStop() {
super.onStop();
//结束轮播
banner1.stopAutoPlay();
banner2.stopAutoPlay();
banner3.stopAutoPlay();
}
}
51 changes: 31 additions & 20 deletions app/src/main/res/layout/activity_custom_view_pager.xml
Original file line number Diff line number Diff line change
@@ -1,29 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:scrollbars="none">

<com.ms.banner.Banner
android:id="@+id/banner1"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginTop="50dp"
app:pageMargin="20dp" />
android:layout_height="wrap_content"
android:orientation="vertical">

<com.ms.banner.Banner
android:id="@+id/banner2"
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_marginTop="30dp" />
<com.ms.banner.Banner
android:id="@+id/banner1"
android:layout_width="match_parent"
android:layout_height="150dp"
app:pageMargin="20dp" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/banner"
android:layout_marginTop="20dp"
android:padding="10dp"
android:text="这是一个修改banner布局文件的例子,你可以按照这个套路修改里面的内容" />
<com.ms.banner.Banner
android:id="@+id/banner2"
android:layout_width="match_parent"
android:layout_height="150dp" />

<com.ms.banner.Banner
android:id="@+id/banner3"
android:layout_width="match_parent"
android:layout_height="150dp"
app:arc_background="@android:color/darker_gray"
app:arc_height="10dp"
app:indicator_margin="13dp" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray"
android:padding="10dp"
android:text="这是一个修改banner布局文件的例子,你可以按照这个套路修改里面的内容" />

</LinearLayout>
</LinearLayout>
</ScrollView>
Binary file added pic/3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 28 additions & 6 deletions rollbanner/src/main/java/com/ms/banner/Banner.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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();
}

Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions rollbanner/src/main/java/com/ms/banner/BannerConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

}
71 changes: 71 additions & 0 deletions rollbanner/src/main/java/com/ms/banner/view/ArcShapeView.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
8 changes: 7 additions & 1 deletion rollbanner/src/main/res/layout/banner.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
android:layout_gravity="bottom"
android:gravity="center"
android:orientation="horizontal"
android:padding="10dp"
android:visibility="gone" />

<TextView
Expand Down Expand Up @@ -93,4 +92,11 @@
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:visibility="gone" />

<com.ms.banner.view.ArcShapeView
android:id="@+id/bannerArcView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />

</RelativeLayout>
7 changes: 7 additions & 0 deletions rollbanner/src/main/res/values/attr.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,17 @@
<attr name="indicator_width" format="dimension" />
<attr name="indicator_height" format="dimension" />
<attr name="indicator_margin" format="dimension" />
<attr name="indicator_padding" format="dimension" />
<attr name="indicator_drawable_selected" format="reference" />
<attr name="indicator_drawable_unselected" format="reference" />
<attr name="banner_default_image" format="reference" />
<!-- 设置左右2边缩进距离 -->
<attr name="pageMargin" format="dimension" />
<attr name="arc_height" format="dimension" />
<attr name="arc_background" format="color|reference" />
<attr name="arc_direction" format="enum">
<enum name="up" value="0" />
<enum name="down" value="1" />
</attr>
</declare-styleable>
</resources>
1 change: 1 addition & 0 deletions rollbanner/src/main/res/values/ids.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
<item name="numIndicatorInside" type="id" />
<item name="indicatorInside" type="id" />
<item name="bannerDefaultImage" type="id" />
<item name="bannerArcView" type="id" />
</resources>

0 comments on commit ac8911e

Please sign in to comment.