自定义view代码,继承view
package com.example.myapplication;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
/**
* 原点加选中拉伸指示器
* create by lihui
*/
public class DotIndicate extends View {
private static final int DEFAULT_DOT_NUMBER = 3;
private int dotNumber = DEFAULT_DOT_NUMBER;
private int norMalColor = Color.GRAY;
private int selectColor = Color.WHITE;
private boolean enableStretch = true;
private int stretchSize = 10;
private int dotInterval = 10;
private int dotRadio = 3;
private Paint paint;
private int selectIndex = 0;
private RectF rect;
public DotIndicate(Context context) {
this(context,null);
}
public DotIndicate(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public DotIndicate(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.DotIndicate);
dotNumber = typedArray.getInt(R.styleable.DotIndicate_dotNumber, DEFAULT_DOT_NUMBER);
norMalColor = typedArray.getColor(R.styleable.DotIndicate_normalColor,Color.GRAY);
selectColor = typedArray.getColor(R.styleable.DotIndicate_selectColor,Color.WHITE);
enableStretch = typedArray.getBoolean(R.styleable.DotIndicate_stretchEnable,true);
stretchSize = dp2px(context,typedArray.getDimension(R.styleable.DotIndicate_stretchSize,10));
dotInterval = dp2px(context,typedArray.getDimension(R.styleable.DotIndicate_dotInterval,10));
dotRadio = dp2px(context,typedArray.getDimension(R.styleable.DotIndicate_dotRadio,3));
typedArray.recycle();
paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
paint.setColor(norMalColor);
rect = new RectF(0,0,stretchSize,dotRadio * 2);
}
private int dp2px(Context context ,float dp) {
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
float density = displayMetrics.density;
return (int)(density * dp + 0.5);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if (widthMode == MeasureSpec.AT_MOST) {
widthSize = calculateWidthSize();
}
if (heightMode == MeasureSpec.AT_MOST) {
heightSize = dotRadio * 2;
}
setMeasuredDimension(widthSize,heightSize);
}
private int calculateWidthSize() {
int selectDotWidth = dotRadio * 2;
int normalDotWidth = dotRadio * 2;
if (enableStretch) {
selectDotWidth += stretchSize;
}
return selectDotWidth + (dotNumber - 1) * (normalDotWidth + dotInterval);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int currentLeft = 0;
for (int i = 0; i < dotNumber ; i ++) {
if (selectIndex == i) {
paint.setColor(selectColor);
if (enableStretch) {
rect.set(currentLeft,0,currentLeft + stretchSize,dotRadio * 2);
canvas.drawRoundRect(rect, dotRadio,dotRadio,paint );
currentLeft += (stretchSize + dotInterval);
} else {
canvas.drawCircle(currentLeft + dotRadio,dotRadio,dotRadio,paint);
currentLeft += (2 * dotRadio + dotInterval);
}
} else {
paint.setColor(norMalColor);
canvas.drawCircle(currentLeft + dotRadio,dotRadio,dotRadio,paint);
currentLeft += (2 * dotRadio + dotInterval);
}
}
}
public void setSelect(int index) {
selectIndex = index;
invalidate();
}
}
自定义属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="DotIndicate" >
<attr name="normalColor" format="color"/>
<attr name="selectColor" format="color"/>
<attr name="dotRadio" format="dimension"/>
<attr name="dotNumber" format="integer"/>
<attr name="stretchEnable" format="boolean"/>
<attr name="stretchSize" format="dimension"/>
<attr name="dotInterval" format="dimension"/>
</declare-styleable>
</resources>
使用方式
<com.example.myapplication.DotIndicate
android:id="@+id/di"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:dotNumber="7"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
DotIndicate viewById = findViewById(R.id.di);
viewById.setSelect(3);