Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 50 additions & 4 deletions app/src/main/java/qdx/indexbarlayout/IndexBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.ColorInt;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
Expand All @@ -13,7 +16,8 @@


public class IndexBar extends View {
private Paint mPaint;
private final String TAG = "qdx";
private Paint mPaint, mBarBgPaint;
private int textSpan;//每个index占据空间
private IndexChangeListener listener;
private List<String> indexsList;
Expand All @@ -22,6 +26,13 @@ public class IndexBar extends View {
private int norTextColor = Color.GRAY;
private float yAxis;//文字y轴方向的基线

private RectF mIndexBarBg;
//默认的背景色
private int norBarBgColor = ContextCompat.getColor(getContext(), android.R.color.transparent);
//按下时的背景色,默认为透明
private int selBarBgColor = ContextCompat.getColor(getContext(), android.R.color.transparent);
private int mIndexBarBgRadius = 0;

public IndexBar(Context context) {
this(context, null);
}
Expand All @@ -36,6 +47,11 @@ public IndexBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr)
}

private void init() {
//设置背景属性
mBarBgPaint = new Paint();
mBarBgPaint.setColor(norBarBgColor);
mBarBgPaint.setAntiAlias(true);
//设置文字属性
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(norTextColor);
mPaint.setTextSize(textSize);
Expand All @@ -52,16 +68,27 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
if (indexsList != null && indexsList.size() > 0) {
textSpan = h / (indexsList.size() + 1);
}
//背景大小
mIndexBarBg = new RectF(
0,
0,
w,
h
);
}

@Override
protected void onDraw(Canvas canvas) {
//先绘制背景
canvas.drawRoundRect(mIndexBarBg, mIndexBarBgRadius, mIndexBarBgRadius, mBarBgPaint);
//再绘制文字
if (indexsList != null && indexsList.size() > 0) {
for (int i = 0; i < indexsList.size(); i++) {
canvas.drawText(indexsList.get(i), getWidth() / 2, textSpan * (i + 1) + yAxis, mPaint);
}
}


}

private int curPos = -1;
Expand All @@ -74,12 +101,12 @@ public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mPaint.setColor(selTextColor);
mBarBgPaint.setColor(selBarBgColor);
invalidate();
case MotionEvent.ACTION_MOVE:
if (event.getY() < textSpan / 2 || (event.getY() - textSpan / 2) > textSpan * indexsList.size()) {
return true;
}

int position = (int) ((event.getY() - textSpan / 2) / textSpan * 1.0f);
if (position >= 0 && position < indexsList.size()) {
((IndexLayout) getParent()).drawCircle(event.getY(), indexsList.get(position));
Expand All @@ -92,6 +119,7 @@ public boolean onTouchEvent(MotionEvent event) {
case MotionEvent.ACTION_UP:
((IndexLayout) getParent()).dismissCircle();
mPaint.setColor(norTextColor);
mBarBgPaint.setColor(norBarBgColor);
invalidate();
break;
}
Expand All @@ -117,12 +145,30 @@ public void setIndexTextSize(int textSize) {
mPaint.setTextSize(textSize);
}

public void setSelTextColor(int selTextColor) {
public void setSelTextColor(@ColorInt int selTextColor) {
this.selTextColor = selTextColor;
}

public void setNorTextColor(int norTextColor) {
public void setNorTextColor(@ColorInt int norTextColor) {
this.norTextColor = norTextColor;
mPaint.setColor(norTextColor);
}

public int getNorBarBgColor() {
return norBarBgColor;
}

public void setNorBarBgColor(@ColorInt int norBarBgColor) {
this.norBarBgColor = norBarBgColor;
mBarBgPaint.setColor(norBarBgColor);
}

public int getSelBarBgColor() {
return selBarBgColor;
}

public void setSelBarBgColor(@ColorInt int selBarBgColor) {
this.selBarBgColor = selBarBgColor;

}
}
3 changes: 2 additions & 1 deletion app/src/main/java/qdx/indexbarlayout/IndexLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.FloatRange;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
Expand Down Expand Up @@ -227,7 +228,7 @@ public void setDrawByTouch(boolean drawByTouch) {
/**
* indexBar 高度占父容器的比率,默认1
*/
public void setIndexBarHeightRatio(float indexBarHeightRatio) {
public void setIndexBarHeightRatio(@FloatRange(from = 0.0f,to = 1.0f) float indexBarHeightRatio) {
this.indexBarHeightRatio = indexBarHeightRatio;
}

Expand Down
9 changes: 5 additions & 4 deletions app/src/main/java/qdx/indexbarlayout/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,13 @@ public void headerClick(int pos) {
heads.add(car.getInitial());
}
}
indexLayout.setIndexBarHeightRatio(0.9f);
indexLayout.setIndexBarHeightRatio(0.7f);
indexLayout.getIndexBar().setIndexsList(heads);
indexLayout.setCircleTextColor(Color.RED);
indexLayout.setCircleRadius(200);
indexLayout.setCirCleTextSize(150);
indexLayout.setCircleTextColor(Color.WHITE);
indexLayout.setCircleRadius(120);
indexLayout.setCirCleTextSize(80);
indexLayout.setCircleColor(ContextCompat.getColor(this, R.color.circle_bg));
indexLayout.getIndexBar().setSelBarBgColor(Color.parseColor("#45000000"));
indexLayout.getIndexBar().setIndexChangeListener(new IndexBar.IndexChangeListener() {
@Override
public void indexChanged(String indexName) {
Expand Down