<p style="font-family: Monaco; font-size: 11px; margin-top: 0px; margin-bottom: 0px;"><span style="color: #931a68">public</span> <span style="color: #931a68">class</span> AlphaView <span style="color: #931a68">extends</span> View {</p><p style="font-family: Monaco; font-size: 11px; margin-top: 0px; margin-bottom: 0px; min-height: 15px;">
</p><p style="font-family: Monaco; font-size: 11px; margin-top: 0px; margin-bottom: 0px;"><span style="white-space:pre"> </span>OnTouchingLetterChangedListener <span style="color: #0326cc">onTouchingLetterChangedListener</span>;</p><p style="font-family: Monaco; font-size: 11px; margin-top: 0px; margin-bottom: 0px;"><span style="white-space:pre"> </span><span style="color: #931a68">int</span> <span style="color: #0326cc">choose</span> = -1; //當前選中首字母的位置</p><p style="font-family: Monaco; font-size: 11px; margin-top: 0px; margin-bottom: 0px;"><span style="white-space:pre"> </span>Paint <span style="color: #0326cc">paint</span> = <span style="color: #931a68">new</span> Paint();</p><p style="font-family: Monaco; font-size: 11px; margin-top: 0px; margin-bottom: 0px; color: rgb(147, 26, 104);"><span style="color: #000000"><span style="white-space:pre"> </span></span>boolean<span style="color: #000000"> </span><span style="color: #0326cc">showBkg</span><span style="color: #000000"> = </span>false<span style="color: #000000">;</span></p><p style="font-family: Monaco; font-size: 11px; margin-top: 0px; margin-bottom: 0px;"><span style="white-space:pre"> </span><span style="color: #931a68">private</span> ArrayList<String> <span style="color: #0326cc">letters</span>;</p><p style="font-family: Monaco; font-size: 11px; margin-top: 0px; margin-bottom: 0px; color: rgb(78, 144, 114);"><span style="color: #000000"><span style="white-space:pre"> </span>String[] </span><span style="color: #0326cc">b</span><span style="color: #000000">; </span>//= {"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",};</p><p style="font-family: Monaco; font-size: 11px; margin-top: 0px; margin-bottom: 0px;"><span style="white-space:pre"></span><pre name="code" class="java" style="font-size: 11px;"><p style="margin-top: 0px; margin-bottom: 0px; font-family: Monaco;"><span style="color: rgb(147, 26, 104);"> public</span> AlphaView(Context context) {</p><p style="margin-top: 0px; margin-bottom: 0px; font-family: Monaco;"><span> </span><span style="color: rgb(147, 26, 104);">super</span>(context);</p><p style="margin-top: 0px; margin-bottom: 0px; font-family: Monaco;"><span style="font-family: Monaco; font-size: 11px; white-space: pre; background-color: rgb(240, 240, 240);"> }</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-family: Monaco;"><pre name="code" class="java" style="font-size: 11px;"><p style="margin-top: 0px; margin-bottom: 0px; font-family: Monaco;"><span style="color: rgb(147, 26, 104);"> public</span> AlphaView(Context context, AttributeSet attrs) {</p><p style="margin-top: 0px; margin-bottom: 0px; font-family: Monaco;"><span> </span><span style="color: rgb(147, 26, 104);">super</span>(context, attrs);</p><p style="margin-top: 0px; margin-bottom: 0px; font-family: Monaco;"><span> </span>}</p><p style="margin-top: 0px; margin-bottom: 0px; font-family: Monaco;"> <span style="color: rgb(147, 26, 104);"> public</span> AlphaView(Context context, AttributeSet attrs, <span style="color: rgb(147, 26, 104);">int</span> defStyle) {</p>
super(context, attrs, defStyle);
}
public void setLetters(ArrayList<String> letters){
this.letters = letters;
b = new String[letters.size()];
b = this.letters.toArray(b);
Paint demoPaint = new Paint();
demoPaint.setAntiAlias(true);
demoPaint.setTypeface(Typeface.DEFAULT_BOLD);
demoPaint.setTextSize(18f);
demoPaint.setAntiAlias(true);
Rect bounds = new Rect();
demoPaint.getTextBounds(b[0], 0, 1, bounds);
//字母實際高度
int textHeight = Math.abs(bounds.height());
//26個字母平分屏幕高度
int height = (DensityUtil.getWindowWH()[1]-DensityUtil.dip2px(32))/26;
getLayoutParams().height = textHeight*b.length+height/2*b.length;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float height = getHeight();
float width = getWidth();
paint.setAntiAlias(true); //給paint設置抗鋸齒效果
//根據是否顯示背景顏色來爲paint設置不同的顏色值
if (showBkg) { //顯示背景顏色
paint.setColor(Color.parseColor("#40000000"));
} else { //不顯示背景顏色
paint.setColor(Color.TRANSPARENT);
}
//畫一個圓角矩形
canvas.drawRoundRect(new RectF(0, 0, width, height), width / 2.0f,
width / 2.0f, paint);
float singleHeight = height / b.length;
int green1 = getContext().getResources().getColor(
android.R.color.black);
for (int i = 0; i < b.length; i++) {
paint.setColor(green1);
paint.setTypeface(Typeface.DEFAULT_BOLD); //設置字體爲默認黑體
paint.setTextSize(18f);
paint.setAntiAlias(true);
if (i == choose) {
paint.setColor(Color.parseColor("#3399ff"));
paint.setFakeBoldText(true); //設置爲粗體 ,false是非粗體
}
//paint.measureText(b[i])取得字符串的寬度
float xPos = width / 2 - paint.measureText(b[i]) / 2;
// float yPos = singleHeight * i + singleHeight;
Rect bounds = new Rect();
paint.getTextBounds(b[i], 0, 1, bounds);
int textHeight = Math.abs(bounds.height());
float yPos = singleHeight * i + singleHeight
- ((singleHeight - textHeight) / 2);
//xPos指的是b[i]字符左上角的位置,yPos指的是b[i]字符baseline 在屏幕上的位置
canvas.drawText(b[i], xPos, yPos, paint);
paint.reset();
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
final int action = event.getAction();
final float y = event.getY();
final int oldChoose = choose;
final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;
final int c = (int) (y / getHeight() * b.length);
switch (action) {
case MotionEvent.ACTION_DOWN:
showBkg = true;
if (oldChoose != c && listener != null) {
if (c >= 0 && c < b.length) {
listener.onTouchingLetterChanged(b[c]);
choose = c;
invalidate();
}
}
break;
case MotionEvent.ACTION_MOVE:
if (oldChoose != c && listener != null) {
if (c >= 0 && c < b.length) {
listener.onTouchingLetterChanged(b[c]);
choose = c;
invalidate();
}
}
break;
case MotionEvent.ACTION_UP:
showBkg = false;
choose = -1;
invalidate();
break;
}
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return super.onTouchEvent(event);
}
public void setOnTouchingLetterChangedListener(
OnTouchingLetterChangedListener onTouchingLetterChangedListener) {
this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
}
public interface OnTouchingLetterChangedListener {
public void onTouchingLetterChanged(String s);
}
}