android仿微信点击好友,安卓仿微信联系人快速索引

importandroid.content.Context;importandroid.graphics.Canvas;importandroid.graphics.Paint;importandroid.support.annotation.Nullable;importandroid.util.AttributeSet;importandroid.view.MotionEvent;importandroid.view.View;importcom.example.z.xxxx.R;/*** Created by z on 2017/9/21.*/

public class SlideBar extendsView {private static final String[] BAR_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 Paint paint = newPaint();private int selectIndex = -1;//选项的数组下标

privateonTouchLetterListener onTouchLetterListener;private boolean isShowBkg = false;//背景是否变化

publicSlideBar(Context context) {super(context);

}publicSlideBar(Context context, @Nullable AttributeSet attrs) {super(context, attrs);

}public SlideBar(Context context, @Nullable AttributeSet attrs, intdefStyleAttr) {super(context, attrs, defStyleAttr);

}public interfaceonTouchLetterListener {void onTouchLetterChange(String letter,booleantouch);

}public voidsetOnTouchLetterListener(onTouchLetterListener onTouchLetterListener) {this.onTouchLetterListener =onTouchLetterListener;

}

@Overrideprotected voidonDraw(Canvas canvas) {super.onDraw(canvas);if (isShowBkg == true) {

canvas.drawColor(getResources().getColor(R.color.text_backgroud_color));//显示背景

}int height =getHeight();int width =getWidth();int singleHeight = (height - 20) / BAR_LETTERS.length;//每个字母占得高度

System.out.println(height + "-------" +width);for (int i = 0; i < BAR_LETTERS.length; i++) {//遍历字母

paint.setColor(getResources().getColor(R.color.text_color));

paint.setTextSize(50);

paint.setAntiAlias(true);//设置抗锯齿样式

System.out.println(i+"-----"+selectIndex);if (i == selectIndex) {//如果被选中

System.out.println(i+"*****"+selectIndex);

paint.setColor(getResources().getColor(R.color.isread_true));

paint.setFakeBoldText(true);//加粗

}//从左下角开始绘制

float xpos = (width - paint.measureText(BAR_LETTERS[i])) / 2;//x轴

float ypos = singleHeight * i + singleHeight;//y轴 (i从0开始算)//System.out.println(xpos + "" + ypos);

canvas.drawText(BAR_LETTERS[i], xpos, ypos, paint);//开始绘制

paint.reset();//重置画笔

}

}

@Overridepublic booleandispatchTouchEvent(MotionEvent event) {int action =event.getAction();float ypos =event.getY();int oldSelectIndex = selectIndex;//上一次触发的坐标

int newSelectIndex = (int) (ypos / getHeight() * BAR_LETTERS.length);//根据触摸的位置确定点的哪个字母

switch(action) {caseMotionEvent.ACTION_DOWN:

isShowBkg= true;if (onTouchLetterListener != null && oldSelectIndex !=newSelectIndex&& newSelectIndex >= 0 && newSelectIndex <=BAR_LETTERS.length) {

onTouchLetterListener.onTouchLetterChange(BAR_LETTERS[newSelectIndex],true);//注册点击事件并传入字母

oldSelectIndex =newSelectIndex;

selectIndex=newSelectIndex;

invalidate();//重新绘制

}break;caseMotionEvent.ACTION_UP:

isShowBkg= false;

selectIndex= -1;

invalidate();

onTouchLetterListener.onTouchLetterChange(BAR_LETTERS[newSelectIndex],false);//注册点击事件并传入字母

break;caseMotionEvent.ACTION_MOVE:if (onTouchLetterListener != null && oldSelectIndex !=newSelectIndex&& newSelectIndex >= 0 && newSelectIndex <=BAR_LETTERS.length) {

onTouchLetterListener.onTouchLetterChange(BAR_LETTERS[newSelectIndex],true);//注册点击事件并传入字母

oldSelectIndex =newSelectIndex;

selectIndex=newSelectIndex;

invalidate();//重新绘制

}break;

}return true;

}

@Overridepublic booleanonTouchEvent(MotionEvent event) {return super.onTouchEvent(event);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值