自定义索引view




import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;





public class LetterView extends View {
//private NinePatchDrawable localBitmap = null;
private Drawable bg;
private static final int SPACE_RIGHT = 15;//padding left and right
private Align align = Align.CENTER;
private int count = 27;//字母个数 ;


public LetterView(Context context) {
super(context);
bg = context.getResources().getDrawable(R.drawable.reg_tel_letter_color);
}


public LetterView(Context context, AttributeSet attrs) {
super(context, attrs);
bg = context.getResources().getDrawable(R.drawable.reg_tel_letter_color);
}


public LetterView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
bg = context.getResources().getDrawable(R.drawable.reg_tel_letter_color);
}

public float getLetterHeight()
{
float h = getHeight() - 10;
float textSize = h * 5 / 6 / count;
float spaceH = (h - count * textSize) / (count - 1);//vertical margin
return  textSize+spaceH;

}

protected void onDraw(Canvas canvas) {
super.onDraw(canvas);


Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(0xff6d7276);
paint.setStyle(Style.FILL);


float h = getHeight() - 10;
float textSize = h * 5 / 6 / count;
paint.setTextSize(textSize);
paint.setTextAlign(align);


float spaceH = (h - count * textSize) / (count - 1);


if (isTouch) {
bg.setBounds(new Rect(getWidth() - (int) textSize - 2*SPACE_RIGHT, 0, getWidth(), getHeight()));
bg.draw(canvas);
}


String drawText = null;
for (int i = 0; i < 27; i++) {


if (i == 26) {
drawText = "#";
} else {
drawText = String.valueOf((char) ('A' + (i)));
}
canvas.drawText(drawText, getWidth() - textSize / 2 - SPACE_RIGHT+5, (i + 1) * (textSize + spaceH),
paint);
}
if (isTouch) {
invalidate(new Rect(getWidth() - (int) textSize  - 2*SPACE_RIGHT, 0, getWidth(),getHeight()));
}
}


private boolean isTouch = false;


public void setTouch(boolean isTouch) {
this.isTouch = isTouch;
}


}


letterView.setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub

float y = event.getY();
height = letterView.getLetterHeight();
int index = (int) (y / height);
if (index > -1 && index < alpha.length) {// 防止越界
String key = alpha[index];
if (mapIndex.containsKey(key)) {
int pos = mapIndex.get(key);
if (countryView.getHeaderViewsCount() > 0) {// 防止ListView有标题栏
countryView.setSelectionFromTop(pos + countryView.getHeaderViewsCount(), 0);
} else {
countryView.setSelectionFromTop(pos, 0);// 滑动到第一项
}
}
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
letterView.setBackgroundColor(Color.parseColor("#606060"));
break;


case MotionEvent.ACTION_MOVE:


break;
case MotionEvent.ACTION_UP:
letterView.setBackgroundColor(Color.parseColor("#00ffffff"));
break;
}
return true;
}
});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值