package com.bitcare.view; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Typeface; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; /** * @date 2013-8-6 */ public class MySideBar extends View { OnTouchingLetterChangedListener onTouchingLetterChangedListener; // 26個字母 public static String[] b = { "#", "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" }; int choose = -1; Paint paint = new Paint(); public MySideBar(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public MySideBar(Context context, AttributeSet attrs) { super(context, attrs); } public MySideBar(Context context) { super(context); } /** * 重寫這個方法 */ protected void onDraw(Canvas canvas) { super.onDraw(canvas); int height = getHeight(); int width = getWidth(); int singleHeight = height / b.length; for (int i = 0; i < b.length; i++) { paint.setColor(Color.BLACK); paint.setTypeface(Typeface.DEFAULT_BOLD); paint.setAntiAlias(true); paint.setTextSize(20); if (i == choose) { paint.setColor(Color.RED); paint.setFakeBoldText(true); } float xPos = width / 2 - paint.measureText(b[i]) / 2; float yPos = singleHeight * i + singleHeight; 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: 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: listener.onTouchingUp(); choose = -1; invalidate(); break; } return true; } @Override public boolean onTouchEvent(MotionEvent event) { return super.onTouchEvent(event); } /** * 向外公開的方法 * * @param onTouchingLetterChangedListener */ public void setOnTouchingLetterChangedListener(OnTouchingLetterChangedListener onTouchingLetterChangedListener) { this.onTouchingLetterChangedListener = onTouchingLetterChangedListener; } /** * 接口 */ public interface OnTouchingLetterChangedListener { public void onTouchingLetterChanged(String s);//改變事件 public void onTouchingUp();//擡手事件 } }Activity中的代碼
TextView overlay;// 顯示選中哪一個字母 private void initOverlay() { overlay = new TextView(context); overlay.setGravity(Gravity.CENTER); overlay.setBackgroundResource(R.color.white); overlay.setTextColor(getResources().getColor(R.color.blue_side_txt)); overlay.setTextSize(70); overlay.setWidth(140); overlay.setHeight(140); overlay.setVisibility(View.INVISIBLE); WindowManager.LayoutParams lp = new WindowManager.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_APPLICATION, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, PixelFormat.TRANSLUCENT); WindowManager windowManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE); windowManager.addView(overlay, lp); }在onCreate中使用該方法,初始化彈出的
private OnTouchingLetterChangedListener changedListener = new OnTouchingLetterChangedListener() { @Override public void onTouchingLetterChanged(String s) { overlay.setText(s); overlay.setVisibility(View.VISIBLE); if (firstAlphabet.get(s) != null) { lvCity.setSelection(firstAlphabet.get(s)); } } @Override public void onTouchingUp() { overlay.setVisibility(View.INVISIBLE); } };
版權聲明:本文爲博主原創文章,未經博主容許不得轉載。java