package com.ioc.cn.custom;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
/**
* Created by Cwm on 2018/3/13.
* 右侧字母触摸处理
*
*/
public class LetterSide extends View {
// 定义26个字母
public static String[] mLetters = {"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 mPaint;
private String mCurrentLetters = null;
public LetterSide(Context context) {
this(context, null);
}
public LetterSide(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public LetterSide(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mPaint = new Paint();
mPaint.setColor(Color.BLUE);
mPaint.setTextSize(px2sp(15));
}
// px 转 sp
private float px2sp(int px) {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
px, getResources().getDisplayMetrics());
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//高度
int height = MeasureSpec.getSize(heightMeasureSpec);
//宽度 (左右间距+字符宽度)
int width = (int) (getPaddingLeft() + getPaddingRight() + mPaint.measureText("A"));
setMeasuredDimension(width, height);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int itmeHeight = (getHeight() - getPaddingTop() - getPaddingBottom()) / mLetters.length;
for (int i = 0; i < mLetters.length; i++) {
//首先获取每个字母的中心位置 以方便获取基线
int centreY = i * itmeHeight + itmeHeight / 2 + getPaddingTop();
Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
int dy = (int) ((fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom);
int baseLine = centreY + dy;
//宽度
int baseX = (int) (getWidth() / 2 - mPaint.measureText(mLetters[i]) / 2);
if (mLetters[i].equals(mCurrentLetters)) {//根据选中的进行变色处理(这里最好在初始化一个画笔进行处理)
mPaint.setColor(Color.RED);
canvas.drawText(mLetters[i], baseX, baseLine, mPaint);
} else {
mPaint.setColor(Color.BLUE);
canvas.drawText(mLetters[i], baseX, baseLine, mPaint);
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
int y = (int) event.getY();
//每个字符的高度
int itmeHeight = (getHeight() - getPaddingTop() - getPaddingBottom()) / mLetters.length;
int pon = y / itmeHeight;//获取触摸字符位置
if (pon < 0)//越界处理
pon = 0;
if (pon >= mLetters.length)
pon = mLetters.length - 1;
mCurrentLetters = mLetters[pon];
if (mLinter != null) {//触摸时显示中间字符
mLinter.onTouchLinter(mCurrentLetters, true);
}
invalidate();
break;
case MotionEvent.ACTION_UP://抬起时 中间显示的字符消失
postDelayed(new Runnable() {//为了消失的时候不突兀,延迟两秒消失
@Override
public void run() {
if (mLinter != null) {
mLinter.onTouchLinter(mCurrentLetters, false);
}
}
}, 2000);
break;
}
return true;
}
private LetteronTouchLinter mLinter;
public void setLinter(LetteronTouchLinter linter) {
this.mLinter = linter;
}
//接口回调处理中心展示字符
public interface LetteronTouchLinter {
void onTouchLinter(String letter, boolean flag);
}
}
字母索引列表(右侧字母触摸处理)
最新推荐文章于 2024-05-14 09:43:58 发布