效果图如下:
实现方式:整体是水平的LinearLayout,大小可变的圆点是自定义控件。用属性动画控制圆点宽度的变化,实现大小动态改变的效果。
具体实现步骤:
1.可变大小的指示器实现
/**
* 可缩小成圆放大成圆角矩形的view
*/
public class BannerItemView extends View {
private static final String TAG = BannerItemView.class.getSimpleName();
private Paint mPaint;
private float rectWidth;
private RectF rectF;
public static final int CENTER = 0;
public static final int LEFT = 1;
public static final int RIGHT = 2;
private int location = CENTER;
public BannerItemView(Context context) {
super(context);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
rectF = new RectF();
mPaint.setColor(Color.RED);
}
public BannerItemView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
rectF = new RectF();
mPaint.setColor(Color.RED);
}
public BannerItemView(Context context, int location) {
this(context);
this.location = location;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
switch (location) {
case CENTER:
//中间对齐
rectF.left = getWidth() / 2 - getHeight() / 2 - rectWidth;
rectF.right = getHeight() / 2 + getWidth() / 2 + rectWidth;
rectF.top = 0;
rectF.bottom = getHeight();
break;
case LEFT:
rectF.left = getWidth() - getHeight() - rectWidth;
rectF.right = getWidth();
rectF.top = 0;
rectF.bottom = getHeight();
break;
case RIGHT:
rectF.left = 0;
rectF.right = getHeight() + rectWidth;
rectF.top = 0;
rectF.bottom = getHeight();
break;
}
canvas.drawRoundRect(rectF, getHeight() / 2, getHeight() / 2, mPaint);
}
public float getRectWidth() {
return rectWidth;
}
public void setRectWidth(float rectWidth) {
this.rectWidth = rectWidth;
invalidate();
}
public int getLocation() {
return location;
}
public void setLocation(int location) {
this.location = location;
}
}
2.可以自定义的属性
<!--viewpager指示器-->
<declare-styleable name="BannerIndicator">
<!--指示器之间的间隙-->
<attr name="gap" format="dimension" />
<!--指示器的宽度-->
<attr name="slider_width" format="dimension" />
<!--指示器的高度-->
<attr name="slider_height" format="dimension" />
<!--指示器大小变化方向-->
<attr name="sleider_align">
<enum name="Left" value="1" />
<enum name="Center" value="0" />
<enum name="Right" value="2" />
</attr>
</declare-styleable>
3.具体实现代码
public class BannerIndicator extends LinearLayout {
private int dashGap;
private int position;
private static final String TAG = BannerIndicator.class.getSimpleName();
private ViewPager viewPager;
private int slider_width;
private int slider_height;
private int sliderAlign;
public BannerIndicator(Context context) {
this(context, null);
}
public BannerIndicator(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public BannerIndicator(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setOrientation(HORIZONTAL);
//获取自定义属性
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.BannerIndicator);
dashGap = (int) array.getDimension(R.styleable.BannerIndicator_gap, Tool.dpToPx(3));
slider_width = (int) array.getDimension(R.styleable.BannerIndicator_slider_width,
Tool.dpToPx(12));
slider_height = (int) array.getDimension(R.styleable.BannerIndicator_slider_height, Tool.dpToPx(4));
sliderAlign = array.getInt(R.styleable.BannerIndicator_sleider_align, 1);
array.recycle();
}
//和viewpager联动,根据viewpager页面动态生成相应的小圆点
public void setUpWidthViewPager(ViewPager viewPager) {
removeAllViews();
if (viewPager == null || viewPager.getAdapter()==null||viewPager.getAdapter().getCount() < 2) {
return;
}
position = 0;
this.viewPager = viewPager;
for (int i = 0; i < viewPager.getAdapter().getCount(); i++) {
BannerItemView imageView = new BannerItemView(getContext(), sliderAlign);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(slider_width,
slider_height);
//设置小圆点之间的距离
if (i > 0) {
layoutParams.setMargins(dashGap, 0, 0, 0);
imageView.setAlpha(0.4f);
} else {
layoutParams.setMargins(0, 0, 0, 0);
imageView.setAlpha(1);
}
imageView.setLayoutParams(layoutParams);
addView(imageView);
//默认第一个指示器增大
setLarge(0);
}
//根据viewpager页面切换事件,设置指示器大小变化
viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
if (BannerIndicator.this.position != position) {
resetSize(BannerIndicator.this.position, position);
BannerIndicator.this.position = position;
}
}
});
}
//重置指示器样式
private void resetSize(int last, int current) {
setLarge(current);
setSmall(last);
}
//指示器增大同时设置透明度变化
private void setLarge(int position) {
if (getChildAt(position) instanceof BannerItemView) {
AnimatorSet set = new AnimatorSet();
ValueAnimator animator = getEnlarge((BannerItemView) getChildAt(position));
ValueAnimator alpha = ObjectAnimator.ofFloat(getChildAt(position), "alpha", 0.4f, 1f);
set.play(animator).with(alpha);
set.setDuration(618);
set.start();
}
}
//放大动画
private ValueAnimator getEnlarge(BannerItemView roundRectView) {
return ObjectAnimator.ofFloat(roundRectView,
"rectWidth",
0, getOffset(roundRectView));
}
//根据大小变化方向获取指示器大小偏移量
private int getOffset(BannerItemView bannerItemView) {
int offsest = 0;
switch (bannerItemView.getLocation()) {
case BannerItemView.CENTER:
offsest = (slider_width - slider_height) / 2;
break;
case BannerItemView.LEFT:
offsest = slider_width - slider_height;
break;
case BannerItemView.RIGHT:
offsest = slider_width - slider_height;
break;
}
return offsest;
}
//缩小动画
private ValueAnimator getShrink(BannerItemView roundRectView) {
return ObjectAnimator.ofFloat(roundRectView,
"rectWidth",
getOffset(roundRectView), 0);
}
//缩小动画同事伴随透明度变化
public void setSmall(int small) {
if (getChildAt(small) instanceof BannerItemView) {
AnimatorSet set = new AnimatorSet();
ValueAnimator alpha = ObjectAnimator.ofFloat(getChildAt(position), "alpha", 1, 0.4f);
ValueAnimator animator = getShrink((BannerItemView) getChildAt(small));
set.play(animator).with(alpha);
set.setDuration(618);
set.start();
}
}
}
4.具体使用
4.1布局文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#dedede"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.example.demo.BannerIndicator
android:id="@+id/indicator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="10dp"
app:slider_height="10dp"
app:slider_width="24dp"
app:sleider_align="Center"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/viewpager" />
</android.support.constraint.ConstraintLayout>
4.2.代码实现
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init(){
ViewPager viewPager=findViewById(R.id.viewpager);
BannerIndicator indicator=findViewById(R.id.indicator);
final List<TestFragment>datas=new ArrayList<>();
for (int i = 0; i <8 ; i++) {
datas.add(new TestFragment());
}
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int position) {
return datas.get(position);
}
@Override
public int getCount() {
return datas.size();
}
});
indicator.setUpWidthViewPager(viewPager);
}
}
4.3.上面用到的fragment和布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
public class TestFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_layout,container,false);
}
}