Skip to content

Commit

Permalink
优化弧形渐变模式
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaomsh committed May 20, 2019
1 parent 04a5224 commit 5c7371a
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 19 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ repositories {
}
dependencies{
implementation 'com.github.wenchaosong:Banner:2.3.10'
implementation 'com.github.wenchaosong:Banner:2.3.11'
// 或者
implementation('com.github.wenchaosong:Banner:2.3.10') {
implementation('com.github.wenchaosong:Banner:2.3.11') {
exclude group: 'com.android.support'
}
}
Expand Down Expand Up @@ -160,7 +160,8 @@ protected void onStop() {
|page_left_margin|dimension|左边缩进的距离|
|page_right_margin|dimension|右边缩进的距离|
|arc_height|dimension|底部弧形的高度|
|arc_background|reference|底部弧形的背景颜色|
|arc_start_color|reference|底部弧形的起始颜色|
|arc_end_color|reference|底部弧形的结束颜色|
|arc_direction|enum|底部弧形的方向|

##### java 文件可调用的方法
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/test/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected void onCreate(Bundle savedInstanceState) {
.setDelayTime(3000)
.setOnBannerClickListener(new OnBannerClickListener() {
@Override
public void onBannerClick(int position) {
public void onBannerClick(List datas, int position) {
Toast.makeText(MainActivity.this, "你点击了:" + position, Toast.LENGTH_SHORT).show();
}
})
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/layout/activity_custom_view_pager.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginTop="30dp"
app:arc_background="@android:color/darker_gray"
app:arc_end_color="#009cff"
app:arc_height="10dp"
app:arc_start_color="#00ffff"
app:indicator_margin="13dp" />

<TextView
Expand Down
9 changes: 5 additions & 4 deletions banner/src/main/java/com/ms/banner/Banner.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public class Banner extends FrameLayout implements OnPageChangeListener {
private int mPageLeftMargin;
private int mPageRightMargin;
private int mArcHeight;
private int mArcBg;
private int mArcStartColor, mArcEndColor;
private int mArcDirection;
private static final int NUM = 5000;
private WeakHandler handler = new WeakHandler();
Expand Down Expand Up @@ -110,7 +110,7 @@ private void initView(Context context, AttributeSet attrs) {
} else {
arcShapeView.setVisibility(VISIBLE);
arcShapeView.setArcHeight(mArcHeight);
arcShapeView.setBackground(mArcBg);
arcShapeView.setBackground(mArcStartColor, mArcEndColor);
arcShapeView.setDirection(mArcDirection);
}
viewPager = view.findViewById(R.id.bannerViewPager);
Expand Down Expand Up @@ -156,7 +156,8 @@ private void handleTypedArray(Context context, AttributeSet attrs) {
mPageLeftMargin = typedArray.getDimensionPixelSize(R.styleable.Banner_page_left_margin, BannerConfig.PAGE_MARGIN);
mPageRightMargin = typedArray.getDimensionPixelSize(R.styleable.Banner_page_right_margin, 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);
mArcStartColor = typedArray.getColor(R.styleable.Banner_arc_start_color, BannerConfig.ARC_BACKGROUND);
mArcEndColor = typedArray.getColor(R.styleable.Banner_arc_end_color, BannerConfig.ARC_BACKGROUND);
mArcDirection = typedArray.getInt(R.styleable.Banner_arc_direction, BannerConfig.ARC_DIRECTION);
typedArray.recycle();
}
Expand Down Expand Up @@ -584,7 +585,7 @@ public Object instantiateItem(ViewGroup container, final int position) {
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
listener.onBannerClick(toRealPosition(position));
listener.onBannerClick(mDatas, toRealPosition(position));
}
});
}
Expand Down
9 changes: 5 additions & 4 deletions banner/src/main/java/com/ms/banner/BannerNew.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public class BannerNew extends FrameLayout implements OnPageChangeListener {
private int mPageLeftMargin;
private int mPageRightMargin;
private int mArcHeight;
private int mArcBg;
private int mArcStartColor, mArcEndColor;
private int mArcDirection;
private WeakHandler handler = new WeakHandler();

Expand Down Expand Up @@ -109,7 +109,7 @@ private void initView(Context context, AttributeSet attrs) {
} else {
arcShapeView.setVisibility(VISIBLE);
arcShapeView.setArcHeight(mArcHeight);
arcShapeView.setBackground(mArcBg);
arcShapeView.setBackground(mArcStartColor, mArcEndColor);
arcShapeView.setDirection(mArcDirection);
}
viewPager = view.findViewById(R.id.bannerViewPager);
Expand Down Expand Up @@ -155,7 +155,8 @@ private void handleTypedArray(Context context, AttributeSet attrs) {
mPageLeftMargin = typedArray.getDimensionPixelSize(R.styleable.Banner_page_left_margin, BannerConfig.PAGE_MARGIN);
mPageRightMargin = typedArray.getDimensionPixelSize(R.styleable.Banner_page_right_margin, 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);
mArcStartColor = typedArray.getColor(R.styleable.Banner_arc_start_color, BannerConfig.ARC_BACKGROUND);
mArcEndColor = typedArray.getColor(R.styleable.Banner_arc_end_color, BannerConfig.ARC_BACKGROUND);
mArcDirection = typedArray.getInt(R.styleable.Banner_arc_direction, BannerConfig.ARC_DIRECTION);
typedArray.recycle();
}
Expand Down Expand Up @@ -580,7 +581,7 @@ public Object instantiateItem(ViewGroup container, final int position) {
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
listener.onBannerClick(toRealPosition(position));
listener.onBannerClick(mDatas, toRealPosition(position));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.ms.banner.listener;

import java.util.List;

public interface OnBannerClickListener {

void onBannerClick(int position);
void onBannerClick(List datas, int position);
}
14 changes: 10 additions & 4 deletions banner/src/main/java/com/ms/banner/view/ArcShapeView.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

Expand All @@ -12,7 +14,8 @@ public class ArcShapeView extends View {
private Paint mPaint;
private Path mPath;
private int arcHeight = 0;
private int background = 0XFFFFFFFF;
private int startColor = 0XFFFFFFFF;
private int endColor = 0XFFFFFFFF;
private int direction = 0;

public ArcShapeView(Context context) {
Expand Down Expand Up @@ -42,7 +45,9 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

mPaint.setColor(background);
LinearGradient gradient = new LinearGradient(0, 0, getWidth(), getHeight(),
startColor, endColor, Shader.TileMode.CLAMP);
mPaint.setShader(gradient);

if (direction == 0) {
mPath.moveTo(0, getHeight());
Expand All @@ -61,8 +66,9 @@ public void setArcHeight(int arcHeight) {
this.arcHeight = arcHeight;
}

public void setBackground(int background) {
this.background = background;
public void setBackground(int startColor, int endColor) {
this.startColor = startColor;
this.endColor = endColor;
}

public void setDirection(int direction) {
Expand Down
3 changes: 2 additions & 1 deletion banner/src/main/res/values/attr.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
<attr name="page_left_margin" format="dimension" />
<attr name="page_right_margin" format="dimension" />
<attr name="arc_height" format="dimension" />
<attr name="arc_background" format="color|reference" />
<attr name="arc_start_color" format="color|reference" />
<attr name="arc_end_color" format="color|reference" />
<attr name="arc_direction" format="enum">
<enum name="up" value="0" />
<enum name="down" value="1" />
Expand Down

0 comments on commit 5c7371a

Please sign in to comment.