此类查找控件有很多帖子,很久没有写过这个控件了 ^_^,闲着练练手。
下面贴出源码:
package com.lt.vs.come.custom;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
//import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
//import android.view.ViewGroup;
import android.widget.TextView;
/**
* Created by 金色VS时空 on 2018/2/20.
*/
public class ContactBannerView extends View {
public ContactBannerView(Context context) {
super(context);
}
public ContactBannerView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ContactBannerView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
private Paint mPaint;
/**
* 字母列表资源
*/
private String []letters={"A","B","C","D","E","F","G","H",
"I","J","K","L","M","N","O",
"P","Q","R","S","T","U","V",
"W","X","Y","Z"};
/*private char[] chars={'A','B','C','D','E','F','G','H',
'I','J','K','L','M','N','O',
'P','Q','R','S','T','U','V',
'W','X','Y','Z'};*/
private OnLetterSelectedListener mOnLetterSelectedListener;
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
initPaint();
//画初始默认背景色
if (isTouch){
//画按下时背景色
mPaint.setColor(Color.parseColor(pressedColor));
}else{
//画默认背景色
mPaint.setColor(Color.parseColor(defaultColor));
}
mPaint.setStyle(Paint.Style.FILL);
canvas.drawRect(0,0,getWidth(),getHeight(),mPaint);
//绘制字母列表
mPaint.setColor(Color.BLACK);
mPaint.setTextSize(35);
int length=letters.length;
float padding=getHeight()/length;
float startX=getWidth()/2;
float startY=padding-10;
Log.e("==========",""+startY);
for (int i = 0; i
控件还可以拓展接口。喜欢的点个赞吧
这是更新的简洁版本代码如下,很简单不多解释了:
package com.example.myapplication.coustom;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
/**
* email:[email protected]
* Created by gold on 2019/9/25
* Describe:
**/
public class ABarView extends View {
public ABarView(Context context) {
this(context,null);
}
public ABarView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public ABarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initPaint();
}
private Paint paint;
private int w = 100;
private int h;
private List abc;
private int startX,startY;
private int perLine;
private int padding=100;//上下安全距离
private void initPaint() {
paint=new Paint();
paint.setTextSize(12f);
paint.setAntiAlias(true);
//设置绘制整体区域
abc=new ArrayList<>();
for (int i = 0; i < 26; i++) {
abc.add(String.valueOf((char)('A' + i)));
}
Log.e("@@@@==",abc.toString());
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
w = MeasureSpec.getSize(widthMeasureSpec); //取出宽度的确切数值
h = MeasureSpec.getSize(heightMeasureSpec); //取出高度的确切数值
perLine = (h-padding*2)/abc.size();
setMeasuredDimension(w,h);
}
private boolean isPressed;
private List points=new ArrayList<>();
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
//绘制常态背景
paint.setShadowLayer(20 , 0, 0, Color.parseColor("#436EEE"));
if (!isPressed){
paint.setColor(Color.parseColor("#F8F8FF"));
}else{
paint.setColor(Color.parseColor("#DEDEDE"));
}
paint.setStyle(Paint.Style.FILL);
canvas.drawRect(new Rect(0,0,w,h),paint);
//绘制字母
for (int i = 0; i < abc.size(); i++) {
if (getIndex()==i){
paint.setColor(Color.parseColor("#000000"));
paint.setTextSize(sp2px(getContext(),16));
paint.setStyle(Paint.Style.FILL);
}else{
paint.setColor(Color.parseColor("#4682B4"));
paint.setTextSize(sp2px(getContext(),12));
paint.setStyle(Paint.Style.STROKE);
}
//x表示下限值 y表示上限值
Point point = new Point();
String per = abc.get(i);
Paint.FontMetricsInt metrics=paint.getFontMetricsInt();
int per_height = metrics.descent-metrics.ascent;
Rect rect = new Rect();
paint.getTextBounds(per,0,per.length(),rect);
int per_width = rect.width();
paint.setStyle(Paint.Style.FILL);
startX = w/2-per_width/2;
startY = (padding+per_height) +perLine*i;
point.x=startY-perLine/2;
point.y=startY+perLine/2;
points.add(point);
canvas.drawText(per,startX,startY,paint);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action =event.getAction();
switch (action){
case MotionEvent.ACTION_DOWN:
isPressed=true;
Log.e("@@@@==","ACTION_DOWN="+event.getY());
choosed_ACTION_DOWN(event.getY());
break;
case MotionEvent.ACTION_MOVE:
Log.e("@@@@==","ACTION_MOVE="+event.getY());
choosed_ACTION_DOWN(event.getY());
break;
case MotionEvent.ACTION_UP:
Log.e("@@@@==","ACTION_UP="+event.getY());
isPressed=false;
invalidate();
break;
}
return true;
}
private void choosed_ACTION_DOWN(float rawY) {
for (int i = 0; i < points.size(); i++) {
if (rawY<=points.get(i).y && i==0){//选中第一个
setIndex(0);
return;
}
if (rawY>=points.get(i).x && i==points.size()-1){//选中最后一个
setIndex(abc.size()-1);
return;
}
if (rawY>points.get(i).x && rawY'A'){
setIndex(c-'A');
}else{
//防止越界
setIndex(0);
}
}
/**
* sp转换成px
*/
public float sp2px(Context context,float spValue){
float fontScale=context.getResources().getDisplayMetrics().scaledDensity;
return spValue*fontScale+0.5f;
}
private OnLetterChangedListener onLetterChangedListener;
public void setOnLetterChangedListener(OnLetterChangedListener onLetterChangedListener) {
this.onLetterChangedListener = onLetterChangedListener;
}
public interface OnLetterChangedListener{
void onChanged(int index);
}
}